Skip to content

Commit 678dd8e

Browse files
committed
feat(tracing): Ensure pageload transaction starts at timeOrigin
1 parent 7d080dc commit 678dd8e

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { Integrations } from '@sentry/tracing';
3+
4+
window.Sentry = Sentry;
5+
window._testBaseTimestamp = performance.timeOrigin / 1000;
6+
7+
Sentry.init({
8+
dsn: 'https://[email protected]/1337',
9+
integrations: [new Integrations.BrowserTracing()],
10+
tracesSampleRate: 1,
11+
});

packages/browser-integration-tests/suites/tracing/browsertracing/pageload/test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ sentryTest('should create a pageload transaction', async ({ getLocalTestPath, pa
1212
const url = await getLocalTestPath({ testDir: __dirname });
1313

1414
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
15+
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');
16+
17+
const { start_timestamp: startTimestamp } = eventData;
18+
19+
expect(startTimestamp).toBeCloseTo(timeOrigin);
1520

1621
expect(eventData.contexts?.trace?.op).toBe('pageload');
1722
expect(eventData.spans?.length).toBeGreaterThan(0);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { Integrations } from '@sentry/tracing';
3+
4+
window.Sentry = Sentry;
5+
window._testBaseTimestamp = performance.timeOrigin / 1000;
6+
7+
setTimeout(() => {
8+
window._testTimeoutTimestamp = (performance.timeOrigin + performance.now()) / 1000;
9+
Sentry.init({
10+
dsn: 'https://[email protected]/1337',
11+
integrations: [new Integrations.BrowserTracing()],
12+
tracesSampleRate: 1,
13+
});
14+
}, 250);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
6+
7+
sentryTest('should create a pageload transaction when initialized delayed', async ({ getLocalTestPath, page }) => {
8+
if (shouldSkipTracingTest()) {
9+
sentryTest.skip();
10+
}
11+
12+
const url = await getLocalTestPath({ testDir: __dirname });
13+
14+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
15+
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');
16+
const timeoutTimestamp = await page.evaluate<number>('window._testTimeoutTimestamp');
17+
18+
const { start_timestamp: startTimestamp } = eventData;
19+
20+
expect(startTimestamp).toBeCloseTo(timeOrigin);
21+
expect(startTimestamp).toBeLessThan(timeoutTimestamp);
22+
23+
expect(eventData.contexts?.trace?.op).toBe('pageload');
24+
expect(eventData.spans?.length).toBeGreaterThan(0);
25+
expect(eventData.transaction_info?.source).toEqual('url');
26+
});

packages/tracing-internal/src/browser/router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Transaction, TransactionContext } from '@sentry/types';
2-
import { addInstrumentationHandler, logger } from '@sentry/utils';
2+
import { addInstrumentationHandler, browserPerformanceTimeOrigin, logger } from '@sentry/utils';
33

44
import { WINDOW } from './types';
55

@@ -22,6 +22,8 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(
2222
if (startTransactionOnPageLoad) {
2323
activeTransaction = customStartTransaction({
2424
name: WINDOW.location.pathname,
25+
// pageload should always start at timeOrigin
26+
startTimestamp: browserPerformanceTimeOrigin,
2527
op: 'pageload',
2628
metadata: { source: 'url' },
2729
});

0 commit comments

Comments
 (0)