Skip to content

Commit 7da3667

Browse files
committed
review suggestions and fix tests
1 parent 0c398ab commit 7da3667

File tree

6 files changed

+42
-40
lines changed

6 files changed

+42
-40
lines changed

dev-packages/e2e-tests/test-applications/nuxt-3-dynamic-import/tests/tracing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ test.describe('distributed tracing', () => {
4747
});
4848

4949
expect(serverTxnEvent).toMatchObject({
50-
transaction: `GET /test-param/:param`,
51-
transaction_info: { source: 'route' },
50+
transaction: `GET /test-param/${PARAM}`, // todo: parametrize
51+
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {
5454
trace: {

dev-packages/e2e-tests/test-applications/nuxt-3-min/tests/tracing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ test.describe('distributed tracing', () => {
4747
});
4848

4949
expect(serverTxnEvent).toMatchObject({
50-
transaction: `GET /test-param/:param`,
51-
transaction_info: { source: 'route' },
50+
transaction: `GET /test-param/${PARAM}`, // todo: parametrize
51+
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {
5454
trace: {

dev-packages/e2e-tests/test-applications/nuxt-3-top-level-import/tests/tracing.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test.describe('distributed tracing', () => {
4747
});
4848

4949
expect(serverTxnEvent).toMatchObject({
50-
transaction: `GET /test-param/${PARAM}`, // todo: parametrize (nitro)
50+
transaction: `GET /test-param/${PARAM}`, // todo: parametrize
5151
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {

dev-packages/e2e-tests/test-applications/nuxt-3/tests/tracing.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test.describe('distributed tracing', () => {
2323
const baggageMetaTagContent = await page.locator('meta[name="baggage"]').getAttribute('content');
2424

2525
expect(baggageMetaTagContent).toContain(`sentry-trace_id=${serverTxnEvent.contexts?.trace?.trace_id}`);
26-
expect(baggageMetaTagContent).toContain(`sentry-transaction=GET%20%2Ftest-param%2F%3Aparam`); // URL-encoded for 'GET /test-param/:param'
26+
expect(baggageMetaTagContent).toContain(`sentry-transaction=GET%20%2Ftest-param%2F${PARAM}`); // URL-encoded for 'GET /test-param/s0me-param'
2727
expect(baggageMetaTagContent).toContain('sentry-sampled=true');
2828
expect(baggageMetaTagContent).toContain('sentry-sample_rate=1');
2929

@@ -47,8 +47,8 @@ test.describe('distributed tracing', () => {
4747
});
4848

4949
expect(serverTxnEvent).toMatchObject({
50-
transaction: `GET /test-param/:param`,
51-
transaction_info: { source: 'route' },
50+
transaction: `GET /test-param/${PARAM}`, // todo: parametrize
51+
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {
5454
trace: {

dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test.describe('distributed tracing', () => {
4747
});
4848

4949
expect(serverTxnEvent).toMatchObject({
50-
transaction: `GET /test-param/${PARAM}`,
50+
transaction: `GET /test-param/${PARAM}`, // todo: parametrize
5151
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {

packages/nuxt/src/runtime/hooks/updateRouteBeforeResponse.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,46 @@ import type { H3Event } from 'h3';
55
* Update the root span (transaction) name for routes with parameters based on the matched route.
66
*/
77
export function updateRouteBeforeResponse(event: H3Event): void {
8-
if (event.context.matchedRoute) {
9-
const matchedRoutePath = event.context.matchedRoute.path;
10-
11-
// If the matched route path is defined and differs from the event's path, it indicates a parametrized route
12-
// Example: Matched route is "/users/:id" and the event's path is "/users/123",
13-
if (matchedRoutePath && matchedRoutePath !== event._path) {
14-
if (matchedRoutePath === '/**') {
15-
// todo: support parametrized SSR pageload spans
16-
// If page is server-side rendered, the whole path gets transformed to `/**` (Example : `/users/123` becomes `/**` instead of `/users/:id`).
17-
return; // Skip if the matched route is a catch-all route.
18-
}
8+
if (!event.context.matchedRoute) {
9+
return;
10+
}
1911

20-
const method = event._method || 'GET';
12+
const matchedRoutePath = event.context.matchedRoute.path;
2113

22-
const parametrizedTransactionName = `${method.toUpperCase()} ${matchedRoutePath}`;
23-
getCurrentScope().setTransactionName(parametrizedTransactionName);
14+
// If the matched route path is defined and differs from the event's path, it indicates a parametrized route
15+
// Example: Matched route is "/users/:id" and the event's path is "/users/123",
16+
if (matchedRoutePath && matchedRoutePath !== event._path) {
17+
if (matchedRoutePath === '/**') {
18+
// todo: support parametrized SSR pageload spans
19+
// If page is server-side rendered, the whole path gets transformed to `/**` (Example : `/users/123` becomes `/**` instead of `/users/:id`).
20+
return; // Skip if the matched route is a catch-all route.
21+
}
2422

25-
const activeSpan = getActiveSpan(); // In development mode, getActiveSpan() is always undefined
26-
if (activeSpan) {
27-
const rootSpan = getRootSpan(activeSpan);
28-
if (rootSpan) {
29-
rootSpan.updateName(parametrizedTransactionName);
30-
rootSpan.setAttributes({
31-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
32-
'http.route': matchedRoutePath,
33-
});
23+
const method = event._method || 'GET';
3424

35-
const params = event.context?.params || null;
25+
const parametrizedTransactionName = `${method.toUpperCase()} ${matchedRoutePath}`;
26+
getCurrentScope().setTransactionName(parametrizedTransactionName);
3627

37-
if (params && typeof params === 'object') {
38-
Object.entries(params).forEach(([key, value]) => {
39-
// Based on this convention: https://getsentry.github.io/sentry-conventions/generated/attributes/url.html#urlpathparameterkey
40-
rootSpan.setAttribute(`url.path.parameter.${key}`, String(value));
41-
});
42-
}
28+
const activeSpan = getActiveSpan(); // In development mode, getActiveSpan() is always undefined
29+
if (activeSpan) {
30+
const rootSpan = getRootSpan(activeSpan);
31+
if (rootSpan) {
32+
rootSpan.updateName(parametrizedTransactionName);
33+
rootSpan.setAttributes({
34+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
35+
'http.route': matchedRoutePath,
36+
});
4337

44-
logger.log(`Updated transaction name for parametrized route: ${parametrizedTransactionName}`);
38+
const params = event.context?.params;
39+
40+
if (params && typeof params === 'object') {
41+
Object.entries(params).forEach(([key, value]) => {
42+
// Based on this convention: https://getsentry.github.io/sentry-conventions/generated/attributes/url.html#urlpathparameterkey
43+
rootSpan.setAttribute(`url.path.parameter.${key}`, String(value));
44+
});
4545
}
46+
47+
logger.log(`Updated transaction name for parametrized route: ${parametrizedTransactionName}`);
4648
}
4749
}
4850
}

0 commit comments

Comments
 (0)