Skip to content

Commit 62167cf

Browse files
committed
fix(apm): Make sure performance spans change transaction start timestamp
1 parent ae950c7 commit 62167cf

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

packages/apm/src/integrations/tracing.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ export class Tracing implements Integration {
581581

582582
// tslint:disable-next-line: completed-docs
583583
function addPerformanceNavigationTiming(parent: Span, entry: { [key: string]: number }, event: string): void {
584-
parent.startChild({
584+
_startChild(parent, {
585585
description: event,
586586
endTimestamp: timeOrigin + Tracing._msToSec(entry[`${event}End`]),
587587
op: 'browser',
@@ -591,14 +591,14 @@ export class Tracing implements Integration {
591591

592592
// tslint:disable-next-line: completed-docs
593593
function addRequest(parent: Span, entry: { [key: string]: number }): void {
594-
parent.startChild({
594+
_startChild(parent, {
595595
description: 'request',
596596
endTimestamp: timeOrigin + Tracing._msToSec(entry.responseEnd),
597597
op: 'browser',
598598
startTimestamp: timeOrigin + Tracing._msToSec(entry.requestStart),
599599
});
600600

601-
parent.startChild({
601+
_startChild(parent, {
602602
description: 'response',
603603
endTimestamp: timeOrigin + Tracing._msToSec(entry.responseEnd),
604604
op: 'browser',
@@ -648,12 +648,12 @@ export class Tracing implements Integration {
648648
case 'mark':
649649
case 'paint':
650650
case 'measure':
651-
const mark = transactionSpan.startChild({
651+
const mark = _startChild(transactionSpan, {
652652
description: entry.name,
653+
endTimestamp: timeOrigin + startTime + duration,
653654
op: entry.entryType,
655+
startTimestamp: timeOrigin + startTime,
654656
});
655-
mark.startTimestamp = timeOrigin + startTime;
656-
mark.endTimestamp = mark.startTimestamp + duration;
657657
if (tracingInitMarkStartTime === undefined && entry.name === 'sentry-tracing-init') {
658658
tracingInitMarkStartTime = mark.startTimestamp;
659659
}
@@ -671,12 +671,12 @@ export class Tracing implements Integration {
671671
});
672672
}
673673
} else {
674-
const resource = transactionSpan.startChild({
674+
const resource = _startChild(transactionSpan, {
675675
description: `${entry.initiatorType} ${resourceName}`,
676+
endTimestamp: timeOrigin + startTime + duration,
676677
op: `resource`,
678+
startTimestamp: timeOrigin + startTime,
677679
});
678-
resource.startTimestamp = timeOrigin + startTime;
679-
resource.endTimestamp = resource.startTimestamp + duration;
680680
// We remember the entry script end time to calculate the difference to the first init mark
681681
if (entryScriptStartEndTime === undefined && (entryScriptSrc || '').indexOf(resourceName) > -1) {
682682
entryScriptStartEndTime = resource.endTimestamp;
@@ -689,7 +689,7 @@ export class Tracing implements Integration {
689689
});
690690

691691
if (entryScriptStartEndTime !== undefined && tracingInitMarkStartTime !== undefined) {
692-
transactionSpan.startChild({
692+
_startChild(transactionSpan, {
693693
description: 'evaluation',
694694
endTimestamp: tracingInitMarkStartTime,
695695
op: `script`,
@@ -1054,3 +1054,19 @@ function historyCallback(_: { [key: string]: any }): void {
10541054
});
10551055
}
10561056
}
1057+
1058+
/**
1059+
* Helper function to start child on transactions. This function will make sure that the transaction will
1060+
* will use the start timestamp of the created child span if it is earlier than the transactions actual
1061+
* start timestamp.
1062+
*/
1063+
export function _startChild(parent: Span, { startTimestamp, ...ctx }: SpanContext): Span {
1064+
if (startTimestamp && parent.startTimestamp > startTimestamp) {
1065+
parent.startTimestamp = startTimestamp;
1066+
}
1067+
1068+
return parent.startChild({
1069+
startTimestamp,
1070+
...ctx,
1071+
});
1072+
}

0 commit comments

Comments
 (0)