Skip to content

fix(node-experimental): Guard against missing fetch #9275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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