Skip to content

Commit cadeefe

Browse files
authored
fix(node-experimental): Guard against missing fetch (#9275)
It seems that even in Node 18 fetch may not be usable. By using the `NodePerformanceIntegration` base we actually try-catch initializing the instrumentation, which should catch this case.
1 parent f5dbca5 commit cadeefe

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

packages/node-experimental/src/integrations/node-fetch.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { Span } from '@opentelemetry/api';
22
import { SpanKind } from '@opentelemetry/api';
3-
import { registerInstrumentations } from '@opentelemetry/instrumentation';
3+
import type { Instrumentation } from '@opentelemetry/instrumentation';
44
import { hasTracingEnabled } from '@sentry/core';
5-
import type { EventProcessor, Hub, Integration } from '@sentry/types';
5+
import type { Integration } from '@sentry/types';
66
import { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node';
77

88
import { OTEL_ATTR_ORIGIN } from '../constants';
99
import type { NodeExperimentalClient } from '../sdk/client';
1010
import { getCurrentHub } from '../sdk/hub';
1111
import { getRequestSpanData } from '../utils/getRequestSpanData';
1212
import { getSpanKind } from '../utils/getSpanKind';
13+
import { NodePerformanceIntegration } from './NodePerformanceIntegration';
1314

1415
interface NodeFetchOptions {
1516
/**
@@ -31,7 +32,7 @@ interface NodeFetchOptions {
3132
* * Create breadcrumbs for outgoing requests
3233
* * Create spans for outgoing requests
3334
*/
34-
export class NodeFetch implements Integration {
35+
export class NodeFetch extends NodePerformanceIntegration<NodeFetchOptions> implements Integration {
3536
/**
3637
* @inheritDoc
3738
*/
@@ -47,7 +48,6 @@ export class NodeFetch implements Integration {
4748
*/
4849
public shouldCreateSpansForRequests: boolean;
4950

50-
private _unload?: () => void;
5151
private readonly _breadcrumbs: boolean;
5252
// If this is undefined, use default behavior based on client settings
5353
private readonly _spans: boolean | undefined;
@@ -56,6 +56,8 @@ export class NodeFetch implements Integration {
5656
* @inheritDoc
5757
*/
5858
public constructor(options: NodeFetchOptions = {}) {
59+
super(options);
60+
5961
this.name = NodeFetch.id;
6062
this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
6163
this._spans = typeof options.spans === 'undefined' ? undefined : options.spans;
@@ -64,33 +66,30 @@ export class NodeFetch implements Integration {
6466
this.shouldCreateSpansForRequests = false;
6567
}
6668

69+
/** @inheritDoc */
70+
public setupInstrumentation(): void | Instrumentation[] {
71+
return [
72+
new FetchInstrumentation({
73+
onRequest: ({ span }: { span: Span }) => {
74+
this._updateSpan(span);
75+
this._addRequestBreadcrumb(span);
76+
},
77+
}),
78+
];
79+
}
80+
6781
/**
6882
* @inheritDoc
6983
*/
70-
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
71-
// No need to instrument if we don't want to track anything
72-
if (!this._breadcrumbs && this._spans === false) {
73-
return;
74-
}
84+
public setupOnce(): void {
85+
super.setupOnce();
7586

7687
const client = getCurrentHub().getClient<NodeExperimentalClient>();
7788
const clientOptions = client?.getOptions();
7889

7990
// This is used in the sampler function
8091
this.shouldCreateSpansForRequests =
8192
typeof this._spans === 'boolean' ? this._spans : hasTracingEnabled(clientOptions);
82-
83-
// Register instrumentations we care about
84-
this._unload = registerInstrumentations({
85-
instrumentations: [
86-
new FetchInstrumentation({
87-
onRequest: ({ span }: { span: Span }) => {
88-
this._updateSpan(span);
89-
this._addRequestBreadcrumb(span);
90-
},
91-
}),
92-
],
93-
});
9493
}
9594

9695
/**

0 commit comments

Comments
 (0)