@@ -2,7 +2,7 @@ import type { TransactionContext } from '@sentry/types';
2
2
import { dropUndefinedKeys , isThenable , logger , tracingContextFromHeaders } from '@sentry/utils' ;
3
3
4
4
import { DEBUG_BUILD } from '../debug-build' ;
5
- import { getCurrentScope } from '../exports' ;
5
+ import { getCurrentScope , withScope } from '../exports' ;
6
6
import type { Hub } from '../hub' ;
7
7
import { getCurrentHub } from '../hub' ;
8
8
import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
@@ -89,42 +89,42 @@ export function trace<T>(
89
89
export function startSpan < T > ( context : TransactionContext , callback : ( span : Span | undefined ) => T ) : T {
90
90
const ctx = normalizeContext ( context ) ;
91
91
92
- const hub = getCurrentHub ( ) ;
93
- const scope = getCurrentScope ( ) ;
94
- const parentSpan = scope . getSpan ( ) ;
92
+ return withScope ( scope => {
93
+ const hub = getCurrentHub ( ) ;
94
+ const parentSpan = scope . getSpan ( ) ;
95
95
96
- const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
97
- scope . setSpan ( activeSpan ) ;
98
-
99
- function finishAndSetSpan ( ) : void {
100
- activeSpan && activeSpan . end ( ) ;
101
- scope . setSpan ( parentSpan ) ;
102
- }
96
+ const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
97
+ scope . setSpan ( activeSpan ) ;
103
98
104
- let maybePromiseResult : T ;
105
- try {
106
- maybePromiseResult = callback ( activeSpan ) ;
107
- } catch ( e ) {
108
- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
109
- finishAndSetSpan ( ) ;
110
- throw e ;
111
- }
99
+ function finishAndSetSpan ( ) : void {
100
+ activeSpan && activeSpan . end ( ) ;
101
+ }
112
102
113
- if ( isThenable ( maybePromiseResult ) ) {
114
- Promise . resolve ( maybePromiseResult ) . then (
115
- ( ) => {
116
- finishAndSetSpan ( ) ;
117
- } ,
118
- ( ) => {
119
- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
120
- finishAndSetSpan ( ) ;
121
- } ,
122
- ) ;
123
- } else {
124
- finishAndSetSpan ( ) ;
125
- }
126
-
127
- return maybePromiseResult ;
103
+ let maybePromiseResult : T ;
104
+ try {
105
+ maybePromiseResult = callback ( activeSpan ) ;
106
+ } catch ( e ) {
107
+ activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
108
+ finishAndSetSpan ( ) ;
109
+ throw e ;
110
+ }
111
+
112
+ if ( isThenable ( maybePromiseResult ) ) {
113
+ Promise . resolve ( maybePromiseResult ) . then (
114
+ ( ) => {
115
+ finishAndSetSpan ( ) ;
116
+ } ,
117
+ ( ) => {
118
+ activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
119
+ finishAndSetSpan ( ) ;
120
+ } ,
121
+ ) ;
122
+ } else {
123
+ finishAndSetSpan ( ) ;
124
+ }
125
+
126
+ return maybePromiseResult ;
127
+ } ) ;
128
128
}
129
129
130
130
/**
@@ -149,33 +149,33 @@ export function startSpanManual<T>(
149
149
) : T {
150
150
const ctx = normalizeContext ( context ) ;
151
151
152
- const hub = getCurrentHub ( ) ;
153
- const scope = getCurrentScope ( ) ;
154
- const parentSpan = scope . getSpan ( ) ;
152
+ return withScope ( scope => {
153
+ const hub = getCurrentHub ( ) ;
154
+ const parentSpan = scope . getSpan ( ) ;
155
155
156
- const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
157
- scope . setSpan ( activeSpan ) ;
156
+ const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
157
+ scope . setSpan ( activeSpan ) ;
158
158
159
- function finishAndSetSpan ( ) : void {
160
- activeSpan && activeSpan . end ( ) ;
161
- scope . setSpan ( parentSpan ) ;
162
- }
159
+ function finishAndSetSpan ( ) : void {
160
+ activeSpan && activeSpan . end ( ) ;
161
+ }
163
162
164
- let maybePromiseResult : T ;
165
- try {
166
- maybePromiseResult = callback ( activeSpan , finishAndSetSpan ) ;
167
- } catch ( e ) {
168
- activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
169
- throw e ;
170
- }
171
-
172
- if ( isThenable ( maybePromiseResult ) ) {
173
- Promise . resolve ( maybePromiseResult ) . then ( undefined , ( ) => {
163
+ let maybePromiseResult : T ;
164
+ try {
165
+ maybePromiseResult = callback ( activeSpan , finishAndSetSpan ) ;
166
+ } catch ( e ) {
174
167
activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
175
- } ) ;
176
- }
168
+ throw e ;
169
+ }
177
170
178
- return maybePromiseResult ;
171
+ if ( isThenable ( maybePromiseResult ) ) {
172
+ Promise . resolve ( maybePromiseResult ) . then ( undefined , ( ) => {
173
+ activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
174
+ } ) ;
175
+ }
176
+
177
+ return maybePromiseResult ;
178
+ } ) ;
179
179
}
180
180
181
181
/**
0 commit comments