Skip to content

Commit 95a5f48

Browse files
authored
feat(vue): Allow to set routeLabel: 'path' to opt-out of using name (#7326)
1 parent 45158f4 commit 95a5f48

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

packages/vue/src/router.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import type { Transaction, TransactionContext, TransactionSource } from '@sentry
33

44
import { getActiveTransaction } from './tracing';
55

6+
interface VueRouterInstrumationOptions {
7+
/**
8+
* What to use for route labels.
9+
* By default, we use route.name (if set) and else the path.
10+
*
11+
* Default: 'name'
12+
*/
13+
routeLabel: 'name' | 'path';
14+
}
15+
616
export type VueRouterInstrumentation = <T extends Transaction>(
717
startTransaction: (context: TransactionContext) => T | undefined,
818
startTransactionOnPageLoad?: boolean,
@@ -35,9 +45,15 @@ interface VueRouter {
3545
/**
3646
* Creates routing instrumentation for Vue Router v2, v3 and v4
3747
*
48+
* You can optionally pass in an options object with the available option:
49+
* * `routeLabel`: Set this to `route` to opt-out of using `route.name` for transaction names.
50+
*
3851
* @param router The Vue Router instance that is used
3952
*/
40-
export function vueRouterInstrumentation(router: VueRouter): VueRouterInstrumentation {
53+
export function vueRouterInstrumentation(
54+
router: VueRouter,
55+
options: Partial<VueRouterInstrumationOptions> = {},
56+
): VueRouterInstrumentation {
4157
return (
4258
startTransaction: (context: TransactionContext) => Transaction | undefined,
4359
startTransactionOnPageLoad: boolean = true,
@@ -81,7 +97,7 @@ export function vueRouterInstrumentation(router: VueRouter): VueRouterInstrument
8197
// Determine a name for the routing transaction and where that name came from
8298
let transactionName: string = to.path;
8399
let transactionSource: TransactionSource = 'url';
84-
if (to.name) {
100+
if (to.name && options.routeLabel !== 'path') {
85101
transactionName = to.name.toString();
86102
transactionSource = 'custom';
87103
} else if (to.matched[0] && to.matched[0].path) {

packages/vue/test/router.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,68 @@ describe('vueRouterInstrumentation()', () => {
169169
},
170170
);
171171

172+
it('allows to configure routeLabel=path', () => {
173+
// create instrumentation
174+
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'path' });
175+
176+
// instrument
177+
instrument(mockStartTransaction, true, true);
178+
179+
// check
180+
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
181+
182+
const from = testRoutes.normalRoute1;
183+
const to = testRoutes.namedRoute;
184+
beforeEachCallback(to, from, mockNext);
185+
186+
// first startTx call happens when the instrumentation is initialized (for pageloads)
187+
expect(mockStartTransaction).toHaveBeenLastCalledWith({
188+
name: '/login',
189+
metadata: {
190+
source: 'route',
191+
},
192+
data: {
193+
params: to.params,
194+
query: to.query,
195+
},
196+
op: 'navigation',
197+
tags: {
198+
'routing.instrumentation': 'vue-router',
199+
},
200+
});
201+
});
202+
203+
it('allows to configure routeLabel=name', () => {
204+
// create instrumentation
205+
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'name' });
206+
207+
// instrument
208+
instrument(mockStartTransaction, true, true);
209+
210+
// check
211+
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
212+
213+
const from = testRoutes.normalRoute1;
214+
const to = testRoutes.namedRoute;
215+
beforeEachCallback(to, from, mockNext);
216+
217+
// first startTx call happens when the instrumentation is initialized (for pageloads)
218+
expect(mockStartTransaction).toHaveBeenLastCalledWith({
219+
name: 'login-screen',
220+
metadata: {
221+
source: 'custom',
222+
},
223+
data: {
224+
params: to.params,
225+
query: to.query,
226+
},
227+
op: 'navigation',
228+
tags: {
229+
'routing.instrumentation': 'vue-router',
230+
},
231+
});
232+
});
233+
172234
it("doesn't overwrite a pageload transaction name it was set to custom before the router resolved the route", () => {
173235
const mockedTxn = {
174236
setName: jest.fn(),

0 commit comments

Comments
 (0)