Skip to content

Commit a79b9a0

Browse files
committed
apply code review feedback
1 parent 6fbd1e9 commit a79b9a0

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

packages/sveltekit/src/client/router.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { getCurrentHub, WINDOW } from '@sentry/svelte';
1+
import { getActiveTransaction } from '@sentry/core';
2+
import { WINDOW } from '@sentry/svelte';
23
import type { Span, Transaction, TransactionContext } from '@sentry/types';
34

45
import { navigating, page } from '$app/stores';
56

7+
const DEFAULT_TAGS = {
8+
'routing.instrumentation': '@sentry/sveltekit',
9+
};
10+
611
/**
712
*
813
* @param startTransactionFn
@@ -25,7 +30,16 @@ export function svelteKitRoutingInstrumentation<T extends Transaction>(
2530
}
2631

2732
function instrumentPageload(startTransactionFn: (context: TransactionContext) => Transaction | undefined): void {
28-
const pageloadTransaction = createPageloadTxn(startTransactionFn);
33+
const initialPath = WINDOW && WINDOW.location && WINDOW.location.pathname;
34+
35+
const pageloadTransaction = startTransactionFn({
36+
name: initialPath,
37+
op: 'pageload',
38+
description: initialPath,
39+
tags: {
40+
...DEFAULT_TAGS,
41+
},
42+
});
2943

3044
page.subscribe(page => {
3145
if (!page) {
@@ -62,13 +76,20 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
6276
const routeDestination = navigation.to && navigation.to.route.id;
6377
const routeOrigin = navigation.from && navigation.from.route.id;
6478

79+
if (routeOrigin === routeDestination) {
80+
return;
81+
}
82+
6583
activeTransaction = getActiveTransaction();
6684

6785
if (!activeTransaction) {
6886
activeTransaction = startTransactionFn({
69-
name: routeDestination || 'unknown',
87+
name: routeDestination || (WINDOW && WINDOW.location && WINDOW.location.pathname),
7088
op: 'navigation',
7189
metadata: { source: 'route' },
90+
tags: {
91+
...DEFAULT_TAGS,
92+
},
7293
});
7394
}
7495

@@ -78,31 +99,10 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
7899
routingSpan.finish();
79100
}
80101
routingSpan = activeTransaction.startChild({
81-
description: 'SvelteKit Route Change',
82102
op: 'ui.sveltekit.routing',
83-
tags: {
84-
'routing.instrumentation': '@sentry/sveltekit',
85-
from: routeOrigin,
86-
to: routeDestination,
87-
},
103+
description: 'SvelteKit Route Change',
88104
});
105+
activeTransaction.setTag('from', routeOrigin);
89106
}
90107
});
91108
}
92-
93-
function createPageloadTxn(
94-
startTransactionFn: (context: TransactionContext) => Transaction | undefined,
95-
): Transaction | undefined {
96-
const ctx: TransactionContext = {
97-
name: 'pageload',
98-
op: 'pageload',
99-
description: WINDOW.location.pathname,
100-
};
101-
102-
return startTransactionFn(ctx);
103-
}
104-
105-
function getActiveTransaction(): Transaction | undefined {
106-
const scope = getCurrentHub().getScope();
107-
return scope && scope.getTransaction();
108-
}

packages/sveltekit/test/client/router.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('sveltekitRoutingInstrumentation', () => {
3030
startChild: vi.fn().mockImplementation(ctx => {
3131
return { ...mockedRoutingSpan, ...ctx };
3232
}),
33+
setTag: vi.fn(),
3334
};
3435
return returnedTransaction;
3536
});
@@ -50,9 +51,12 @@ describe('sveltekitRoutingInstrumentation', () => {
5051

5152
expect(mockedStartTransaction).toHaveBeenCalledTimes(1);
5253
expect(mockedStartTransaction).toHaveBeenCalledWith({
53-
name: 'pageload',
54+
name: '/',
5455
op: 'pageload',
5556
description: '/',
57+
tags: {
58+
'routing.instrumentation': '@sentry/sveltekit',
59+
},
5660
});
5761

5862
// We emit an update to the `page` store to simulate the SvelteKit router lifecycle
@@ -101,18 +105,18 @@ describe('sveltekitRoutingInstrumentation', () => {
101105
metadata: {
102106
source: 'route',
103107
},
108+
tags: {
109+
'routing.instrumentation': '@sentry/sveltekit',
110+
},
104111
});
105112

106113
expect(returnedTransaction?.startChild).toHaveBeenCalledWith({
107114
op: 'ui.sveltekit.routing',
108115
description: 'SvelteKit Route Change',
109-
tags: {
110-
'routing.instrumentation': '@sentry/sveltekit',
111-
from: 'testNavigationOrigin',
112-
to: 'testNavigationDestination',
113-
},
114116
});
115117

118+
expect(returnedTransaction?.setTag).toHaveBeenCalledWith('from', 'testNavigationOrigin');
119+
116120
// We emit `null` here to simulate the end of the navigation lifecycle
117121
// @ts-ignore this is fine
118122
navigating.set(null);

0 commit comments

Comments
 (0)