Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Sentry from '@sentry/browser';

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

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [Sentry.browserTracingIntegration({ enableReportPageLoaded: true })],
tracesSampleRate: 1,
debug: true,
});

setTimeout(() => {
Sentry.reportPageLoaded();
}, 2500);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from '@playwright/test';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/browser';
import { sentryTest } from '../../../../../utils/fixtures';
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers';

sentryTest(
'waits for Sentry.reportPageLoaded() to be called when `enableReportPageLoaded` is true',
async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const pageloadEventPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === 'pageload');

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

await page.goto(url);

const eventData = envelopeRequestParser(await pageloadEventPromise);

const traceContextData = eventData.contexts?.trace?.data;
const spanDurationSeconds = eventData.timestamp! - eventData.start_timestamp!;

expect(traceContextData).toMatchObject({
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.browser',
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
['sentry.idle_span_finish_reason']: 'reportPageLoaded',
});

// We wait for 2.5 seconds before calling Sentry.reportPageLoaded()
// the margins are to account for timing weirdness in CI to avoid flakes
expect(spanDurationSeconds).toBeGreaterThan(2);
expect(spanDurationSeconds).toBeLessThan(3);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Sentry from '@sentry/browser';

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

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [Sentry.browserTracingIntegration({ enableReportPageLoaded: true, finalTimeout: 3000 })],
tracesSampleRate: 1,
debug: true,
});

// not calling Sentry.reportPageLoaded() on purpose!
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from '@playwright/test';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/browser';
import { sentryTest } from '../../../../../utils/fixtures';
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers';

sentryTest(
'final timeout cancels the pageload span even if `enableReportPageLoaded` is true',
async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const pageloadEventPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === 'pageload');

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

await page.goto(url);

const eventData = envelopeRequestParser(await pageloadEventPromise);

const traceContextData = eventData.contexts?.trace?.data;
const spanDurationSeconds = eventData.timestamp! - eventData.start_timestamp!;

expect(traceContextData).toMatchObject({
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.browser',
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
['sentry.idle_span_finish_reason']: 'finalTimeout',
});

// We wait for 3 seconds before calling Sentry.reportPageLoaded()
// the margins are to account for timing weirdness in CI to avoid flakes
expect(spanDurationSeconds).toBeGreaterThan(2.5);
expect(spanDurationSeconds).toBeLessThan(3.5);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Sentry from '@sentry/browser';

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

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [Sentry.browserTracingIntegration({ enableReportPageLoaded: true, instrumentNavigation: false })],
tracesSampleRate: 1,
debug: true,
});

setTimeout(() => {
Sentry.startBrowserTracingNavigationSpan(Sentry.getClient(), { name: 'custom_navigation' });
}, 1000);

setTimeout(() => {
Sentry.reportPageLoaded();
}, 2500);
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from '@playwright/test';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/browser';
import { sentryTest } from '../../../../../utils/fixtures';
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../../utils/helpers';

sentryTest(
'starting a navigation span cancels the pageload span even if `enableReportPageLoaded` is true',
async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const pageloadEventPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === 'pageload');

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

await page.goto(url);

const eventData = envelopeRequestParser(await pageloadEventPromise);

const traceContextData = eventData.contexts?.trace?.data;
const spanDurationSeconds = eventData.timestamp! - eventData.start_timestamp!;

expect(traceContextData).toMatchObject({
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.browser',
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
['sentry.idle_span_finish_reason']: 'cancelled',
});

// ending span after 1s but adding a margin of 0.5s to account for timing weirdness in CI to avoid flakes
expect(spanDurationSeconds).toBeLessThan(1.5);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
browserTracingIntegration,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
reportPageLoaded,
} from './tracing/browserTracingIntegration';

