@@ -3,22 +3,28 @@ import {
3
3
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
4
4
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
5
5
applySdkMetadata ,
6
+ getCapturedScopesOnSpan ,
6
7
getClient ,
8
+ getCurrentScope ,
7
9
getGlobalScope ,
10
+ getIsolationScope ,
8
11
getRootSpan ,
12
+ setCapturedScopesOnSpan ,
9
13
spanToJSON ,
10
14
} from '@sentry/core' ;
11
15
import { getDefaultIntegrations , httpIntegration , init as nodeInit } from '@sentry/node' ;
12
16
import type { NodeClient , NodeOptions } from '@sentry/node' ;
13
17
import { GLOBAL_OBJ , extractTraceparentData , logger , stripUrlQueryAndFragment } from '@sentry/utils' ;
14
18
19
+ import { context } from '@opentelemetry/api' ;
15
20
import {
16
21
ATTR_HTTP_REQUEST_METHOD ,
17
22
ATTR_HTTP_ROUTE ,
18
23
ATTR_URL_QUERY ,
19
24
SEMATTRS_HTTP_METHOD ,
20
25
SEMATTRS_HTTP_TARGET ,
21
26
} from '@opentelemetry/semantic-conventions' ;
27
+ import { getScopesFromContext } from '@sentry/opentelemetry' ;
22
28
import type { EventProcessor } from '@sentry/types' ;
23
29
import { DEBUG_BUILD } from '../common/debug-build' ;
24
30
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor' ;
@@ -221,6 +227,21 @@ export function init(options: NodeOptions): NodeClient | undefined {
221
227
span . updateName ( 'next server handler' ) ; // This is all lowercase because the spans that Next.js emits by itself generally look like this.
222
228
}
223
229
}
230
+
231
+ // We want to fork the isolation scope for incoming requests
232
+ if ( spanAttributes ?. [ 'next.span_type' ] === 'BaseServer.handleRequest' && span === getRootSpan ( span ) ) {
233
+ const scopes = getCapturedScopesOnSpan ( span ) ;
234
+
235
+ const isolationScope = ( scopes . isolationScope || getIsolationScope ( ) ) . clone ( ) ;
236
+ const scope = scopes . scope || getCurrentScope ( ) ;
237
+
238
+ const currentScopesPointer = getScopesFromContext ( context . active ( ) ) ;
239
+ if ( currentScopesPointer ) {
240
+ currentScopesPointer . isolationScope = isolationScope ;
241
+ }
242
+
243
+ setCapturedScopesOnSpan ( span , scope , isolationScope ) ;
244
+ }
224
245
} ) ;
225
246
226
247
getGlobalScope ( ) . addEventProcessor (
0 commit comments