-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(nextjs): remove tracing from pages router API routes #18394
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
base: develop
Are you sure you want to change the base?
Changes from all commits
e2fef71
6fce173
ec986a8
31fac7a
eb4c84d
596150f
ea4d841
bdb4be3
ceaa144
03e7627
9c04b47
ecfe19b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,11 @@ import { | |
| import { getScopesFromContext } from '@sentry/opentelemetry'; | ||
| import type { VercelEdgeOptions } from '@sentry/vercel-edge'; | ||
| import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge'; | ||
| import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached'; | ||
| import { ATTR_NEXT_SPAN_NAME, ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes'; | ||
| import { | ||
| ATTR_NEXT_PAGES_API_ROUTE_TYPE, | ||
| TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION, | ||
| } from '../common/span-attributes-with-logic-attached'; | ||
| import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes'; | ||
| import { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests'; | ||
| import { isBuild } from '../common/utils/isBuild'; | ||
|
|
@@ -82,12 +86,21 @@ export function init(options: VercelEdgeOptions = {}): void { | |
| dropMiddlewareTunnelRequests(span, spanAttributes); | ||
|
|
||
| // Mark all spans generated by Next.js as 'auto' | ||
| if (spanAttributes?.['next.span_type'] !== undefined) { | ||
| if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] !== undefined) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto'); | ||
| } | ||
|
|
||
| // Backfill span attributes for api route pages because we removed it from the wrapper | ||
| if ( | ||
| spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'Node.runHandler' && | ||
| String(spanAttributes?.['next.span_name']).startsWith(ATTR_NEXT_PAGES_API_ROUTE_TYPE) | ||
| ) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server'); | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route'); | ||
| } | ||
|
|
||
| // Make sure middleware spans get the right op | ||
| if (spanAttributes?.['next.span_type'] === 'Middleware.execute') { | ||
| if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute') { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware'); | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url'); | ||
|
|
||
|
|
@@ -119,8 +132,8 @@ export function init(options: VercelEdgeOptions = {}): void { | |
| // The otel auto inference will clobber the transaction name because the span has an http.target | ||
| if ( | ||
| event.type === 'transaction' && | ||
| event.contexts?.trace?.data?.['next.span_type'] === 'Middleware.execute' && | ||
| event.contexts?.trace?.data?.['next.span_name'] | ||
| event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute' && | ||
| event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_NAME] !== undefined | ||
| ) { | ||
| if (event.transaction) { | ||
| // 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 { | |
| } | ||
| } | ||
|
|
||
| // Backfill the transaction name for api route pages because we removed it from the wrapper | ||
| if ( | ||
| event.type === 'transaction' && | ||
| event.contexts?.trace?.data?.[ATTR_NEXT_SPAN_TYPE] === 'Node.runHandler' && | ||
| String(event.contexts.trace.data['next.span_name']).startsWith(ATTR_NEXT_PAGES_API_ROUTE_TYPE) | ||
| ) { | ||
| let path = String(event.contexts.trace.data['next.span_name']).replace(ATTR_NEXT_PAGES_API_ROUTE_TYPE, '').trim(); | ||
| // Set transaction name on isolation scope to ensure parameterized routes are used | ||
| // The HTTP server integration sets it on isolation scope, so we need to match that | ||
| const method = event.request?.method || 'GET'; | ||
| path = path ?? event.request?.url ?? '/'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Nullish coalescing won't handle empty path stringThe |
||
| event.transaction = `${method} ${path}`; | ||
| } | ||
|
|
||
| setUrlProcessingMetadata(event); | ||
| }); | ||
|
|
||
|
|
||
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched to a blocking await here before throwing which should work better than calling
waitUntilthen throwing as the runtime can shutdown as soon as an unhandled error like these are thrown.This matches the logic we have in the other API handler wrapper.