Skip to content

Commit ae8a747

Browse files
s1gr1dmydea
authored andcommitted
Remove Http integration from Next.js
1 parent 75d288c commit ae8a747

File tree

2 files changed

+48
-67
lines changed

2 files changed

+48
-67
lines changed
Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import {
2-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
3-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
42
SPAN_STATUS_ERROR,
53
addTracingExtensions,
64
captureException,
7-
continueTrace,
5+
getActiveSpan,
6+
getIsolationScope,
7+
getRootSpan,
88
handleCallbackErrors,
99
setHttpStatus,
10-
startSpan,
1110
} from '@sentry/core';
1211
import { winterCGHeadersToDict } from '@sentry/utils';
13-
1412
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
1513
import type { RouteHandlerContext } from './types';
1614
import { platformSupportsStreaming } from './utils/platformSupportsStreaming';
@@ -26,73 +24,55 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
2624
context: RouteHandlerContext,
2725
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>> {
2826
addTracingExtensions();
29-
const { method, parameterizedRoute, headers } = context;
27+
28+
const { headers } = context;
29+
3030
return new Proxy(routeHandler, {
31-
apply: (originalFunction, thisArg, args) => {
32-
return withIsolationScopeOrReuseFromRootSpan(async isolationScope => {
33-
isolationScope.setSDKProcessingMetadata({
34-
request: {
35-
headers: headers ? winterCGHeadersToDict(headers) : undefined,
36-
},
37-
});
38-
return continueTrace(
39-
{
40-
// TODO(v8): Make it so that continue trace will allow null as sentryTrace value and remove this fallback here
41-
sentryTrace: headers?.get('sentry-trace') ?? undefined,
42-
baggage: headers?.get('baggage'),
43-
},
44-
async () => {
45-
try {
46-
return await startSpan(
47-
{
48-
op: 'http.server',
49-
name: `${method} ${parameterizedRoute}`,
50-
forceTransaction: true,
51-
attributes: {
52-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
53-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
54-
},
55-
},
56-
async span => {
57-
const response: Response = await handleCallbackErrors(
58-
() => originalFunction.apply(thisArg, args),
59-
error => {
60-
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
61-
if (isRedirectNavigationError(error)) {
62-
// Don't do anything
63-
} else if (isNotFoundNavigationError(error)) {
64-
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
65-
} else {
66-
captureException(error, {
67-
mechanism: {
68-
handled: false,
69-
},
70-
});
71-
}
72-
},
73-
);
31+
apply: async (originalFunction, thisArg, args) => {
32+
getIsolationScope().setSDKProcessingMetadata({
33+
request: {
34+
headers: headers ? winterCGHeadersToDict(headers) : undefined,
35+
},
36+
});
7437

75-
try {
76-
if (span && response.status) {
77-
setHttpStatus(span, response.status);
78-
}
79-
} catch {
80-
// best effort - response may be undefined?
81-
}
38+
try {
39+
const activeSpan = getActiveSpan();
40+
const rootSpan = activeSpan && getRootSpan(activeSpan);
8241

83-
return response;
42+
const response: Response = await handleCallbackErrors(
43+
() => originalFunction.apply(thisArg, args),
44+
error => {
45+
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
46+
if (isRedirectNavigationError(error)) {
47+
// Don't do anything
48+
} else if (isNotFoundNavigationError(error) && rootSpan) {
49+
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
50+
} else {
51+
captureException(error, {
52+
mechanism: {
53+
handled: false,
8454
},
85-
);
86-
} finally {
87-
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
88-
// 1. Edge transport requires manual flushing
89-
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
90-
await flushQueue();
91-
}
55+
});
9256
}
9357
},
9458
);
95-
});
59+
60+
try {
61+
if (rootSpan && response.status) {
62+
setHttpStatus(rootSpan, response.status);
63+
}
64+
} catch {
65+
// best effort - response may be undefined?
66+
}
67+
68+
return response;
69+
} finally {
70+
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
71+
// 1. Edge transport requires manual flushing
72+
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
73+
await flushQueue();
74+
}
75+
}
9676
},
9777
});
9878
}

packages/nextjs/src/server/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export function init(options: NodeOptions): void {
7575
...getDefaultIntegrations(options).filter(
7676
integration =>
7777
integration.name !== 'OnUncaughtException' &&
78-
// Next.js comes with its own Node-Fetch instrumentation so we shouldn't add ours on-top
79-
integration.name !== 'NodeFetch',
78+
// Next.js comes with its own Node-Fetch and Http instrumentation, so we shouldn't add ours on-top
79+
integration.name !== 'NodeFetch' &&
80+
integration.name !== 'Http',
8081
),
8182
onUncaughtExceptionIntegration(),
8283
];

0 commit comments

Comments
 (0)