Skip to content

Commit 8a5584f

Browse files
committed
set source to 'custom' when beforeNavigate changes transaction name
1 parent 053163f commit 8a5584f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ export class BrowserTracing implements Integration {
222222
// from being sent to Sentry).
223223
const finalContext = modifiedContext === undefined ? { ...expandedContext, sampled: false } : modifiedContext;
224224

225+
// If `beforeNavigate` set a custom name, record that fact
226+
finalContext.metadata =
227+
finalContext.name !== expandedContext.name
228+
? { ...finalContext.metadata, source: 'custom' }
229+
: finalContext.metadata;
230+
225231
if (finalContext.sampled === false) {
226232
__DEBUG_BUILD__ &&
227233
logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`);

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,39 @@ describe('BrowserTracing', () => {
216216

217217
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
218218
});
219+
220+
it("sets transaction name source to `'custom'` if name is changed", () => {
221+
const mockBeforeNavigation = jest.fn(ctx => ({
222+
...ctx,
223+
name: 'newName',
224+
}));
225+
createBrowserTracing(true, {
226+
beforeNavigate: mockBeforeNavigation,
227+
routingInstrumentation: customInstrumentRouting,
228+
});
229+
const transaction = getActiveTransaction(hub) as IdleTransaction;
230+
expect(transaction).toBeDefined();
231+
expect(transaction.name).toBe('newName');
232+
expect(transaction.metadata.source).toBe('custom');
233+
234+
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
235+
});
236+
237+
it("doesn't set transaction name source if name is not changed", () => {
238+
const mockBeforeNavigation = jest.fn(ctx => ({
239+
...ctx,
240+
}));
241+
createBrowserTracing(true, {
242+
beforeNavigate: mockBeforeNavigation,
243+
routingInstrumentation: customInstrumentRouting,
244+
});
245+
const transaction = getActiveTransaction(hub) as IdleTransaction;
246+
expect(transaction).toBeDefined();
247+
expect(transaction.name).toBe('a/path');
248+
expect(transaction.metadata.source).toBeUndefined();
249+
250+
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
251+
});
219252
});
220253

221254
it('sets transaction context from sentry-trace header', () => {

0 commit comments

Comments
 (0)