Skip to content

Commit 16594de

Browse files
authored
fix(node-experimental): Make node-fetch support optional (#9321)
Installing this on Node <18 produces install time errors, as the package is not compatible, e.g.: ``` npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '[email protected]', npm WARN EBADENGINE required: { node: '>18.0.0' }, npm WARN EBADENGINE current: { node: 'v16.18.0', npm: '8.19.2' } npm WARN EBADENGINE } ``` We already try-catch the `setupInstrumentation` hook anyhow, so no need to check again here - if this fails we'll just skip running this integration.
1 parent d7de20e commit 16594de

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

packages/node-experimental/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
"@sentry/node": "7.74.1",
4646
"@sentry/opentelemetry": "7.74.1",
4747
"@sentry/types": "7.74.1",
48-
"@sentry/utils": "7.74.1",
48+
"@sentry/utils": "7.74.1"
49+
},
50+
"optionalDependencies": {
4951
"opentelemetry-instrumentation-fetch-node": "1.1.0"
5052
},
5153
"scripts": {

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
44
import { hasTracingEnabled } from '@sentry/core';
55
import { _INTERNAL, getCurrentHub, getSpanKind } from '@sentry/opentelemetry';
66
import type { Integration } from '@sentry/types';
7-
import { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node';
87

98
import type { NodeExperimentalClient } from '../types';
109
import { addOriginToSpan } from '../utils/addOriginToSpan';
@@ -66,14 +65,20 @@ export class NodeFetch extends NodePerformanceIntegration<NodeFetchOptions> impl
6665

6766
/** @inheritDoc */
6867
public setupInstrumentation(): void | Instrumentation[] {
69-
return [
70-
new FetchInstrumentation({
71-
onRequest: ({ span }: { span: Span }) => {
72-
this._updateSpan(span);
73-
this._addRequestBreadcrumb(span);
74-
},
75-
}),
76-
];
68+
try {
69+
// eslint-disable-next-line @typescript-eslint/no-var-requires
70+
const { FetchInstrumentation } = require('opentelemetry-instrumentation-fetch-node');
71+
return [
72+
new FetchInstrumentation({
73+
onRequest: ({ span }: { span: Span }) => {
74+
this._updateSpan(span);
75+
this._addRequestBreadcrumb(span);
76+
},
77+
}),
78+
];
79+
} catch (error) {
80+
// Could not load instrumentation
81+
}
7782
}
7883

7984
/**

0 commit comments

Comments
 (0)