@@ -95,6 +95,18 @@ interface Activity {
95
95
const global = getGlobalObject < Window > ( ) ;
96
96
const defaultTracingOrigins = [ 'localhost' , / ^ \/ / ] ;
97
97
98
+ if ( global . performance ) {
99
+ // Polyfill for performance.timeOrigin.
100
+ //
101
+ // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin
102
+ // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing.
103
+ // tslint:disable-next-line:strict-type-predicates
104
+ if ( performance . timeOrigin === undefined ) {
105
+ // tslint:disable-next-line:deprecation
106
+ performance . timeOrigin = performance . timing . navigationStart ;
107
+ }
108
+ }
109
+
98
110
/**
99
111
* Tracing Integration
100
112
*/
@@ -382,15 +394,11 @@ export class Tracing implements Integration {
382
394
// Gatekeeper if performance API not available
383
395
return ;
384
396
}
397
+
385
398
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
- }
399
+
400
+ const timeOrigin = Tracing . _msToSec ( performance . timeOrigin ) ;
401
+
394
402
// tslint:disable-next-line: completed-docs
395
403
function addSpan ( span : SpanClass ) : void {
396
404
if ( transactionSpan . spanRecorder ) {
@@ -404,8 +412,8 @@ export class Tracing implements Integration {
404
412
description : event ,
405
413
op : 'browser' ,
406
414
} ) ;
407
- span . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry [ `${ event } Start` ] ) ;
408
- span . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry [ `${ event } End` ] ) ;
415
+ span . startTimestamp = timeOrigin + Tracing . _msToSec ( entry [ `${ event } Start` ] ) ;
416
+ span . timestamp = timeOrigin + Tracing . _msToSec ( entry [ `${ event } End` ] ) ;
409
417
addSpan ( span ) ;
410
418
}
411
419
@@ -415,15 +423,15 @@ export class Tracing implements Integration {
415
423
description : 'request' ,
416
424
op : 'browser' ,
417
425
} ) ;
418
- request . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry . requestStart ) ;
419
- request . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseEnd ) ;
426
+ request . startTimestamp = timeOrigin + Tracing . _msToSec ( entry . requestStart ) ;
427
+ request . timestamp = timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ;
420
428
addSpan ( request ) ;
421
429
const response = parent . child ( {
422
430
description : 'response' ,
423
431
op : 'browser' ,
424
432
} ) ;
425
- response . startTimestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseStart ) ;
426
- response . timestamp = parent . startTimestamp + Tracing . _msToSec ( entry . responseEnd ) ;
433
+ response . startTimestamp = timeOrigin + Tracing . _msToSec ( entry . responseStart ) ;
434
+ response . timestamp = timeOrigin + Tracing . _msToSec ( entry . responseEnd ) ;
427
435
addSpan ( response ) ;
428
436
}
429
437
@@ -469,7 +477,7 @@ export class Tracing implements Integration {
469
477
description : `${ entry . entryType } ${ entry . name } ` ,
470
478
op : 'mark' ,
471
479
} ) ;
472
- mark . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
480
+ mark . startTimestamp = timeOrigin + startTime ;
473
481
mark . timestamp = mark . startTimestamp + duration ;
474
482
if ( tracingInitMarkStartTime === undefined && entry . name === 'sentry-tracing-init' ) {
475
483
tracingInitMarkStartTime = mark . startTimestamp ;
@@ -483,7 +491,7 @@ export class Tracing implements Integration {
483
491
if ( transactionSpan . spanRecorder ) {
484
492
transactionSpan . spanRecorder . finishedSpans . map ( ( finishedSpan : SpanClass ) => {
485
493
if ( finishedSpan . description && finishedSpan . description . indexOf ( resourceName ) !== - 1 ) {
486
- finishedSpan . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
494
+ finishedSpan . startTimestamp = timeOrigin + startTime ;
487
495
finishedSpan . timestamp = finishedSpan . startTimestamp + duration ;
488
496
}
489
497
} ) ;
@@ -493,7 +501,7 @@ export class Tracing implements Integration {
493
501
description : `${ entry . initiatorType } ${ resourceName } ` ,
494
502
op : `resource` ,
495
503
} ) ;
496
- resource . startTimestamp = transactionSpan . startTimestamp + startTime - navigationOffset ;
504
+ resource . startTimestamp = timeOrigin + startTime ;
497
505
resource . timestamp = resource . startTimestamp + duration ;
498
506
// We remember the entry script end time to calculate the difference to the first init mark
499
507
if ( entryScriptStartEndTime === undefined && ( entryScriptSrc || '' ) . includes ( resourceName ) ) {
@@ -651,10 +659,17 @@ export class Tracing implements Integration {
651
659
} ) ;
652
660
}
653
661
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 ;
662
+ // If there is an offset in data, update timestamps accordingly
663
+ if (
664
+ global . performance &&
665
+ span . data &&
666
+ typeof span . data . offset === 'number' &&
667
+ typeof span . timestamp === 'number'
668
+ ) {
669
+ const timeOrigin = Tracing . _msToSec ( performance . timeOrigin ) ;
670
+ const duration = span . timestamp - span . startTimestamp ;
671
+ span . startTimestamp = timeOrigin + span . data . offset ;
672
+ span . timestamp = timeOrigin + duration ;
658
673
}
659
674
}
660
675
// tslint:disable-next-line: no-dynamic-delete
0 commit comments