Skip to content

Commit be692e6

Browse files
committed
fix(apm): Make sure performance spans change transaction start timestamp
1 parent 832726c commit be692e6

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
}
@@ -663,12 +663,12 @@ export class Tracing implements Integration {
663663
// we already instrument based on fetch and xhr, so we don't need to
664664
// duplicate spans here.
665665
if (entry.initiatorType !== 'xmlhttprequest' && entry.initiatorType !== 'fetch') {
666-
const resource = transactionSpan.startChild({
666+
const resource = _startChild(transactionSpan, {
667667
description: `${entry.initiatorType} ${resourceName}`,
668+
endTimestamp: timeOrigin + startTime + duration,
668669
op: `resource`,
670+
startTimestamp: timeOrigin + startTime,
669671
});
670-
resource.startTimestamp = timeOrigin + startTime;
671-
resource.endTimestamp = resource.startTimestamp + duration;
672672
// We remember the entry script end time to calculate the difference to the first init mark
673673
if (entryScriptStartEndTime === undefined && (entryScriptSrc || '').indexOf(resourceName) > -1) {
674674
entryScriptStartEndTime = resource.endTimestamp;
@@ -681,7 +681,7 @@ export class Tracing implements Integration {
681681
});
682682

683683
if (entryScriptStartEndTime !== undefined && tracingInitMarkStartTime !== undefined) {
684-
transactionSpan.startChild({
684+
_startChild(transactionSpan, {
685685
description: 'evaluation',
686686
endTimestamp: tracingInitMarkStartTime,
687687
op: `script`,
@@ -1046,3 +1046,19 @@ function historyCallback(_: { [key: string]: any }): void {
10461046
});
10471047
}
10481048
}
1049+
1050+
/**
1051+
* Helper function to start child on transactions. This function will make sure that the transaction will
1052+
* will use the start timestamp of the created child span if it is earlier than the transactions actual
1053+
* start timestamp.
1054+
*/
1055+
export function _startChild(parent: Span, { startTimestamp, ...ctx }: SpanContext): Span {
1056+
if (startTimestamp && parent.startTimestamp > startTimestamp) {
1057+
parent.startTimestamp = startTimestamp;
1058+
}
1059+
1060+
return parent.startChild({
1061+
startTimestamp,
1062+
...ctx,
1063+
});
1064+
}

0 commit comments

Comments
 (0)