Skip to content

Commit 537527e

Browse files
authored
ref(tracing): Add transaction source to default router (#5386)
1 parent 2bc61b0 commit 537527e

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

packages/integration-tests/suites/tracing/browsertracing/navigation/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ sentryTest('should create a navigation transaction on page navigation', async ({
1313
expect(pageloadRequest.contexts?.trace.op).toBe('pageload');
1414
expect(navigationRequest.contexts?.trace.op).toBe('navigation');
1515

16+
expect(navigationRequest.transaction_info?.source).toEqual('url');
17+
1618
const pageloadTraceId = pageloadRequest.contexts?.trace.trace_id;
1719
const navigationTraceId = navigationRequest.contexts?.trace.trace_id;
1820

packages/integration-tests/suites/tracing/browsertracing/pageload/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ sentryTest('should create a pageload transaction', async ({ getLocalTestPath, pa
1111

1212
expect(eventData.contexts?.trace?.op).toBe('pageload');
1313
expect(eventData.spans?.length).toBeGreaterThan(0);
14+
expect(eventData.transaction_info?.source).toEqual('url');
1415
});

packages/tracing/src/browser/browsertracing.ts

+6
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ export class BrowserTracing implements Integration {
208208
const expandedContext = {
209209
...context,
210210
...parentContextFromHeader,
211+
...(parentContextFromHeader && {
212+
metadata: {
213+
...context.metadata,
214+
...parentContextFromHeader.metadata,
215+
},
216+
}),
211217
trimEnd: true,
212218
};
213219
const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate(expandedContext) : expandedContext;

packages/tracing/src/browser/router.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(
2020

2121
let activeTransaction: T | undefined;
2222
if (startTransactionOnPageLoad) {
23-
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'pageload' });
23+
activeTransaction = customStartTransaction({
24+
name: global.location.pathname,
25+
op: 'pageload',
26+
metadata: { source: 'url' },
27+
});
2428
}
2529

2630
if (startTransactionOnLocationChange) {
@@ -46,7 +50,11 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(
4650
// If there's an open transaction on the scope, we need to finish it before creating an new one.
4751
activeTransaction.finish();
4852
}
49-
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'navigation' });
53+
activeTransaction = customStartTransaction({
54+
name: global.location.pathname,
55+
op: 'navigation',
56+
metadata: { source: 'url' },
57+
});
5058
}
5159
});
5260
}

packages/tracing/test/browser/router.test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ describe('instrumentRoutingWithDefaults', () => {
4242
it('starts a pageload transaction', () => {
4343
instrumentRoutingWithDefaults(customStartTransaction);
4444
expect(customStartTransaction).toHaveBeenCalledTimes(1);
45-
expect(customStartTransaction).toHaveBeenLastCalledWith({ name: 'blank', op: 'pageload' });
45+
expect(customStartTransaction).toHaveBeenLastCalledWith({
46+
name: 'blank',
47+
op: 'pageload',
48+
metadata: { source: 'url' },
49+
});
4650
});
4751

4852
it('does not start a pageload transaction if startTransactionOnPageLoad is false', () => {
@@ -58,7 +62,11 @@ describe('instrumentRoutingWithDefaults', () => {
5862

5963
it('it is not created automatically', () => {
6064
instrumentRoutingWithDefaults(customStartTransaction);
61-
expect(customStartTransaction).not.toHaveBeenLastCalledWith({ name: 'blank', op: 'navigation' });
65+
expect(customStartTransaction).not.toHaveBeenLastCalledWith({
66+
name: 'blank',
67+
op: 'navigation',
68+
metadata: { source: 'url' },
69+
});
6270
});
6371

6472
it('is created on location change', () => {
@@ -67,7 +75,11 @@ describe('instrumentRoutingWithDefaults', () => {
6775
expect(addInstrumentationHandlerType).toBe('history');
6876

6977
expect(customStartTransaction).toHaveBeenCalledTimes(2);
70-
expect(customStartTransaction).toHaveBeenLastCalledWith({ name: 'blank', op: 'navigation' });
78+
expect(customStartTransaction).toHaveBeenLastCalledWith({
79+
name: 'blank',
80+
op: 'navigation',
81+
metadata: { source: 'url' },
82+
});
7183
});
7284

7385
it('is not created if startTransactionOnLocationChange is false', () => {

0 commit comments

Comments
 (0)