Skip to content

Commit ee058e6

Browse files
committed
fix(node): Ensure tracing is suppressed for all generated requests
This exports a new `suppressTracing` utility from `@sentry/opentelemetry` which can be used to wrap a callback to avoid tracing inside of it. We use this for bun transport, and for nextjs error symbolication.
1 parent 4759d4c commit ee058e6

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

packages/bun/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@sentry/core": "8.0.0-alpha.9",
4646
"@sentry/node": "8.0.0-alpha.9",
47+
"@sentry/opentelemetry": "8.0.0-alpha.9",
4748
"@sentry/types": "8.0.0-alpha.9",
4849
"@sentry/utils": "8.0.0-alpha.9"
4950
},

packages/bun/src/transports/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createTransport } from '@sentry/core';
2+
import { suppressTracing } from '@sentry/opentelemetry';
23
import type { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
34
import { rejectedSyncPromise } from '@sentry/utils';
45

@@ -19,14 +20,16 @@ export function makeFetchTransport(options: BunTransportOptions): Transport {
1920
};
2021

2122
try {
22-
return fetch(options.url, requestOptions).then(response => {
23-
return {
24-
statusCode: response.status,
25-
headers: {
26-
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
27-
'retry-after': response.headers.get('Retry-After'),
28-
},
29-
};
23+
return suppressTracing(() => {
24+
return fetch(options.url, requestOptions).then(response => {
25+
return {
26+
statusCode: response.status,
27+
headers: {
28+
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
29+
'retry-after': response.headers.get('Retry-After'),
30+
},
31+
};
32+
});
3033
});
3134
} catch (e) {
3235
return rejectedSyncPromise(e);

packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { suppressTracing } from '@sentry/opentelemetry';
12
import type { Event, EventHint } from '@sentry/types';
23
import { GLOBAL_OBJ } from '@sentry/utils';
34
import type { StackFrame } from 'stacktrace-parser';
@@ -40,17 +41,19 @@ async function resolveStackFrame(
4041

4142
const controller = new AbortController();
4243
const timer = setTimeout(() => controller.abort(), 3000);
43-
const res = await fetch(
44-
`${
45-
// eslint-disable-next-line no-restricted-globals
46-
typeof window === 'undefined' ? 'http://localhost:3000' : '' // TODO: handle the case where users define a different port
47-
}${basePath}/__nextjs_original-stack-frame?${params.toString()}`,
48-
{
49-
signal: controller.signal,
50-
},
51-
).finally(() => {
52-
clearTimeout(timer);
53-
});
44+
const res = await suppressTracing(() =>
45+
fetch(
46+
`${
47+
// eslint-disable-next-line no-restricted-globals
48+
typeof window === 'undefined' ? 'http://localhost:3000' : '' // TODO: handle the case where users define a different port
49+
}${basePath}/__nextjs_original-stack-frame?${params.toString()}`,
50+
{
51+
signal: controller.signal,
52+
},
53+
).finally(() => {
54+
clearTimeout(timer);
55+
}),
56+
);
5457

5558
if (!res.ok || res.status === 204) {
5659
return null;

packages/node/src/transports/http.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as http from 'node:http';
22
import * as https from 'node:https';
33
import { Readable } from 'stream';
44
import { createGzip } from 'zlib';
5-
import { context } from '@opentelemetry/api';
6-
import { suppressTracing } from '@opentelemetry/core';
75
import { createTransport } from '@sentry/core';
6+
import { suppressTracing } from '@sentry/opentelemetry';
87
import type {
98
BaseTransportOptions,
109
Transport,
@@ -82,7 +81,7 @@ export function makeNodeTransport(options: NodeTransportOptions): Transport {
8281
: new nativeHttpModule.Agent({ keepAlive, maxSockets: 30, timeout: 2000 });
8382

8483
// This ensures we do not generate any spans in OpenTelemetry for the transport
85-
return context.with(suppressTracing(context.active()), () => {
84+
return suppressTracing(() => {
8685
const requestExecutor = createRequestExecutor(options, options.httpModule ?? nativeHttpModule, agent);
8786
return createTransport(options, requestExecutor);
8887
});

packages/opentelemetry/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export { isSentryRequestSpan } from './utils/isSentryRequest';
2323
export { getActiveSpan } from './utils/getActiveSpan';
2424
export { startSpan, startSpanManual, startInactiveSpan, withActiveSpan, continueTrace } from './trace';
2525

26+
export { suppressTracing } from './utils/suppressTracing';
27+
2628
// eslint-disable-next-line deprecation/deprecation
2729
export { setupGlobalHub } from './custom/hub';
2830
// eslint-disable-next-line deprecation/deprecation
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { context } from '@opentelemetry/api';
2+
import { suppressTracing as suppressTracingImpl } from '@opentelemetry/core';
3+
4+
/** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
5+
export function suppressTracing<T>(callback: () => T): T {
6+
const ctx = suppressTracingImpl(context.active());
7+
return context.with(ctx, callback);
8+
}

0 commit comments

Comments
 (0)