Skip to content

Commit

Permalink
feat(tracing): Ensure pageload transaction starts at timeOrigin (#7632
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mydea committed Mar 28, 2023
1 parent 3ba8265 commit 849297a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/browser';
import { Integrations } from '@sentry/tracing';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [new Integrations.BrowserTracing()],
tracesSampleRate: 1,
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ sentryTest('should create a pageload transaction', async ({ getLocalTestPath, pa
const url = await getLocalTestPath({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');

const { start_timestamp: startTimestamp } = eventData;

expect(startTimestamp).toBeCloseTo(timeOrigin, 1);

expect(eventData.contexts?.trace?.op).toBe('pageload');
expect(eventData.spans?.length).toBeGreaterThan(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Sentry from '@sentry/browser';
import { Integrations } from '@sentry/tracing';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;

setTimeout(() => {
window._testTimeoutTimestamp = (performance.timeOrigin + performance.now()) / 1000;
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [new Integrations.BrowserTracing()],
tracesSampleRate: 1,
});
}, 250);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest('should create a pageload transaction when initialized delayed', async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');
const timeoutTimestamp = await page.evaluate<number>('window._testTimeoutTimestamp');

const { start_timestamp: startTimestamp } = eventData;

expect(startTimestamp).toBeCloseTo(timeOrigin, 1);
expect(startTimestamp).toBeLessThan(timeoutTimestamp);

expect(eventData.contexts?.trace?.op).toBe('pageload');
expect(eventData.spans?.length).toBeGreaterThan(0);
expect(eventData.transaction_info?.source).toEqual('url');
});
4 changes: 3 additions & 1 deletion packages/tracing-internal/src/browser/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Transaction, TransactionContext } from '@sentry/types';
import { addInstrumentationHandler, logger } from '@sentry/utils';
import { addInstrumentationHandler, browserPerformanceTimeOrigin, logger } from '@sentry/utils';

import { WINDOW } from './types';

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

0 comments on commit 849297a

Please sign in to comment.