Skip to content

Commit f67fa79

Browse files
authored
fix(browser): Ensure that performance.measure spans have a positive duration (#15415)
1 parent e991a5f commit f67fa79

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/browser-utils/src/metrics/browserMetrics.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export function _addMeasureSpans(
425425
startTime: number,
426426
duration: number,
427427
timeOrigin: number,
428-
): number {
428+
): void {
429429
const navEntry = getNavigationEntry(false);
430430
const requestTime = msToSec(navEntry ? navEntry.requestStart : 0);
431431
// Because performance.measure accepts arbitrary timestamps it can produce
@@ -450,13 +450,14 @@ export function _addMeasureSpans(
450450
attributes['sentry.browser.measure_start_time'] = measureStartTimestamp;
451451
}
452452

453-
startAndEndSpan(span, measureStartTimestamp, measureEndTimestamp, {
454-
name: entry.name as string,
455-
op: entry.entryType as string,
456-
attributes,
457-
});
458-
459-
return measureStartTimestamp;
453+
// Measurements from third parties can be off, which would create invalid spans, dropping transactions in the process.
454+
if (measureStartTimestamp <= measureEndTimestamp) {
455+
startAndEndSpan(span, measureStartTimestamp, measureEndTimestamp, {
456+
name: entry.name as string,
457+
op: entry.entryType as string,
458+
attributes,
459+
});
460+
}
460461
}
461462

462463
/** Instrument navigation entries */

packages/browser-utils/test/browser/browserMetrics.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ describe('_addMeasureSpans', () => {
9292
}),
9393
);
9494
});
95+
96+
it('drops measurement spans with negative duration', () => {
97+
const spans: Span[] = [];
98+
99+
getClient()?.on('spanEnd', span => {
100+
spans.push(span);
101+
});
102+
103+
const entry = {
104+
entryType: 'measure',
105+
name: 'measure-1',
106+
duration: 10,
107+
startTime: 12,
108+
} as PerformanceEntry;
109+
110+
const timeOrigin = 100;
111+
const startTime = 23;
112+
const duration = -50;
113+
114+
_addMeasureSpans(span, entry, startTime, duration, timeOrigin);
115+
116+
expect(spans).toHaveLength(0);
117+
});
95118
});
96119

97120
describe('_addResourceSpans', () => {

0 commit comments

Comments
 (0)