export { getFeedback, sendFeedback } from '@sentry-internal/feedback';
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
browserTracingIntegration,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
reportPageLoaded,
} from './tracing/browserTracingIntegration';
export { feedbackIntegrationShim as feedbackAsyncIntegration, feedbackIntegrationShim as feedbackIntegration };

Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
browserTracingIntegration,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
reportPageLoaded,
} from './tracing/browserTracingIntegration';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
browserTracingIntegration,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
reportPageLoaded,
} from './tracing/browserTracingIntegration';
export type { RequestInstrumentationOptions } from './tracing/request';
export {
Expand Down
62 changes: 57 additions & 5 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@ export interface BrowserTracingOptions {
*/
consistentTraceSampling: boolean;

/**
* If set to `true`, the pageload span will not end itself automatically, unless it
* runs until the {@link BrowserTracingOptions.finalTimeout} (30 seconds by default) is reached.
*
* Set this option to `true`, if you want full control over the pageload span duration.
* You can use `Sentry.reportPageLoaded()` to manually end the pageload span whenever convenient.
* Be aware that you have to ensure that this is always called, regardless of the chosen route
* or path in the application.
*
* @default `false`. By default, the pageload span will end itself automatically, based on
* the {@link BrowserTracingOptions.finalTimeout}, {@link BrowserTracingOptions.idleTimeout}
* and {@link BrowserTracingOptions.childSpanTimeout}. This is more convenient to use but means
* that the pageload duration can be arbitrary and might not be fully representative of a perceived
* page load time.
*/
enableReportPageLoaded: boolean;

/**
* _experiments allows the user to send options to define how this integration works.
*
Expand Down Expand Up @@ -297,6 +314,7 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
detectRedirects: true,
linkPreviousTrace: 'in-memory',
consistentTraceSampling: false,
enableReportPageLoaded: false,
_experiments: {},
...defaultRequestInstrumentationOptions,
};
Expand All @@ -310,7 +328,7 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
*
* We explicitly export the proper type here, as this has to be extended in some cases.
*/
export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptions> = {}) => {
export const browserTracingIntegration = ((options: Partial<BrowserTracingOptions> = {}) => {
const latestRoute: RouteInfo = {
name: undefined,
source: undefined,
Expand Down Expand Up @@ -345,18 +363,21 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
detectRedirects,
linkPreviousTrace,
consistentTraceSampling,
enableReportPageLoaded,
onRequestSpanStart,
} = {
...DEFAULT_BROWSER_TRACING_OPTIONS,
..._options,
...options,
};

let _collectWebVitals: undefined | (() => void);
let lastInteractionTimestamp: number | undefined;

let _pageloadSpan: Span | undefined;

/** Create routing idle transaction. */
function _createRouteSpan(client: Client, startSpanOptions: StartSpanOptions, makeActive = true): void {
const isPageloadTransaction = startSpanOptions.op === 'pageload';
const isPageloadSpan = startSpanOptions.op === 'pageload';

const initialSpanName = startSpanOptions.name;
const finalStartSpanOptions: StartSpanOptions = beforeStartSpan
Expand Down Expand Up @@ -390,7 +411,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
finalTimeout,
childSpanTimeout,
// should wait for finish signal if it's a pageload transaction
disableAutoFinish: isPageloadTransaction,
disableAutoFinish: isPageloadSpan,
beforeSpanEnd: span => {
// This will generally always be defined here, because it is set in `setup()` of the integration
// but technically, it is optional, so we guard here to be extra safe
Expand All @@ -415,9 +436,19 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
sampled: spanIsSampled(idleSpan),
dsc: getDynamicSamplingContextFromSpan(span),
});

if (isPageloadSpan) {
// clean up the stored pageload span on the intergration.
_pageloadSpan = undefined;
}
},
trimIdleSpanEndTimestamp: !enableReportPageLoaded,
});

if (isPageloadSpan && enableReportPageLoaded) {
_pageloadSpan = idleSpan;
}

setActiveIdleSpan(client, idleSpan);

function emitFinish(): void {
Expand All @@ -426,7 +457,8 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
}
}

if (isPageloadTransaction && optionalWindowDocument) {
// Enable auto finish of the pageload span if users are not explicitly ending it
if (isPageloadSpan && !enableReportPageLoaded && optionalWindowDocument) {
optionalWindowDocument.addEventListener('readystatechange', () => {
emitFinish();
});
Expand Down Expand Up @@ -573,7 +605,15 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
...startSpanOptions,
});
});

client.on('endPageloadSpan', () => {
if (enableReportPageLoaded && _pageloadSpan) {
_pageloadSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON, 'reportPageLoaded');
_pageloadSpan.end();
}
});
},

afterAllSetup(client) {
let startingUrl: string | undefined = getLocationHref();

Expand Down Expand Up @@ -723,6 +763,18 @@ export function getMetaContent(metaName: string): string | undefined {
return metaTag?.getAttribute('content') || undefined;
}

/**
* Manually report the end of the page load, resulting in the SDK ending the pageload span.
* This only works if {@link BrowserTracingOptions.enableReportPageLoaded} is set to `true`.
* Otherwise, the pageload span will end itself based on the {@link BrowserTracingOptions.finalTimeout},
* {@link BrowserTracingOptions.idleTimeout} and {@link BrowserTracingOptions.childSpanTimeout}.
*
* @param client - The client to use. If not provided, the global client will be used.
*/
export function reportPageLoaded(client: Client | undefined = getClient()): void {
client?.emit('endPageloadSpan');
}

/** Start listener for interaction transactions */
function registerInteractionListener(
client: Client,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
) => void,
): () => void;

/**
* A hook for the browser tracing integrations to trigger the end of a page load span.
* @returns {() => void} A function that, when executed, removes the registered callback.
*/
public on(hook: 'endPageloadSpan', callback: () => void): () => void;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to go with the client hooks approach instead of exposing a method on browserTracingIntegration because in contrast to all other integrations, we don't hide the specific implementation of browserTracingIntegration behind defineIntegration. So adding a method would indeed be public API (we also can't introduce defineIntegration now as that would be a breaking change).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is a nice enough API for this anyhow, I like it!


/**
* A hook for the browser tracing integrations to trigger after the pageload span was started.
* @returns {() => void} A function that, when executed, removes the registered callback.
Expand Down Expand Up @@ -800,6 +806,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
traceOptions?: { sentryTrace?: string | undefined; baggage?: string | undefined },
): void;

/**
* Emit a hook event for browser tracing integrations to trigger the end of a page load span.
*/
public emit(hook: 'endPageloadSpan'): void;

/**
* Emit a hook event for browser tracing integrations to trigger aafter the pageload span was started.
*/
Expand Down
Loading
Loading