Skip to content

Commit

Permalink
fix(browser): Ensure that performance.measure spans have a positive…
Browse files Browse the repository at this point in the history
… duration (#15415)
  • Loading branch information
lforst authored Feb 14, 2025
1 parent e991a5f commit f67fa79
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/browser-utils/src/metrics/browserMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export function _addMeasureSpans(
startTime: number,
duration: number,
timeOrigin: number,
): number {
): void {
const navEntry = getNavigationEntry(false);
const requestTime = msToSec(navEntry ? navEntry.requestStart : 0);
// Because performance.measure accepts arbitrary timestamps it can produce
Expand All @@ -450,13 +450,14 @@ export function _addMeasureSpans(
attributes['sentry.browser.measure_start_time'] = measureStartTimestamp;
}

startAndEndSpan(span, measureStartTimestamp, measureEndTimestamp, {
name: entry.name as string,
op: entry.entryType as string,
attributes,
});

return measureStartTimestamp;
// Measurements from third parties can be off, which would create invalid spans, dropping transactions in the process.
if (measureStartTimestamp <= measureEndTimestamp) {
startAndEndSpan(span, measureStartTimestamp, measureEndTimestamp, {
name: entry.name as string,
op: entry.entryType as string,
attributes,
});
}
}

/** Instrument navigation entries */
Expand Down
23 changes: 23 additions & 0 deletions packages/browser-utils/test/browser/browserMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ describe('_addMeasureSpans', () => {
}),
);
});

it('drops measurement spans with negative duration', () => {
const spans: Span[] = [];

getClient()?.on('spanEnd', span => {
spans.push(span);
});

const entry = {
entryType: 'measure',
name: 'measure-1',
duration: 10,
startTime: 12,
} as PerformanceEntry;

const timeOrigin = 100;
const startTime = 23;
const duration = -50;

_addMeasureSpans(span, entry, startTime, duration, timeOrigin);

expect(spans).toHaveLength(0);
});
});

describe('_addResourceSpans', () => {
Expand Down

0 comments on commit f67fa79

Please sign in to comment.