@@ -5,6 +5,7 @@ import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/ut
55import { DEBUG_BUILD } from '../debug-build' ;
66import { getCurrentScope , withScope } from '../exports' ;
77import type { Hub } from '../hub' ;
8+ import { runWithAsyncContext } from '../hub' ;
89import { getIsolationScope } from '../hub' ;
910import { getCurrentHub } from '../hub' ;
1011import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
@@ -74,31 +75,33 @@ export function trace<T>(
7475export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span | undefined ) => T ) : T {
7576 const ctx = normalizeContext ( context ) ;
7677
77- return withScope ( context . scope , scope => {
78- // eslint-disable-next-line deprecation/deprecation
79- const hub = getCurrentHub ( ) ;
80- // eslint-disable-next-line deprecation/deprecation
81- const parentSpan = scope . getSpan ( ) ;
78+ return runWithAsyncContext ( ( ) => {
79+ return withScope ( context . scope , scope => {
80+ // eslint-disable-next-line deprecation/deprecation
81+ const hub = getCurrentHub ( ) ;
82+ // eslint-disable-next-line deprecation/deprecation
83+ const parentSpan = scope . getSpan ( ) ;
8284
83- const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
84- const activeSpan = shouldSkipSpan ? undefined : createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
85+ const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
86+ const activeSpan = shouldSkipSpan ? undefined : createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
8587
86- // eslint-disable-next-line deprecation/deprecation
87- scope . setSpan ( activeSpan ) ;
88-
89- return handleCallbackErrors (
90- ( ) => callback ( activeSpan ) ,
91- ( ) => {
92- // Only update the span status if it hasn't been changed yet
93- if ( activeSpan ) {
94- const { status } = spanToJSON ( activeSpan ) ;
95- if ( ! status || status === 'ok' ) {
96- activeSpan . setStatus ( 'internal_error' ) ;
88+ // eslint-disable-next-line deprecation/deprecation
89+ scope . setSpan ( activeSpan ) ;
90+
91+ return handleCallbackErrors (
92+ ( ) => callback ( activeSpan ) ,
93+ ( ) => {
94+ // Only update the span status if it hasn't been changed yet
95+ if ( activeSpan ) {
96+ const { status } = spanToJSON ( activeSpan ) ;
97+ if ( ! status || status === 'ok' ) {
98+ activeSpan . setStatus ( 'internal_error' ) ;
99+ }
97100 }
98- }
99- } ,
100- ( ) => activeSpan && activeSpan . end ( ) ,
101- ) ;
101+ } ,
102+ ( ) => activeSpan && activeSpan . end ( ) ,
103+ ) ;
104+ } ) ;
102105 } ) ;
103106}
104107
@@ -124,34 +127,36 @@ export function startSpanManual<T>(
124127) : T {
125128 const ctx = normalizeContext ( context ) ;
126129
127- return withScope ( context . scope , scope => {
128- // eslint-disable-next-line deprecation/deprecation
129- const hub = getCurrentHub ( ) ;
130- // eslint-disable-next-line deprecation/deprecation
131- const parentSpan = scope . getSpan ( ) ;
132-
133- const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
134- const activeSpan = shouldSkipSpan ? undefined : createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
130+ return runWithAsyncContext ( ( ) => {
131+ return withScope ( context . scope , scope => {
132+ // eslint-disable-next-line deprecation/deprecation
133+ const hub = getCurrentHub ( ) ;
134+ // eslint-disable-next-line deprecation/deprecation
135+ const parentSpan = scope . getSpan ( ) ;
135136
136- // eslint-disable-next-line deprecation/deprecation
137- scope . setSpan ( activeSpan ) ;
137+ const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
138+ const activeSpan = shouldSkipSpan ? undefined : createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
138139
139- function finishAndSetSpan ( ) : void {
140- activeSpan && activeSpan . end ( ) ;
141- }
142-
143- return handleCallbackErrors (
144- ( ) => callback ( activeSpan , finishAndSetSpan ) ,
145- ( ) => {
146- // Only update the span status if it hasn't been changed yet, and the span is not yet finished
147- if ( activeSpan && activeSpan . isRecording ( ) ) {
148- const { status } = spanToJSON ( activeSpan ) ;
149- if ( ! status || status === 'ok' ) {
150- activeSpan . setStatus ( 'internal_error' ) ;
140+ // eslint-disable-next-line deprecation/deprecation
141+ scope . setSpan ( activeSpan ) ;
142+
143+ function finishAndSetSpan ( ) : void {
144+ activeSpan && activeSpan . end ( ) ;
145+ }
146+
147+ return handleCallbackErrors (
148+ ( ) => callback ( activeSpan , finishAndSetSpan ) ,
149+ ( ) => {
150+ // Only update the span status if it hasn't been changed yet, and the span is not yet finished
151+ if ( activeSpan && activeSpan . isRecording ( ) ) {
152+ const { status } = spanToJSON ( activeSpan ) ;
153+ if ( ! status || status === 'ok' ) {
154+ activeSpan . setStatus ( 'internal_error' ) ;
155+ }
151156 }
152- }
153- } ,
154- ) ;
157+ } ,
158+ ) ;
159+ } ) ;
155160 } ) ;
156161}
157162
0 commit comments