Skip to content

Commit 364119c

Browse files
authored
fix nextjs PR (#11450)
Just trying some things...
1 parent 786b4cb commit 364119c

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
handleCallbackErrors,
1111
setHttpStatus,
1212
startSpan,
13-
withIsolationScope,
1413
} from '@sentry/core';
1514
import type { Span } from '@sentry/types';
1615
import { winterCGHeadersToDict } from '@sentry/utils';
@@ -22,10 +21,7 @@ import { withIsolationScopeOrReuseFromRootSpan } from './utils/withIsolationScop
2221

2322
/** As our own HTTP integration is disabled (src/server/index.ts) the rootSpan comes from Next.js.
2423
* In case there is not root span, we start a new span. */
25-
function startOrUpdateSpan(
26-
spanName: string,
27-
handleResponseErrors: (rootSpan: Span) => Promise<Response>,
28-
): Promise<Response> {
24+
function startOrUpdateSpan(spanName: string, cb: (rootSpan: Span) => Promise<Response>): Promise<Response> {
2925
const activeSpan = getActiveSpan();
3026
const rootSpan = activeSpan && getRootSpan(activeSpan);
3127

@@ -35,7 +31,7 @@ function startOrUpdateSpan(
3531
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server');
3632
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.function.nextjs');
3733

38-
return handleResponseErrors(rootSpan);
34+
return cb(rootSpan);
3935
} else {
4036
return startSpan(
4137
{
@@ -48,7 +44,7 @@ function startOrUpdateSpan(
4844
},
4945
},
5046
(span: Span) => {
51-
return handleResponseErrors(span);
47+
return cb(span);
5248
},
5349
);
5450
}
@@ -68,7 +64,7 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
6864

6965
return new Proxy(routeHandler, {
7066
apply: (originalFunction, thisArg, args) => {
71-
return withIsolationScope(async isolationScope => {
67+
return withIsolationScopeOrReuseFromRootSpan(async isolationScope => {
7268
isolationScope.setSDKProcessingMetadata({
7369
request: {
7470
headers: headers ? winterCGHeadersToDict(headers) : undefined,

packages/nextjs/src/server/requestIsolationScopeIntegration.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { SpanKind } from '@opentelemetry/api';
2-
import { defineIntegration, spanToJSON } from '@sentry/core';
3-
import { getSpanKind, getSpanScopes } from '@sentry/opentelemetry';
2+
import {
3+
defineIntegration,
4+
getCapturedScopesOnSpan,
5+
getCurrentScope,
6+
getIsolationScope,
7+
setCapturedScopesOnSpan,
8+
spanToJSON,
9+
} from '@sentry/core';
10+
import { getSpanKind } from '@sentry/opentelemetry';
411

512
/**
613
* This integration is responsible for creating isolation scopes for incoming Http requests.
@@ -15,13 +22,17 @@ export const requestIsolationScopeIntegration = defineIntegration(() => {
1522
setup(client) {
1623
client.on('spanStart', span => {
1724
const spanJson = spanToJSON(span);
25+
const data = spanJson.data || {};
1826

1927
// The following check is a heuristic to determine whether the started span is a span that tracks an incoming HTTP request
20-
if (getSpanKind(span) === SpanKind.SERVER && spanJson.data && 'http.method' in spanJson.data) {
21-
const scopes = getSpanScopes(span);
22-
if (scopes) {
23-
scopes.isolationScope = scopes.isolationScope.clone();
24-
}
28+
if ((getSpanKind(span) === SpanKind.SERVER && data['http.method']) || data['next.route']) {
29+
const scopes = getCapturedScopesOnSpan(span);
30+
31+
// Update the isolation scope, isolate this request
32+
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
33+
const scope = scopes.scope || getCurrentScope();
34+
35+
setCapturedScopesOnSpan(span, scope, isolationScope);
2536
}
2637
});
2738
},

0 commit comments

Comments
 (0)