11import { 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