Skip to content

Commit 89db11c

Browse files
committed
feat(apm): Update Span timing from absolute ref
Use performance.timing.navigationStart as the absolute reference to update Span start and end timestamps.
1 parent 0bc7a4d commit 89db11c

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

packages/apm/src/integrations/tracing.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,11 @@ export class Tracing implements Integration {
382382
// Gatekeeper if performance API not available
383383
return;
384384
}
385+
385386
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+
394390
// tslint:disable-next-line: completed-docs
395391
function addSpan(span: SpanClass): void {
396392
if (transactionSpan.spanRecorder) {
@@ -404,8 +400,8 @@ export class Tracing implements Integration {
404400
description: event,
405401
op: 'browser',
406402
});
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`]);
409405
addSpan(span);
410406
}
411407

@@ -415,15 +411,15 @@ export class Tracing implements Integration {
415411
description: 'request',
416412
op: 'browser',
417413
});
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);
420416
addSpan(request);
421417
const response = parent.child({
422418
description: 'response',
423419
op: 'browser',
424420
});
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);
427423
addSpan(response);
428424
}
429425

@@ -469,7 +465,7 @@ export class Tracing implements Integration {
469465
description: `${entry.entryType} ${entry.name}`,
470466
op: 'mark',
471467
});
472-
mark.startTimestamp = transactionSpan.startTimestamp + startTime - navigationOffset;
468+
mark.startTimestamp = navigationStart + startTime;
473469
mark.timestamp = mark.startTimestamp + duration;
474470
if (tracingInitMarkStartTime === undefined && entry.name === 'sentry-tracing-init') {
475471
tracingInitMarkStartTime = mark.startTimestamp;
@@ -483,7 +479,7 @@ export class Tracing implements Integration {
483479
if (transactionSpan.spanRecorder) {
484480
transactionSpan.spanRecorder.finishedSpans.map((finishedSpan: SpanClass) => {
485481
if (finishedSpan.description && finishedSpan.description.indexOf(resourceName) !== -1) {
486-
finishedSpan.startTimestamp = transactionSpan.startTimestamp + startTime - navigationOffset;
482+
finishedSpan.startTimestamp = navigationStart + startTime;
487483
finishedSpan.timestamp = finishedSpan.startTimestamp + duration;
488484
}
489485
});
@@ -493,7 +489,7 @@ export class Tracing implements Integration {
493489
description: `${entry.initiatorType} ${resourceName}`,
494490
op: `resource`,
495491
});
496-
resource.startTimestamp = transactionSpan.startTimestamp + startTime - navigationOffset;
492+
resource.startTimestamp = navigationStart + startTime;
497493
resource.timestamp = resource.startTimestamp + duration;
498494
// We remember the entry script end time to calculate the difference to the first init mark
499495
if (entryScriptStartEndTime === undefined && (entryScriptSrc || '').includes(resourceName)) {
@@ -651,10 +647,17 @@ export class Tracing implements Integration {
651647
});
652648
}
653649
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;
658661
}
659662
}
660663
// tslint:disable-next-line: no-dynamic-delete

0 commit comments

Comments
 (0)