Skip to content

Commit 6ef628d

Browse files
committed
ref(browser): Update scope.transactionName on pageload and navigation span creation
1 parent 40c847c commit 6ef628d

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setTimeout(() => {
2+
throw new Error('Error during pageload');
3+
}, 100);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest(
7+
'should put the pageload transaction name onto an error event caught during pageload',
8+
async ({ getLocalTestPath, page }) => {
9+
if (shouldSkipTracingTest()) {
10+
sentryTest.skip();
11+
}
12+
13+
const url = await getLocalTestPath({ testDir: __dirname });
14+
15+
await page.goto(url);
16+
17+
const [e1, e2] = await getMultipleSentryEnvelopeRequests<Event>(page, 2);
18+
19+
const pageloadTxnEvent = e1.type === 'transaction' ? e1 : e2;
20+
const errorEvent = e1.type === 'transaction' ? e2 : e1;
21+
22+
expect(pageloadTxnEvent.contexts?.trace?.op).toEqual('pageload');
23+
expect(pageloadTxnEvent.spans?.length).toBeGreaterThan(0);
24+
expect(errorEvent.exception?.values?.[0]).toBeDefined();
25+
26+
expect(pageloadTxnEvent.transaction?.endsWith('index.html')).toBe(true);
27+
28+
expect(errorEvent.transaction).toEqual(pageloadTxnEvent.transaction);
29+
},
30+
);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
364364
export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
365365
client.emit('startPageLoadSpan', spanOptions);
366366

367+
getCurrentScope().setTransactionName(spanOptions.name);
368+
367369
const span = getActiveSpan();
368370
const op = span && spanToJSON(span).op;
369371
return op === 'pageload' ? span : undefined;
@@ -376,6 +378,8 @@ export function startBrowserTracingPageLoadSpan(client: Client, spanOptions: Sta
376378
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
377379
client.emit('startNavigationSpan', spanOptions);
378380

381+
getCurrentScope().setTransactionName(spanOptions.name);
382+
379383
const span = getActiveSpan();
380384
const op = span && spanToJSON(span).op;
381385
return op === 'navigation' ? span : undefined;

packages/tracing-internal/test/browser/browserTracingIntegration.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ describe('browserTracingIntegration', () => {
374374

375375
expect(spanToJSON(pageloadSpan!).op).toBe('test op');
376376
});
377+
378+
it('sets the pageload span name on `scope.transactionName`', () => {
379+
const client = new TestClient(
380+
getDefaultClientOptions({
381+
integrations: [browserTracingIntegration()],
382+
}),
383+
);
384+
setCurrentClient(client);
385+
client.init();
386+
387+
startBrowserTracingPageLoadSpan(client, { name: 'test pageload span' });
388+
389+
expect(getCurrentScope().getScopeData().transactionName).toBe('test pageload span');
390+
});
377391
});
378392

379393
it('sets source to "custom" if name is changed in beforeStartSpan', () => {
@@ -584,6 +598,20 @@ describe('browserTracingIntegration', () => {
584598
expect(spanToJSON(pageloadSpan!).description).toBe('changed');
585599
expect(spanToJSON(pageloadSpan!).data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]).toBe('custom');
586600
});
601+
602+
it('sets the pageload span name on `scope.transactionName`', () => {
603+
const client = new TestClient(
604+
getDefaultClientOptions({
605+
integrations: [browserTracingIntegration()],
606+
}),
607+
);
608+
setCurrentClient(client);
609+
client.init();
610+
611+
startBrowserTracingPageLoadSpan(client, { name: 'test navigation span' });
612+
613+
expect(getCurrentScope().getScopeData().transactionName).toBe('test navigation span');
614+
});
587615
});
588616

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

0 commit comments

Comments
 (0)