@@ -16,9 +16,9 @@ import {
16
16
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
17
17
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
18
18
TRACING_DEFAULTS ,
19
+ addNonEnumerableProperty ,
19
20
browserPerformanceTimeOrigin ,
20
21
generateTraceId ,
21
- getActiveSpan ,
22
22
getClient ,
23
23
getCurrentScope ,
24
24
getDynamicSamplingContextFromSpan ,
@@ -246,7 +246,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
246
246
} ;
247
247
248
248
/** Create routing idle transaction. */
249
- function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : Span {
249
+ function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
250
250
const isPageloadTransaction = startSpanOptions . op === 'pageload' ;
251
251
252
252
const finalStartSpanOptions : StartSpanOptions = beforeStartSpan
@@ -274,8 +274,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
274
274
beforeSpanEnd : span => {
275
275
_collectWebVitals ( ) ;
276
276
addPerformanceEntries ( span , { recordClsOnPageloadSpan : ! enableStandaloneClsSpans } ) ;
277
+ setActiveIdleSpan ( client , undefined ) ;
277
278
} ,
278
279
} ) ;
280
+ setActiveIdleSpan ( client , idleSpan ) ;
279
281
280
282
function emitFinish ( ) : void {
281
283
if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
@@ -290,17 +292,16 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
290
292
291
293
emitFinish ( ) ;
292
294
}
293
-
294
- return idleSpan ;
295
295
}
296
296
297
297
return {
298
298
name : BROWSER_TRACING_INTEGRATION_ID ,
299
299
afterAllSetup ( client ) {
300
- let activeSpan : Span | undefined ;
301
300
let startingUrl : string | undefined = WINDOW . location && WINDOW . location . href ;
302
301
303
302
function maybeEndActiveSpan ( ) : void {
303
+ const activeSpan = getActiveIdleSpan ( client ) ;
304
+
304
305
if ( activeSpan && ! spanToJSON ( activeSpan ) . timestamp ) {
305
306
DEBUG_BUILD && logger . log ( `[Tracing] Finishing current active span with op: ${ spanToJSON ( activeSpan ) . op } ` ) ;
306
307
// If there's an open active span, we need to finish it before creating an new one.
@@ -315,7 +316,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
315
316
316
317
maybeEndActiveSpan ( ) ;
317
318
318
- activeSpan = _createRouteSpan ( client , {
319
+ _createRouteSpan ( client , {
319
320
op : 'navigation' ,
320
321
...startSpanOptions ,
321
322
} ) ;
@@ -333,7 +334,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
333
334
const propagationContext = propagationContextFromHeaders ( sentryTrace , baggage ) ;
334
335
getCurrentScope ( ) . setPropagationContext ( propagationContext ) ;
335
336
336
- activeSpan = _createRouteSpan ( client , {
337
+ _createRouteSpan ( client , {
337
338
op : 'pageload' ,
338
339
...startSpanOptions ,
339
340
} ) ;
@@ -408,7 +409,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
408
409
}
409
410
410
411
if ( enableInteractions ) {
411
- registerInteractionListener ( idleTimeout , finalTimeout , childSpanTimeout , latestRoute ) ;
412
+ registerInteractionListener ( client , idleTimeout , finalTimeout , childSpanTimeout , latestRoute ) ;
412
413
}
413
414
414
415
if ( enableInp ) {
@@ -440,12 +441,9 @@ export function startBrowserTracingPageLoadSpan(
440
441
traceOptions ?: { sentryTrace ?: string | undefined ; baggage ?: string | undefined } ,
441
442
) : Span | undefined {
442
443
client . emit ( 'startPageLoadSpan' , spanOptions , traceOptions ) ;
443
-
444
444
getCurrentScope ( ) . setTransactionName ( spanOptions . name ) ;
445
445
446
- const span = getActiveSpan ( ) ;
447
- const op = span && spanToJSON ( span ) . op ;
448
- return op === 'pageload' ? span : undefined ;
446
+ return getActiveIdleSpan ( client ) ;
449
447
}
450
448
451
449
/**
@@ -460,9 +458,7 @@ export function startBrowserTracingNavigationSpan(client: Client, spanOptions: S
460
458
461
459
getCurrentScope ( ) . setTransactionName ( spanOptions . name ) ;
462
460
463
- const span = getActiveSpan ( ) ;
464
- const op = span && spanToJSON ( span ) . op ;
465
- return op === 'navigation' ? span : undefined ;
461
+ return getActiveIdleSpan ( client ) ;
466
462
}
467
463
468
464
/** Returns the value of a meta tag */
@@ -479,6 +475,7 @@ export function getMetaContent(metaName: string): string | undefined {
479
475
480
476
/** Start listener for interaction transactions */
481
477
function registerInteractionListener (
478
+ client : Client ,
482
479
idleTimeout : BrowserTracingOptions [ 'idleTimeout' ] ,
483
480
finalTimeout : BrowserTracingOptions [ 'finalTimeout' ] ,
484
481
childSpanTimeout : BrowserTracingOptions [ 'childSpanTimeout' ] ,
@@ -494,10 +491,9 @@ function registerInteractionListener(
494
491
const registerInteractionTransaction = ( ) : void => {
495
492
const op = 'ui.action.click' ;
496
493
497
- const activeSpan = getActiveSpan ( ) ;
498
- const rootSpan = activeSpan && getRootSpan ( activeSpan ) ;
499
- if ( rootSpan ) {
500
- const currentRootSpanOp = spanToJSON ( rootSpan ) . op ;
494
+ const activeIdleSpan = getActiveIdleSpan ( client ) ;
495
+ if ( activeIdleSpan ) {
496
+ const currentRootSpanOp = spanToJSON ( activeIdleSpan ) . op ;
501
497
if ( [ 'navigation' , 'pageload' ] . includes ( currentRootSpanOp as string ) ) {
502
498
DEBUG_BUILD &&
503
499
logger . warn ( `[Tracing] Did not create ${ op } span because a pageload or navigation span is in progress.` ) ;
@@ -536,3 +532,13 @@ function registerInteractionListener(
536
532
addEventListener ( 'click' , registerInteractionTransaction , { once : false , capture : true } ) ;
537
533
}
538
534
}
535
+
536
+ // We store the active idle span on the client object, so we can access it from exported functions
537
+ const ACTIVE_IDLE_SPAN_PROPERTY = '_sentry_idleSpan' ;
538
+ function getActiveIdleSpan ( client : Client ) : Span | undefined {
539
+ return ( client as { [ ACTIVE_IDLE_SPAN_PROPERTY ] ?: Span } ) [ ACTIVE_IDLE_SPAN_PROPERTY ] ;
540
+ }
541
+
542
+ function setActiveIdleSpan ( client : Client , span : Span | undefined ) : void {
543
+ addNonEnumerableProperty ( client , ACTIVE_IDLE_SPAN_PROPERTY , span ) ;
544
+ }
0 commit comments