Skip to content

Commit d80c681

Browse files
committed
use event processor instead, bandaid!
1 parent 3cab755 commit d80c681

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/route-handlers.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ test.describe('Edge runtime', () => {
172172
expect(edgerouteTransaction2.spans?.length).toBe(1);
173173
expect(edgerouteTransaction3.spans?.length).toBe(1);
174174

175-
expect(edgerouteTransaction1.spans?.[0].description).toBe('GET https://github.com');
176-
expect(edgerouteTransaction2.spans?.[0].description).toBe('GET https://github.com');
177-
expect(edgerouteTransaction3.spans?.[0].description).toBe('GET https://github.com');
175+
expect(edgerouteTransaction1.spans?.[0].description).toBe('GET https://github.com/');
176+
expect(edgerouteTransaction2.spans?.[0].description).toBe('GET https://github.com/');
177+
expect(edgerouteTransaction3.spans?.[0].description).toBe('GET https://github.com/');
178178
});
179179
});
180180

packages/nextjs/src/server/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SEMATTRS_HTTP_METHOD,
77
SEMATTRS_HTTP_TARGET,
88
} from '@opentelemetry/semantic-conventions';
9-
import type { EventProcessor } from '@sentry/core';
9+
import { EventProcessor, isSentryRequestUrl } from '@sentry/core';
1010
import {
1111
applySdkMetadata,
1212
extractTraceparentData,
@@ -203,7 +203,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
203203
}
204204
});
205205

206-
getGlobalScope().addEventProcessor(
206+
client?.addEventProcessor(
207207
Object.assign(
208208
(event => {
209209
if (event.type === 'transaction') {
@@ -260,6 +260,16 @@ export function init(options: NodeOptions): NodeClient | undefined {
260260
}
261261
}
262262

263+
// On Edge Runtime, fetch requests can leak to the Node runtime fetch instrumentation
264+
// and end up being sent as transactions to Sentry.
265+
// We filter them here based on URL
266+
if (event.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'http.client') {
267+
const url = event.contexts?.trace?.data?.['url'];
268+
if (url && isSentryRequestUrl(url, client)) {
269+
return null;
270+
}
271+
}
272+
263273
return event;
264274
} else {
265275
return event;
@@ -269,7 +279,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
269279
),
270280
);
271281

272-
getGlobalScope().addEventProcessor(
282+
client?.addEventProcessor(
273283
Object.assign(
274284
((event, hint) => {
275285
if (event.type !== undefined) {
@@ -364,7 +374,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
364374
});
365375

366376
if (process.env.NODE_ENV === 'development') {
367-
getGlobalScope().addEventProcessor(devErrorSymbolicationEventProcessor);
377+
client?.addEventProcessor(devErrorSymbolicationEventProcessor);
368378
}
369379

370380
try {

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { context } from '@opentelemetry/api';
22
import { isTracingSuppressed, VERSION } from '@opentelemetry/core';
33
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
44
import { InstrumentationBase } from '@opentelemetry/instrumentation';
5-
import { isSentryRequestUrl, SanitizedRequestData } from '@sentry/core';
6-
import {
7-
addBreadcrumb,
5+
import type {
6+
SanitizedRequestData ,
7+
} from '@sentry/core';
8+
import { addBreadcrumb,
89
getBreadcrumbLogLevelFromHttpStatusCode,
910
getClient,
1011
getSanitizedUrlString,
1112
getTraceData,
1213
LRUMap,
13-
parseUrl,
14-
} from '@sentry/core';
14+
parseUrl} from '@sentry/core';
1515
import { shouldPropagateTraceForUrl } from '@sentry/opentelemetry';
1616
import * as diagch from 'diagnostics_channel';
1717
import { NODE_MAJOR, NODE_MINOR } from '../../nodeVersion';
@@ -246,13 +246,6 @@ export class SentryNodeFetchInstrumentation extends InstrumentationBase<SentryNo
246246
const url = getAbsoluteUrl(request.origin, request.path);
247247
const ignoreOutgoingRequests = this.getConfig().ignoreOutgoingRequests;
248248

249-
// Normally, we should not need this, because `suppressTracing` should take care of this
250-
// However, in Next.js Edge Runtime in dev, there is a bug where the edge is simulated but still uses Node under the hood, leading to problems
251-
// So we make sure to ignore outgoing requests to Sentry endpoints
252-
if (isSentryRequestUrl(url, getClient())) {
253-
return true;
254-
}
255-
256249
if (typeof ignoreOutgoingRequests !== 'function' || !url) {
257250
return false;
258251
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { UndiciInstrumentationConfig } from '@opentelemetry/instrumentation-undici';
22
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
3-
import { IntegrationFn, isSentryRequestUrl } from '@sentry/core';
3+
import type { IntegrationFn } from '@sentry/core';
44
import { defineIntegration, getClient, hasSpansEnabled, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
55
import { generateInstrumentOnce } from '../../otel/instrument';
66
import type { NodeClient } from '../../sdk/client';
77
import type { NodeClientOptions } from '../../types';
88
import { SentryNodeFetchInstrumentation } from './SentryNodeFetchInstrumentation';
9-
import { isNextEdgeRuntime } from '../../utils/isNextEdgeRuntime';
109

1110
const INTEGRATION_NAME = 'NodeFetch';
1211

@@ -101,13 +100,6 @@ function getConfigWithDefaults(options: Partial<NodeFetchOptions> = {}): UndiciI
101100
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;
102101
const shouldIgnore = _ignoreOutgoingRequests && url && _ignoreOutgoingRequests(url);
103102

104-
// Normally, we should not need this, because `suppressTracing` should take care of this
105-
// However, in Next.js Edge Runtime in dev, there is a bug where the edge is simulated but still uses Node under the hood, leading to problems
106-
// So we make sure to ignore outgoing requests to Sentry endpoints
107-
if (isSentryRequestUrl(url, getClient())) {
108-
return true;
109-
}
110-
111103
return !!shouldIgnore;
112104
},
113105
startSpanHook: () => {

0 commit comments

Comments
 (0)