Skip to content

ref(browser): Update scope.transactionName on pageload and navigation span creation #10992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,3 @@
setTimeout(() => {
throw new Error('Error during pageload');
}, 100);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';
import { sentryTest } from '../../../../utils/fixtures';
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';

sentryTest(
'should put the pageload transaction name onto an error event caught during pageload',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

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

await page.goto(url);

const [e1, e2] = await getMultipleSentryEnvelopeRequests<Event>(page, 2);

const pageloadTxnEvent = e1.type === 'transaction' ? e1 : e2;
const errorEvent = e1.type === 'transaction' ? e2 : e1;

expect(pageloadTxnEvent.contexts?.trace?.op).toEqual('pageload');
expect(pageloadTxnEvent.spans?.length).toBeGreaterThan(0);
expect(errorEvent.exception?.values?.[0]).toBeDefined();

expect(pageloadTxnEvent.transaction?.endsWith('index.html')).toBe(true);

expect(errorEvent.transaction).toEqual(pageloadTxnEvent.transaction);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
client.emit('startPageLoadSpan', spanOptions);

getCurrentScope().setTransactionName(spanOptions.name);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder now, if that should possibly go on the isolation scope 🤔 (generally for all our auto-instrumentation). Has up-and downsides... probably it's fine this way, just thinking out loud!

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I think in the browser it doesn't matter that much, given we're not automatically forking scopes (afaik) 🤔

I think on the server, the isolation scope makes a lot of sense since it generally lasts as long as a request, no?


const span = getActiveSpan();
const op = span && spanToJSON(span).op;
return op === 'pageload' ? span : undefined;
Expand All @@ -376,6 +378,8 @@ export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: Sta
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
client.emit('startNavigationSpan', spanOptions);

getCurrentScope().setTransactionName(spanOptions.name);

const span = getActiveSpan();
const op = span && spanToJSON(span).op;
return op === 'navigation' ? span : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,20 @@ describe('browserTracingIntegration', () => {

expect(spanToJSON(pageloadSpan!).op).toBe('test op');
});

it('sets the pageload span name on `scope.transactionName`', () => {
const client = new TestClient(
getDefaultClientOptions({
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

startBrowserTracingPageLoadSpan(client, { name: 'test pageload span' });

expect(getCurrentScope().getScopeData().transactionName).toBe('test pageload span');
});
});

it('sets source to "custom" if name is changed in beforeStartSpan', () => {
Expand Down Expand Up @@ -584,6 +598,20 @@ describe('browserTracingIntegration', () => {
expect(spanToJSON(pageloadSpan!).description).toBe('changed');
expect(spanToJSON(pageloadSpan!).data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]).toBe('custom');
});

it('sets the pageload span name on `scope.transactionName`', () => {
const client = new TestClient(
getDefaultClientOptions({
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

startBrowserTracingPageLoadSpan(client, { name: 'test navigation span' });

expect(getCurrentScope().getScopeData().transactionName).toBe('test navigation span');
});
});

describe('using the <meta> tag data', () => {
Expand Down