1- import type { Span , SpanTimeInput , StartSpanOptions , TransactionContext } from '@sentry/types' ;
1+ import type { Scope , Span , SpanTimeInput , StartSpanOptions , TransactionContext } from '@sentry/types' ;
22
3- import { dropUndefinedKeys , logger , tracingContextFromHeaders } from '@sentry/utils' ;
3+ import { addNonEnumerableProperty , dropUndefinedKeys , logger , tracingContextFromHeaders } from '@sentry/utils' ;
44
55import { DEBUG_BUILD } from '../debug-build' ;
66import { getCurrentScope , withScope } from '../exports' ;
@@ -189,20 +189,22 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
189189 return undefined ;
190190 }
191191
192+ const isolationScope = getIsolationScope ( ) ;
193+ const scope = getCurrentScope ( ) ;
194+
195+ let span : Span | undefined ;
196+
192197 if ( parentSpan ) {
193198 // eslint-disable-next-line deprecation/deprecation
194- return parentSpan . startChild ( ctx ) ;
199+ span = parentSpan . startChild ( ctx ) ;
195200 } else {
196- const isolationScope = getIsolationScope ( ) ;
197- const scope = getCurrentScope ( ) ;
198-
199201 const { traceId, dsc, parentSpanId, sampled } = {
200202 ...isolationScope . getPropagationContext ( ) ,
201203 ...scope . getPropagationContext ( ) ,
202204 } ;
203205
204206 // eslint-disable-next-line deprecation/deprecation
205- return hub . startTransaction ( {
207+ span = hub . startTransaction ( {
206208 traceId,
207209 parentSpanId,
208210 parentSampled : sampled ,
@@ -214,6 +216,10 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
214216 } ,
215217 } ) ;
216218 }
219+
220+ setCapturedScopesOnSpan ( span , scope , isolationScope ) ;
221+
222+ return span ;
217223}
218224
219225/**
@@ -335,20 +341,21 @@ function createChildSpanOrTransaction(
335341 return undefined ;
336342 }
337343
344+ const isolationScope = getIsolationScope ( ) ;
345+ const scope = getCurrentScope ( ) ;
346+
347+ let span : Span | undefined ;
338348 if ( parentSpan ) {
339349 // eslint-disable-next-line deprecation/deprecation
340- return parentSpan . startChild ( ctx ) ;
350+ span = parentSpan . startChild ( ctx ) ;
341351 } else {
342- const isolationScope = getIsolationScope ( ) ;
343- const scope = getCurrentScope ( ) ;
344-
345352 const { traceId, dsc, parentSpanId, sampled } = {
346353 ...isolationScope . getPropagationContext ( ) ,
347354 ...scope . getPropagationContext ( ) ,
348355 } ;
349356
350357 // eslint-disable-next-line deprecation/deprecation
351- return hub . startTransaction ( {
358+ span = hub . startTransaction ( {
352359 traceId,
353360 parentSpanId,
354361 parentSampled : sampled ,
@@ -360,6 +367,10 @@ function createChildSpanOrTransaction(
360367 } ,
361368 } ) ;
362369 }
370+
371+ setCapturedScopesOnSpan ( span , scope , isolationScope ) ;
372+
373+ return span ;
363374}
364375
365376/**
@@ -379,3 +390,28 @@ function normalizeContext(context: StartSpanOptions): TransactionContext {
379390
380391 return context ;
381392}
393+
394+ const SCOPE_ON_START_SPAN_FIELD = '_sentryScope' ;
395+ const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope' ;
396+
397+ type SpanWithScopes = Span & {
398+ [ SCOPE_ON_START_SPAN_FIELD ] ?: Scope ;
399+ [ ISOLATION_SCOPE_ON_START_SPAN_FIELD ] ?: Scope ;
400+ } ;
401+
402+ function setCapturedScopesOnSpan ( span : Span | undefined , scope : Scope , isolationScope : Scope ) : void {
403+ if ( span ) {
404+ addNonEnumerableProperty ( span , ISOLATION_SCOPE_ON_START_SPAN_FIELD , isolationScope ) ;
405+ addNonEnumerableProperty ( span , SCOPE_ON_START_SPAN_FIELD , scope ) ;
406+ }
407+ }
408+
409+ /**
410+ * Grabs the scope and isolation scope off a span that were active when the span was started.
411+ */
412+ export function getCapturedScopesOnSpan ( span : Span ) : { scope ?: Scope ; isolationScope ?: Scope } {
413+ return {
414+ scope : ( span as SpanWithScopes ) [ SCOPE_ON_START_SPAN_FIELD ] ,
415+ isolationScope : ( span as SpanWithScopes ) [ ISOLATION_SCOPE_ON_START_SPAN_FIELD ] ,
416+ } ;
417+ }
0 commit comments