@@ -2,7 +2,7 @@ import type { TransactionContext } from '@sentry/types';
22import { dropUndefinedKeys , isThenable , logger , tracingContextFromHeaders } from '@sentry/utils' ;
33
44import { DEBUG_BUILD } from '../debug-build' ;
5- import { getCurrentScope } from '../exports' ;
5+ import { getCurrentScope , withScope } from '../exports' ;
66import type { Hub } from '../hub' ;
77import { getCurrentHub } from '../hub' ;
88import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
@@ -89,42 +89,42 @@ export function trace<T>(
8989export function startSpan < T > ( context : TransactionContext , callback : ( span : Span | undefined ) => T ) : T {
9090 const ctx = normalizeContext ( context ) ;
9191
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 ( ) ;
9595
96- const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
97- scope . setSpan ( activeSpan ) ;
98-
99- function finishAndSetSpan ( ) : void {
100- activeSpan && activeSpan . finish ( ) ;
101- scope . setSpan ( parentSpan ) ;
102- }
96+ const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
97+ scope . setSpan ( activeSpan ) ;
10398
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 . finish ( ) ;
101+ }
112102
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+ } ) ;
128128}
129129
130130/**
@@ -149,33 +149,33 @@ export function startSpanManual<T>(
149149) : T {
150150 const ctx = normalizeContext ( context ) ;
151151
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 ( ) ;
155155
156- const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
157- scope . setSpan ( activeSpan ) ;
156+ const activeSpan = createChildSpanOrTransaction ( hub , parentSpan , ctx ) ;
157+ scope . setSpan ( activeSpan ) ;
158158
159- function finishAndSetSpan ( ) : void {
160- activeSpan && activeSpan . finish ( ) ;
161- scope . setSpan ( parentSpan ) ;
162- }
159+ function finishAndSetSpan ( ) : void {
160+ activeSpan && activeSpan . finish ( ) ;
161+ }
163162
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 ) {
174167 activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
175- } ) ;
176- }
168+ throw e ;
169+ }
177170
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+ } ) ;
179179}
180180
181181/**
0 commit comments