@@ -22,6 +22,7 @@ import {
2222import { getScopesFromContext } from '@sentry/opentelemetry' ;
2323import type { VercelEdgeOptions } from '@sentry/vercel-edge' ;
2424import { getDefaultIntegrations , init as vercelEdgeInit } from '@sentry/vercel-edge' ;
25+ import { ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes' ;
2526import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached' ;
2627import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes' ;
2728import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests' ;
@@ -90,19 +91,21 @@ export function init(options: VercelEdgeOptions = {}): void {
9091 if ( spanAttributes ?. [ 'next.span_type' ] === 'Middleware.execute' ) {
9192 span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , 'http.server.middleware' ) ;
9293 span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_SOURCE , 'url' ) ;
94+ }
9395
94- if ( isRootSpan ) {
95- // Fork isolation scope for middleware requests
96- const scopes = getCapturedScopesOnSpan ( span ) ;
97- const isolationScope = ( scopes . isolationScope || getIsolationScope ( ) ) . clone ( ) ;
98- const scope = scopes . scope || getCurrentScope ( ) ;
99- const currentScopesPointer = getScopesFromContext ( context . active ( ) ) ;
100- if ( currentScopesPointer ) {
101- currentScopesPointer . isolationScope = isolationScope ;
102- }
96+ // We want to fork the isolation scope for incoming requests
97+ if ( spanAttributes ?. [ ATTR_NEXT_SPAN_TYPE ] === 'BaseServer.handleRequest' && isRootSpan ) {
98+ const scopes = getCapturedScopesOnSpan ( span ) ;
10399
104- setCapturedScopesOnSpan ( span , scope , isolationScope ) ;
100+ const isolationScope = ( scopes . isolationScope || getIsolationScope ( ) ) . clone ( ) ;
101+ const scope = scopes . scope || getCurrentScope ( ) ;
102+
103+ const currentScopesPointer = getScopesFromContext ( context . active ( ) ) ;
104+ if ( currentScopesPointer ) {
105+ currentScopesPointer . isolationScope = isolationScope ;
105106 }
107+
108+ setCapturedScopesOnSpan ( span , scope , isolationScope ) ;
106109 }
107110
108111 if ( isRootSpan ) {
0 commit comments