Skip to content

feat(vue): Allow to set routeLabel: 'path' to opt-out of using name #7326

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 2 commits into from
Mar 3, 2023
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
20 changes: 18 additions & 2 deletions packages/vue/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import type { Transaction, TransactionContext, TransactionSource } from '@sentry

import { getActiveTransaction } from './tracing';

interface VueRouterInstrumationOptions {
/**
* What to use for route labels.
* By default, we use route.name (if set) and else the path.
*
* Default: 'name'
*/
routeLabel: 'name' | 'path';
}

export type VueRouterInstrumentation = <T extends Transaction>(
startTransaction: (context: TransactionContext) => T | undefined,
startTransactionOnPageLoad?: boolean,
Expand Down Expand Up @@ -35,9 +45,15 @@ interface VueRouter {
/**
* Creates routing instrumentation for Vue Router v2, v3 and v4
*
* You can optionally pass in an options object with the available option:
* * `routeLabel`: Set this to `route` to opt-out of using `route.name` for transaction names.
*
* @param router The Vue Router instance that is used
*/
export function vueRouterInstrumentation(router: VueRouter): VueRouterInstrumentation {
export function vueRouterInstrumentation(
router: VueRouter,
options: Partial<VueRouterInstrumationOptions> = {},
): VueRouterInstrumentation {
return (
startTransaction: (context: TransactionContext) => Transaction | undefined,
startTransactionOnPageLoad: boolean = true,
Expand Down Expand Up @@ -81,7 +97,7 @@ export function vueRouterInstrumentation(router: VueRouter): VueRouterInstrument
// Determine a name for the routing transaction and where that name came from
let transactionName: string = to.path;
let transactionSource: TransactionSource = 'url';
if (to.name) {
if (to.name && options.routeLabel !== 'path') {
transactionName = to.name.toString();
transactionSource = 'custom';
} else if (to.matched[0] && to.matched[0].path) {
Expand Down
62 changes: 62 additions & 0 deletions packages/vue/test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,68 @@ describe('vueRouterInstrumentation()', () => {
},
);

it('allows to configure routeLabel=path', () => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'path' });

// instrument
instrument(mockStartTransaction, true, true);

// check
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];

const from = testRoutes.normalRoute1;
const to = testRoutes.namedRoute;
beforeEachCallback(to, from, mockNext);

// first startTx call happens when the instrumentation is initialized (for pageloads)
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/login',
metadata: {
source: 'route',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually 'route' or shouldn't this be 'url' now? This has an impact on dynamic sampling and the performance product.

Copy link
Member Author

Choose a reason for hiding this comment

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

This just disables using the name, so it uses the parametrized route when available and falls back to the URL if not.

See:

let transactionName: string = to.path;

},
data: {
params: to.params,
query: to.query,
},
op: 'navigation',
tags: {
'routing.instrumentation': 'vue-router',
},
});
});

it('allows to configure routeLabel=name', () => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'name' });

// instrument
instrument(mockStartTransaction, true, true);

// check
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];

const from = testRoutes.normalRoute1;
const to = testRoutes.namedRoute;
beforeEachCallback(to, from, mockNext);

// first startTx call happens when the instrumentation is initialized (for pageloads)
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: 'login-screen',
metadata: {
source: 'custom',
},
data: {
params: to.params,
query: to.query,
},
op: 'navigation',
tags: {
'routing.instrumentation': 'vue-router',
},
});
});

it("doesn't overwrite a pageload transaction name it was set to custom before the router resolved the route", () => {
const mockedTxn = {
setName: jest.fn(),
Expand Down