@@ -24,6 +24,7 @@ import {
24
24
getDynamicSamplingContextFromSpan ,
25
25
getIsolationScope ,
26
26
getLocationHref ,
27
+ getRootSpan ,
27
28
logger ,
28
29
propagationContextFromHeaders ,
29
30
registerSpanErrorInstrumentation ,
@@ -35,7 +36,6 @@ import { DEBUG_BUILD } from '../debug-build';
35
36
import { WINDOW } from '../helpers' ;
36
37
import { registerBackgroundTabDetection } from './backgroundtab' ;
37
38
import { defaultRequestInstrumentationOptions , instrumentOutgoingRequests } from './request' ;
38
- import type { PreviousTraceInfo } from './previousTrace' ;
39
39
import {
40
40
addPreviousTraceSpanLink ,
41
41
getPreviousTraceFromSessionStorage ,
@@ -274,19 +274,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
274
274
source : undefined ,
275
275
} ;
276
276
277
- let previousTraceInfo : PreviousTraceInfo | undefined ;
278
- if ( enablePreviousTrace && persistPreviousTrace ) {
279
- previousTraceInfo = getPreviousTraceFromSessionStorage ( ) ;
280
- }
281
-
282
277
/** Create routing idle transaction. */
283
278
function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
284
279
const isPageloadTransaction = startSpanOptions . op === 'pageload' ;
285
280
286
- if ( enablePreviousTrace && previousTraceInfo ) {
287
- previousTraceInfo = addPreviousTraceSpanLink ( previousTraceInfo , startSpanOptions ) ;
288
- }
289
-
290
281
const finalStartSpanOptions : StartSpanOptions = beforeStartSpan
291
282
? beforeStartSpan ( startSpanOptions )
292
283
: startSpanOptions ;
@@ -330,16 +321,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
330
321
} ) ;
331
322
setActiveIdleSpan ( client , idleSpan ) ;
332
323
333
- if ( enablePreviousTrace ) {
334
- previousTraceInfo = {
335
- spanContext : idleSpan . spanContext ( ) ,
336
- startTimestamp : spanToJSON ( idleSpan ) . start_timestamp ,
337
- } ;
338
- if ( persistPreviousTrace ) {
339
- storePreviousTraceInSessionStorage ( previousTraceInfo ) ;
340
- }
341
- }
342
-
343
324
function emitFinish ( ) : void {
344
325
if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
345
326
client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
@@ -468,6 +449,22 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
468
449
shouldCreateSpanForRequest,
469
450
enableHTTPTimings,
470
451
} ) ;
452
+
453
+ if ( enablePreviousTrace ) {
454
+ let previousTraceInfo = persistPreviousTrace ? getPreviousTraceFromSessionStorage ( ) : undefined ;
455
+
456
+ client . on ( 'spanStart' , span => {
457
+ if ( getRootSpan ( span ) !== span ) {
458
+ return ;
459
+ }
460
+
461
+ previousTraceInfo = addPreviousTraceSpanLink ( previousTraceInfo , span ) ;
462
+
463
+ if ( persistPreviousTrace ) {
464
+ storePreviousTraceInSessionStorage ( previousTraceInfo ) ;
465
+ }
466
+ } ) ;
467
+ }
471
468
} ,
472
469
} ;
473
470
} ) satisfies IntegrationFn ;
0 commit comments