Skip to content

feat(node): Do not add HTTP & fetch span instrumentation if tracing is disabled #15730

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
May 21, 2025
Merged
Show file tree
Hide file tree
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
21 changes: 18 additions & 3 deletions packages/node/src/integrations/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { diag } from '@opentelemetry/api';
import type { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import type { Span } from '@sentry/core';
import { defineIntegration, getClient } from '@sentry/core';
import { defineIntegration, getClient, hasSpansEnabled } from '@sentry/core';
import { NODE_VERSION } from '../../nodeVersion';
import { generateInstrumentOnce } from '../../otel/instrument';
import type { NodeClient } from '../../sdk/client';
import type { HTTPModuleRequestIncomingMessage } from '../../transports/http-module';
Expand Down Expand Up @@ -159,8 +160,22 @@ export const instrumentOtelHttp = generateInstrumentOnce<HttpInstrumentationConf
/** Exported only for tests. */
export function _shouldInstrumentSpans(options: HttpOptions, clientOptions: Partial<NodeClientOptions> = {}): boolean {
// If `spans` is passed in, it takes precedence
// Else, we by default emit spans, unless `skipOpenTelemetrySetup` is set to `true`
return typeof options.spans === 'boolean' ? options.spans : !clientOptions.skipOpenTelemetrySetup;
// Else, we by default emit spans, unless `skipOpenTelemetrySetup` is set to `true` or spans are not enabled
if (typeof options.spans === 'boolean') {
return options.spans;
}

if (clientOptions.skipOpenTelemetrySetup) {
return false;
}

// IMPORTANT: We only disable span instrumentation when spans are not enabled _and_ we are on Node 22+,
// as otherwise the necessary diagnostics channel is not available yet
if (!hasSpansEnabled(clientOptions) && NODE_VERSION.major >= 22) {
return false;
}

return true;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/node/src/integrations/node-fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UndiciInstrumentationConfig } from '@opentelemetry/instrumentation-undici';
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, getClient, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { defineIntegration, getClient, hasSpansEnabled, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { generateInstrumentOnce } from '../../otel/instrument';
import type { NodeClient } from '../../sdk/client';
import type { NodeClientOptions } from '../../types';
Expand Down Expand Up @@ -86,8 +86,10 @@ function getAbsoluteUrl(origin: string, path: string = '/'): string {

function _shouldInstrumentSpans(options: NodeFetchOptions, clientOptions: Partial<NodeClientOptions> = {}): boolean {
// If `spans` is passed in, it takes precedence
// Else, we by default emit spans, unless `skipOpenTelemetrySetup` is set to `true`
return typeof options.spans === 'boolean' ? options.spans : !clientOptions.skipOpenTelemetrySetup;
// Else, we by default emit spans, unless `skipOpenTelemetrySetup` is set to `true` or spans are not enabled
return typeof options.spans === 'boolean'
? options.spans
: !clientOptions.skipOpenTelemetrySetup && hasSpansEnabled(clientOptions);
}

function getConfigWithDefaults(options: Partial<NodeFetchOptions> = {}): UndiciInstrumentationConfig {
Expand Down
15 changes: 13 additions & 2 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { describe, expect, it } from 'vitest';
import { _shouldInstrumentSpans } from '../../src/integrations/http';
import { conditionalTest } from '../helpers/conditional';

describe('httpIntegration', () => {
describe('_shouldInstrumentSpans', () => {
it.each([
[{}, {}, true],
[{ spans: true }, {}, true],
[{ spans: false }, {}, false],
[{ spans: true }, { skipOpenTelemetrySetup: true }, true],
[{ spans: false }, { skipOpenTelemetrySetup: true }, false],
[{}, { skipOpenTelemetrySetup: true }, false],
[{}, { skipOpenTelemetrySetup: false }, true],
[{}, { tracesSampleRate: 0, skipOpenTelemetrySetup: true }, false],
[{}, { tracesSampleRate: 0 }, true],
])('returns the correct value for options=%j and clientOptions=%j', (options, clientOptions, expected) => {
const actual = _shouldInstrumentSpans(options, clientOptions);
expect(actual).toBe(expected);
});

conditionalTest({ min: 22 })('returns false without tracesSampleRate on Node >=22', () => {
const actual = _shouldInstrumentSpans({}, {});
expect(actual).toBe(false);
});

conditionalTest({ max: 21 })('returns true without tracesSampleRate on Node <22', () => {
const actual = _shouldInstrumentSpans({}, {});
expect(actual).toBe(true);
});
});
});
Loading