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