@@ -5,6 +5,7 @@ import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/ut
5
5
import { DEBUG_BUILD } from '../debug-build' ;
6
6
import { getCurrentScope , withScope } from '../exports' ;
7
7
import type { Hub } from '../hub' ;
8
+ import { runWithAsyncContext } from '../hub' ;
8
9
import { getIsolationScope } from '../hub' ;
9
10
import { getCurrentHub } from '../hub' ;
10
11
import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
@@ -74,31 +75,33 @@ export function trace<T>(
74
75
export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span | undefined ) => T ) : T {
75
76
const ctx = normalizeContext ( context ) ;
76
77
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 ( ) ;
82
84
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 ) ;
85
87
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
+ }
97
100
}
98
- }
99
- } ,
100
- ( ) => activeSpan && activeSpan . end ( ) ,
101
- ) ;
101
+ } ,
102
+ ( ) => activeSpan && activeSpan . end ( ) ,
103
+ ) ;
104
+ } ) ;
102
105
} ) ;
103
106
}
104
107
@@ -124,34 +127,36 @@ export function startSpanManual<T>(
124
127
) : T {
125
128
const ctx = normalizeContext ( context ) ;
126
129
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 ( ) ;
135
136
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 ) ;
138
139
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
+ }
151
156
}
152
- }
153
- } ,
154
- ) ;
157
+ } ,
158
+ ) ;
159
+ } ) ;
155
160
} ) ;
156
161
}
157
162
0 commit comments