Skip to content

Commit 6de8d44

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

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-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: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { context } from '@opentelemetry/api';
55
import {
66
applySdkMetadata,
7+
type Event,
78
type EventProcessor,
89
getCapturedScopesOnSpan,
910
getCurrentScope,
@@ -18,11 +19,16 @@ import {
1819
setCapturedScopesOnSpan,
1920
spanToJSON,
2021
stripUrlQueryAndFragment,
22+
type TransactionEvent,
2123
} from '@sentry/core';
2224
import { getScopesFromContext } from '@sentry/opentelemetry';
2325
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
2426
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
25-
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';
27+
import { ATTR_NEXT_SPAN_NAME, ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes';
28+
import {
29+
ATTR_NEXT_PAGES_API_ROUTE_TYPE,
30+
TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION,
31+
} from '../common/span-attributes-with-logic-attached';
2632
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
2733
import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests';
2834
import { isBuild } from '../common/utils/isBuild';
@@ -82,12 +88,21 @@ export function init(options: VercelEdgeOptions = {}): void {
8288
dropMiddlewareTunnelRequests(span, spanAttributes);
8389

8490
// Mark all spans generated by Next.js as 'auto'
85-
if (spanAttributes?.['next.span_type'] !== undefined) {
91+
if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] !== undefined) {
8692
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
8793
}
8894

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

@@ -119,8 +134,8 @@ export function init(options: VercelEdgeOptions = {}): void {
119134
// The otel auto inference will clobber the transaction name because the span has an http.target
120135
if (
121136
event.type === 'transaction' &&
122-
event.contexts?.trace?.data?.['next.span_type'] === 'Middleware.execute' &&
123-
event.contexts?.trace?.data?.['next.span_name']
137+
event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute' &&
138+
event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_NAME] !== undefined
124139
) {
125140
if (event.transaction) {
126141
// Older nextjs versions pass the full url appended to the middleware name, which results in high cardinality transaction names.
@@ -139,6 +154,20 @@ export function init(options: VercelEdgeOptions = {}): void {
139154
}
140155
}
141156

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

0 commit comments

Comments
 (0)