Skip to content

fix(vue): Correct span name assignment for matched routes in VueRouter #12398

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 3 commits into from
Jun 7, 2024
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 @@ -21,6 +21,15 @@ const router = createRouter({
path: '/users-error/:id',
component: () => import('../views/UserIdErrorView.vue'),
},
{
path: '/categories',
children: [
{
path: ':id',
component: () => import('../views/CategoryIdView.vue'),
},
],
},
],
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<h1>Category ID: {{ $route.params.id }}</h1>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ test('sends a navigation transaction with a parameterized URL', async ({ page })
});
});

test('sends a pageload transaction with a nested route URL', async ({ page }) => {
const transactionPromise = waitForTransaction('vue-3', async transactionEvent => {
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
});

await page.goto(`/categories/123`);

const rootSpan = await transactionPromise;

expect(rootSpan).toMatchObject({
contexts: {
trace: {
data: {
'sentry.source': 'route',
'sentry.origin': 'auto.pageload.vue',
'sentry.op': 'pageload',
'params.id': '123',
},
op: 'pageload',
origin: 'auto.pageload.vue',
},
},
transaction: '/categories/:id',
transaction_info: {
source: 'route',
},
});
});

test('sends a pageload transaction with a route name as transaction name if available', async ({ page }) => {
const transactionPromise = waitForTransaction('vue-3', async transactionEvent => {
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
Expand Down
5 changes: 3 additions & 2 deletions packages/vue/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export function instrumentVueRouter(
if (to.name && options.routeLabel !== 'path') {
spanName = to.name.toString();
transactionSource = 'custom';
} else if (to.matched[0] && to.matched[0].path) {
spanName = to.matched[0].path;
} else if (to.matched.length > 0) {
const lastIndex = to.matched.length - 1;
spanName = to.matched[lastIndex].path;
transactionSource = 'route';
}

Expand Down
10 changes: 10 additions & 0 deletions packages/vue/test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const testRoutes: Record<string, Route> = {
path: '/accounts/4',
query: {},
},
nestedRoute: {
matched: [{ path: '/' }, { path: '/categories' }, { path: '/categories/:categoryId' }],
params: {
categoryId: '1',
},
path: '/categories/1',
query: {},
},
namedRoute: {
matched: [{ path: '/login' }],
name: 'login-screen',
Expand Down Expand Up @@ -85,6 +93,7 @@ describe('instrumentVueRouter()', () => {

it.each([
['normalRoute1', 'normalRoute2', '/accounts/:accountId', 'route'],
['normalRoute1', 'nestedRoute', '/categories/:categoryId', 'route'],
['normalRoute2', 'namedRoute', 'login-screen', 'custom'],
['normalRoute2', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
])(
Expand Down Expand Up @@ -122,6 +131,7 @@ describe('instrumentVueRouter()', () => {

it.each([
['initialPageloadRoute', 'normalRoute1', '/books/:bookId/chapter/:chapterId', 'route'],
['initialPageloadRoute', 'nestedRoute', '/categories/:categoryId', 'route'],
['initialPageloadRoute', 'namedRoute', 'login-screen', 'custom'],
['initialPageloadRoute', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
])(
Expand Down
Loading