Skip to content

ref(tracing): Add transaction source to default router #5386

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 5 commits into from
Jul 7, 2022
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
Expand Up @@ -13,6 +13,8 @@ sentryTest('should create a navigation transaction on page navigation', async ({
expect(pageloadRequest.contexts?.trace.op).toBe('pageload');
expect(navigationRequest.contexts?.trace.op).toBe('navigation');

expect(navigationRequest.transaction_info?.source).toEqual('url');

const pageloadTraceId = pageloadRequest.contexts?.trace.trace_id;
const navigationTraceId = navigationRequest.contexts?.trace.trace_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ sentryTest('should create a pageload transaction', async ({ getLocalTestPath, pa

expect(eventData.contexts?.trace?.op).toBe('pageload');
expect(eventData.spans?.length).toBeGreaterThan(0);
expect(eventData.transaction_info?.source).toEqual('url');
});
6 changes: 6 additions & 0 deletions packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ export class BrowserTracing implements Integration {
const expandedContext = {
...context,
...parentContextFromHeader,
...(parentContextFromHeader && {
metadata: {
...context.metadata,
...parentContextFromHeader.metadata,
},
}),
Comment on lines +211 to +216
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to add this to make the integration tests pass. TODO on coming back to it

trimEnd: true,
};
const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate(expandedContext) : expandedContext;
Expand Down
12 changes: 10 additions & 2 deletions packages/tracing/src/browser/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(

let activeTransaction: T | undefined;
if (startTransactionOnPageLoad) {
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'pageload' });
activeTransaction = customStartTransaction({
name: global.location.pathname,
op: 'pageload',
metadata: { source: 'url' },
});
}

if (startTransactionOnLocationChange) {
Expand All @@ -46,7 +50,11 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(
// If there's an open transaction on the scope, we need to finish it before creating an new one.
activeTransaction.finish();
}
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'navigation' });
activeTransaction = customStartTransaction({
name: global.location.pathname,
op: 'navigation',
metadata: { source: 'url' },
});
}
});
}
Expand Down
18 changes: 15 additions & 3 deletions packages/tracing/test/browser/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('instrumentRoutingWithDefaults', () => {
it('starts a pageload transaction', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(customStartTransaction).toHaveBeenCalledTimes(1);
expect(customStartTransaction).toHaveBeenLastCalledWith({ name: 'blank', op: 'pageload' });
expect(customStartTransaction).toHaveBeenLastCalledWith({
name: 'blank',
op: 'pageload',
metadata: { source: 'url' },
});
});

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

it('it is not created automatically', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(customStartTransaction).not.toHaveBeenLastCalledWith({ name: 'blank', op: 'navigation' });
expect(customStartTransaction).not.toHaveBeenLastCalledWith({
name: 'blank',
op: 'navigation',
metadata: { source: 'url' },
});
});

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

expect(customStartTransaction).toHaveBeenCalledTimes(2);
expect(customStartTransaction).toHaveBeenLastCalledWith({ name: 'blank', op: 'navigation' });
expect(customStartTransaction).toHaveBeenLastCalledWith({
name: 'blank',
op: 'navigation',
metadata: { source: 'url' },
});
});

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