Skip to content

Commit

Permalink
fix(node-experimental): Guard against missing fetch (#9275)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mydea authored Oct 17, 2023
1 parent f5dbca5 commit cadeefe
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions packages/node-experimental/src/integrations/node-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import type { Instrumentation } from '@opentelemetry/instrumentation';
import { hasTracingEnabled } from '@sentry/core';
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import type { Integration } from '@sentry/types';
import { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node';

import { OTEL_ATTR_ORIGIN } from '../constants';
import type { NodeExperimentalClient } from '../sdk/client';
import { getCurrentHub } from '../sdk/hub';
import { getRequestSpanData } from '../utils/getRequestSpanData';
import { getSpanKind } from '../utils/getSpanKind';
import { NodePerformanceIntegration } from './NodePerformanceIntegration';

interface NodeFetchOptions {
/**
Expand All @@ -31,7 +32,7 @@ interface NodeFetchOptions {
* * Create breadcrumbs for outgoing requests
* * Create spans for outgoing requests
*/
export class NodeFetch implements Integration {
export class NodeFetch extends NodePerformanceIntegration<NodeFetchOptions> implements Integration {
/**
* @inheritDoc
*/
Expand All @@ -47,7 +48,6 @@ export class NodeFetch implements Integration {
*/
public shouldCreateSpansForRequests: boolean;

private _unload?: () => void;
private readonly _breadcrumbs: boolean;
// If this is undefined, use default behavior based on client settings
private readonly _spans: boolean | undefined;
Expand All @@ -56,6 +56,8 @@ export class NodeFetch implements Integration {
* @inheritDoc
*/
public constructor(options: NodeFetchOptions = {}) {
super(options);

this.name = NodeFetch.id;
this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
this._spans = typeof options.spans === 'undefined' ? undefined : options.spans;
Expand All @@ -64,33 +66,30 @@ export class NodeFetch implements Integration {
this.shouldCreateSpansForRequests = false;
}

/** @inheritDoc */
public setupInstrumentation(): void | Instrumentation[] {
return [
new FetchInstrumentation({
onRequest: ({ span }: { span: Span }) => {
this._updateSpan(span);
this._addRequestBreadcrumb(span);
},
}),
];
}

/**
* @inheritDoc
*/
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
// No need to instrument if we don't want to track anything
if (!this._breadcrumbs && this._spans === false) {
return;
}
public setupOnce(): void {
super.setupOnce();

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

// This is used in the sampler function
this.shouldCreateSpansForRequests =
typeof this._spans === 'boolean' ? this._spans : hasTracingEnabled(clientOptions);

// Register instrumentations we care about
this._unload = registerInstrumentations({
instrumentations: [
new FetchInstrumentation({
onRequest: ({ span }: { span: Span }) => {
this._updateSpan(span);
this._addRequestBreadcrumb(span);
},
}),
],
});
}

/**
Expand Down

0 comments on commit cadeefe

Please sign in to comment.