@@ -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
@@ -291,7 +291,8 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
291
291
emitFinish ( ) ;
292
292
}
293
293
294
- return idleSpan ;
294
+ // A trace should to stay the consistent over the entire time span of one route.
295
+ setActiveIdleSpan ( client , idleSpan ) ;
295
296
}
296
297
297
298
return {
@@ -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,8 +491,7 @@ 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 ) ;
494
+ const rootSpan = getActiveIdleSpan ( client ) ;
499
495
if ( rootSpan ) {
500
496
const currentRootSpanOp = spanToJSON ( rootSpan ) . op ;
501
497
if ( [ 'navigation' , 'pageload' ] . includes ( currentRootSpanOp as string ) ) {
@@ -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 ) : void {
543
+ addNonEnumerableProperty ( client , ACTIVE_IDLE_SPAN_PROPERTY , span ) ;
544
+ }
0 commit comments