@@ -382,15 +382,11 @@ export class Tracing implements Integration {
382
382
// Gatekeeper if performance API not available
383
383
return ;
384
384
}
385
+
385
386
logger . log ( '[Tracing] Adding & adjusting spans using Performance API' ) ;
386
- let navigationOffset = 0 ;
387
- if (
388
- transactionSpan . op === 'navigation' &&
389
- transactionSpan . data &&
390
- typeof transactionSpan . data . offset === 'number'
391
- ) {
392
- navigationOffset = transactionSpan . data . offset ;
393
- }
387
+
388
+ const navigationStart = Tracing . _msToSec ( performance . timing . navigationStart ) ;
389
+
394
390
// tslint:disable-next-line: completed-docs
395
391
function addSpan ( span : SpanClass ) : void {
396
392
if ( transactionSpan . spanRecorder ) {
@@ -404,8 +400,8 @@ export class Tracing implements Integration {
404
400
description : event ,
405
401
op : 'browser' ,
406
402
} ) ;
407
- span . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry [ `${ event } Start` ] ) ;
408
- span . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry [ `${ event } End` ] ) ;
403
+ span . startTimestamp = navigationStart + Tracing . _msToSec ( entry [ `${ event } Start` ] ) ;
404
+ span . timestamp = navigationStart + Tracing . _msToSec ( entry [ `${ event } End` ] ) ;
409
405
addSpan ( span ) ;
410
406
}
411
407
@@ -415,15 +411,15 @@ export class Tracing implements Integration {
415
411
description : 'request' ,
416
412
op : 'browser' ,
417
413
} ) ;
418
- request . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry . requestStart ) ;
419
- request . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseEnd ) ;
414
+ request . startTimestamp = navigationStart + Tracing . _msToSec ( entry . requestStart ) ;
415
+ request . timestamp = navigationStart + Tracing . _msToSec ( entry . responseEnd ) ;
420
416
addSpan ( request ) ;
421
417
const response = parent . child ( {
422
418
description : 'response' ,
423
419
op : 'browser' ,
424
420
} ) ;
425
- response . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseStart ) ;
426
- response . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseEnd ) ;
421
+ response . startTimestamp = navigationStart + Tracing . _msToSec ( entry . responseStart ) ;
422
+ response . timestamp = navigationStart + Tracing . _msToSec ( entry . responseEnd ) ;
427
423
addSpan ( response ) ;
428
424
}
429
425
@@ -469,7 +465,7 @@ export class Tracing implements Integration {
469
465
description : `${ entry . entryType } ${ entry . name } ` ,
470
466
op : 'mark' ,
471
467
} ) ;
472
- mark . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
468
+ mark . startTimestamp = navigationStart + startTime ;
473
469
mark . timestamp = mark . startTimestamp + duration ;
474
470
if ( tracingInitMarkStartTime === undefined && entry . name === 'sentry-tracing-init' ) {
475
471
tracingInitMarkStartTime = mark . startTimestamp ;
@@ -483,7 +479,7 @@ export class Tracing implements Integration {
483
479
if ( transactionSpan . spanRecorder ) {
484
480
transactionSpan . spanRecorder . finishedSpans . map ( ( finishedSpan : SpanClass ) => {
485
481
if ( finishedSpan . description && finishedSpan . description . indexOf ( resourceName ) !== - 1 ) {
486
- finishedSpan . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
482
+ finishedSpan . startTimestamp = navigationStart + startTime ;
487
483
finishedSpan . timestamp = finishedSpan . startTimestamp + duration ;
488
484
}
489
485
} ) ;
@@ -493,7 +489,7 @@ export class Tracing implements Integration {
493
489
description : `${ entry . initiatorType } ${ resourceName } ` ,
494
490
op : `resource` ,
495
491
} ) ;
496
- resource . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
492
+ resource . startTimestamp = navigationStart + startTime ;
497
493
resource . timestamp = resource . startTimestamp + duration ;
498
494
// We remember the entry script end time to calculate the difference to the first init mark
499
495
if ( entryScriptStartEndTime === undefined && ( entryScriptSrc || '' ) . includes ( resourceName ) ) {
@@ -651,10 +647,17 @@ export class Tracing implements Integration {
651
647
} ) ;
652
648
}
653
649
span . finish ( ) ;
654
- // If there is an offset in data, we need to shift timestamps towards it
655
- if ( span . data && typeof span . data . offset === 'number' && typeof span . timestamp === 'number' ) {
656
- span . startTimestamp += span . data . offset ;
657
- span . timestamp += span . data . offset ;
650
+ // If there is an offset in data, update timestamps accordingly
651
+ if (
652
+ global . performance &&
653
+ span . data &&
654
+ typeof span . data . offset === 'number' &&
655
+ typeof span . timestamp === 'number'
656
+ ) {
657
+ const navigationStart = performance . timing . navigationStart ;
658
+ const duration = span . timestamp - span . startTimestamp ;
659
+ span . startTimestamp = navigationStart + span . data . offset ;
660
+ span . timestamp = navigationStart + duration ;
658
661
}
659
662
}
660
663
// tslint:disable-next-line: no-dynamic-delete
0 commit comments