Skip to content

Commit 9c04b47

Browse files
committed
feat: remove transaction and SDK metadata setting from the wrapper
1 parent 03e7627 commit 9c04b47

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import {
2-
captureException,
3-
debug,
4-
getActiveSpan,
5-
getIsolationScope,
6-
getRootSpan,
7-
httpRequestToRequestData,
8-
objectify,
9-
} from '@sentry/core';
1+
import { captureException, debug, objectify } from '@sentry/core';
102
import type { NextApiRequest } from 'next';
11-
import { TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL } from '../span-attributes-with-logic-attached';
123
import type { AugmentedNextApiResponse, NextApiHandler } from '../types';
134
import { flushSafelyWithTimeout } from '../utils/responseEnd';
145

@@ -52,24 +43,6 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
5243

5344
req.__withSentry_applied__ = true;
5445

55-
// Set transaction name on isolation scope to ensure parameterized routes are used
56-
// The HTTP server integration sets it on isolation scope, so we need to match that
57-
const method = req.method || 'GET';
58-
const isolationScope = getIsolationScope();
59-
isolationScope.setTransactionName(`${method} ${parameterizedRoute}`);
60-
// Set SDK processing metadata
61-
isolationScope.setSDKProcessingMetadata({
62-
normalizedRequest: httpRequestToRequestData(req),
63-
});
64-
65-
// Set the route backfill attribute on the root span so that the transaction name
66-
// gets updated to use the parameterized route during event processing
67-
const activeSpan = getActiveSpan();
68-
if (activeSpan) {
69-
const rootSpan = getRootSpan(activeSpan);
70-
rootSpan.setAttribute(TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL, parameterizedRoute);
71-
}
72-
7346
return await wrappingTarget.apply(thisArg, args);
7447
} catch (e) {
7548
// In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can

packages/nextjs/src/common/span-attributes-with-logic-attached.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export const TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION = 'sentry.drop_transaction
66
export const TRANSACTION_ATTR_SENTRY_TRACE_BACKFILL = 'sentry.sentry_trace_backfill';
77

88
export const TRANSACTION_ATTR_SENTRY_ROUTE_BACKFILL = 'sentry.route_backfill';
9+
10+
export const ATTR_NEXT_PAGES_API_ROUTE_TYPE = 'executing api route (pages)';

packages/nextjs/src/edge/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import {
2222
import { getScopesFromContext } from '@sentry/opentelemetry';
2323
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
2424
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
25-
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';
25+
import { ATTR_NEXT_SPAN_NAME, ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes';
26+
import {
27+
ATTR_NEXT_PAGES_API_ROUTE_TYPE,
28+
TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION,
29+
} from '../common/span-attributes-with-logic-attached';
2630
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
2731
import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests';
2832
import { isBuild } from '../common/utils/isBuild';
@@ -82,12 +86,21 @@ export function init(options: VercelEdgeOptions = {}): void {
8286
dropMiddlewareTunnelRequests(span, spanAttributes);
8387

8488
// Mark all spans generated by Next.js as 'auto'
85-
if (spanAttributes?.['next.span_type'] !== undefined) {
89+
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] !== undefined) {
8690
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
8791
}
8892

93+
// Backfill span attributes for api route pages because we removed it from the wrapper
94+
if (
95+
spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'Node.runHandler' &&
96+
String(spanAttributes?.['next.span_name']).startsWith(ATTR_NEXT_PAGES_API_ROUTE_TYPE)
97+
) {
98+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server');
99+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
100+
}
101+
89102
// Make sure middleware spans get the right op
90-
if (spanAttributes?.['next.span_type'] === 'Middleware.execute') {
103+
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute') {
91104
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');
92105
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');
93106

@@ -119,8 +132,8 @@ export function init(options: VercelEdgeOptions = {}): void {
119132
// The otel auto inference will clobber the transaction name because the span has an http.target
120133
if (
121134
event.type === 'transaction' &&
122-
event.contexts?.trace?.data?.['next.span_type'] === 'Middleware.execute' &&
123-
event.contexts?.trace?.data?.['next.span_name']
135+
event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute' &&
136+
event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_NAME] !== undefined
124137
) {
125138
if (event.transaction) {
126139
// Older nextjs versions pass the full url appended to the middleware name, which results in high cardinality transaction names.
@@ -139,6 +152,20 @@ export function init(options: VercelEdgeOptions = {}): void {
139152
}
140153
}
141154

155+
// Backfill the transaction name for api route pages because we removed it from the wrapper
156+
if (
157+
event.type === 'transaction' &&
158+
event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_TYPE] === 'Node.runHandler' &&
159+
String(event.contexts.trace.data['next.span_name']).startsWith(ATTR_NEXT_PAGES_API_ROUTE_TYPE)
160+
) {
161+
let path = String(event.contexts.trace.data['next.span_name']).replace(ATTR_NEXT_PAGES_API_ROUTE_TYPE, '').trim();
162+
// Set transaction name on isolation scope to ensure parameterized routes are used
163+
// The HTTP server integration sets it on isolation scope, so we need to match that
164+
const method = event.request?.method || 'GET';
165+
path = path ?? event.request?.url ?? '/';
166+
event.transaction = `${method} ${path}`;
167+
}
168+
142169
setUrlProcessingMetadata(event);
143170
});
144171

0 commit comments

Comments
 (0)