Skip to content

Commit 4e75465

Browse files
committed
parametrize server api requests
1 parent 3537d54 commit 4e75465

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
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
@@ -135,8 +135,8 @@ test.describe('distributed tracing', () => {
135135
expect(serverReqTxnEvent).toEqual(
136136
expect.objectContaining({
137137
type: 'transaction',
138-
transaction: `GET /api/user/${PARAM}`,
139-
transaction_info: { source: 'url' },
138+
transaction: `GET /api/user/:userId`, // parametrized route
139+
transaction_info: { source: 'route' },
140140
contexts: expect.objectContaining({
141141
trace: expect.objectContaining({
142142
op: 'http.server',

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
@@ -135,8 +135,8 @@ test.describe('distributed tracing', () => {
135135
expect(serverReqTxnEvent).toEqual(
136136
expect.objectContaining({
137137
type: 'transaction',
138-
transaction: `GET /api/user/${PARAM}`,
139-
transaction_info: { source: 'url' },
138+
transaction: `GET /api/user/:userId`, // parametrized route
139+
transaction_info: { source: 'route' },
140140
contexts: expect.objectContaining({
141141
trace: expect.objectContaining({
142142
op: 'http.server',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ test.describe('distributed tracing', () => {
135135
expect(serverReqTxnEvent).toEqual(
136136
expect.objectContaining({
137137
type: 'transaction',
138-
transaction: `GET /api/user/${PARAM}`,
139-
transaction_info: { source: 'url' },
138+
transaction: `GET /api/user/:userId`, // parametrized route
139+
transaction_info: { source: 'route' },
140140
contexts: expect.objectContaining({
141141
trace: expect.objectContaining({
142142
op: 'http.server',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ test.describe('distributed tracing', () => {
135135
expect(serverReqTxnEvent).toEqual(
136136
expect.objectContaining({
137137
type: 'transaction',
138-
transaction: `GET /api/user/${PARAM}`,
139-
transaction_info: { source: 'url' },
138+
transaction: `GET /api/user/:userId`, // parametrized route
139+
transaction_info: { source: 'route' },
140140
contexts: expect.objectContaining({
141141
trace: expect.objectContaining({
142142
op: 'http.server',

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

Lines changed: 5 additions & 5 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}`,
51+
transaction_info: { source: 'url' },
5252
type: 'transaction',
5353
contexts: {
5454
trace: {
@@ -135,8 +135,8 @@ test.describe('distributed tracing', () => {
135135
expect(serverReqTxnEvent).toEqual(
136136
expect.objectContaining({
137137
type: 'transaction',
138-
transaction: `GET /api/user/${PARAM}`,
139-
transaction_info: { source: 'url' },
138+
transaction: `GET /api/user/:userId`, // parametrized route
139+
transaction_info: { source: 'route' },
140140
contexts: expect.objectContaining({
141141
trace: expect.objectContaining({
142142
op: 'http.server',

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import type { H3Event } from 'h3';
66
*/
77
export function updateRouteBeforeResponse(event: H3Event): void {
88
if (event.context.matchedRoute) {
9-
const matchedRoute = event.context.matchedRoute;
10-
const matchedRoutePath = matchedRoute.path;
11-
const params = event.context?.params || null;
12-
const method = event._method || 'GET';
9+
const matchedRoutePath = event.context.matchedRoute.path;
1310

1411
// If the matched route path is defined and differs from the event's path, it indicates a parametrized route
15-
// Example: If the matched route is "/users/:id" and the event's path is "/users/123",
12+
// Example: Matched route is "/users/:id" and the event's path is "/users/123",
1613
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+
}
19+
20+
const method = event._method || 'GET';
21+
1722
const parametrizedTransactionName = `${method.toUpperCase()} ${matchedRoutePath}`;
1823
getCurrentScope().setTransactionName(parametrizedTransactionName);
1924

@@ -22,8 +27,12 @@ export function updateRouteBeforeResponse(event: H3Event): void {
2227
const rootSpan = getRootSpan(activeSpan);
2328
if (rootSpan) {
2429
rootSpan.updateName(parametrizedTransactionName);
25-
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
26-
rootSpan.setAttribute('http.route', matchedRoutePath);
30+
rootSpan.setAttributes({
31+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
32+
'http.route': matchedRoutePath,
33+
});
34+
35+
const params = event.context?.params || null;
2736

2837
if (params && typeof params === 'object') {
2938
Object.entries(params).forEach(([key, value]) => {

0 commit comments

Comments
 (0)