-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Remove tracing from generation function template #18733
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
Open
chargome
wants to merge
3
commits into
develop
Choose a base branch
from
cg/JS-1424/remove-tracing-from-wrapgenerationfunctionwithsentry
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−100
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 48 additions & 98 deletions
146
packages/nextjs/src/common/wrapGenerationFunctionWithSentry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,133 +1,83 @@ | ||
| import type { RequestEventData, WebFetchHeaders } from '@sentry/core'; | ||
| import type { RequestEventData } from '@sentry/core'; | ||
| import { | ||
| captureException, | ||
| getActiveSpan, | ||
| getCapturedScopesOnSpan, | ||
| getRootSpan, | ||
| getIsolationScope, | ||
| handleCallbackErrors, | ||
| propagationContextFromHeaders, | ||
| Scope, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
| setCapturedScopesOnSpan, | ||
| SPAN_STATUS_ERROR, | ||
| SPAN_STATUS_OK, | ||
| startSpanManual, | ||
| winterCGHeadersToDict, | ||
| withIsolationScope, | ||
| withScope, | ||
| } from '@sentry/core'; | ||
| import type { GenerationFunctionContext } from '../common/types'; | ||
| import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils'; | ||
| import { TRANSACTION_ATTR_SENTRY_TRACE_BACKFILL } from './span-attributes-with-logic-attached'; | ||
| import { commonObjectToIsolationScope, commonObjectToPropagationContext } from './utils/tracingUtils'; | ||
| import { flushSafelyWithTimeout, waitUntil } from './utils/responseEnd'; | ||
|
|
||
| /** | ||
| * Wraps a generation function (e.g. generateMetadata) with Sentry error and performance instrumentation. | ||
| * Wraps a generation function (e.g. generateMetadata) with Sentry error instrumentation. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => any>( | ||
| generationFunction: F, | ||
| context: GenerationFunctionContext, | ||
| ): F { | ||
| const { requestAsyncStorage, componentRoute, componentType, generationFunctionIdentifier } = context; | ||
| return new Proxy(generationFunction, { | ||
| apply: (originalFunction, thisArg, args) => { | ||
| const requestTraceId = getActiveSpan()?.spanContext().traceId; | ||
| let headers: WebFetchHeaders | undefined = undefined; | ||
| // We try-catch here just in case anything goes wrong with the async storage here goes wrong since it is Next.js internal API | ||
| const isolationScope = getIsolationScope(); | ||
|
|
||
| let headers = undefined; | ||
| // We try-catch here just in case anything goes wrong with the async storage since it is Next.js internal API | ||
| try { | ||
| headers = requestAsyncStorage?.getStore()?.headers; | ||
| headers = context.requestAsyncStorage?.getStore()?.headers; | ||
| } catch { | ||
| /** empty */ | ||
| } | ||
|
|
||
| const isolationScope = commonObjectToIsolationScope(headers); | ||
|
|
||
| const activeSpan = getActiveSpan(); | ||
| if (activeSpan) { | ||
| const rootSpan = getRootSpan(activeSpan); | ||
| const { scope } = getCapturedScopesOnSpan(rootSpan); | ||
| setCapturedScopesOnSpan(rootSpan, scope ?? new Scope(), isolationScope); | ||
| } | ||
|
|
||
| const headersDict = headers ? winterCGHeadersToDict(headers) : undefined; | ||
|
|
||
| return withIsolationScope(isolationScope, () => { | ||
| return withScope(scope => { | ||
| scope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`); | ||
| isolationScope.setSDKProcessingMetadata({ | ||
| normalizedRequest: { | ||
| headers: headersDict, | ||
| } satisfies RequestEventData, | ||
| }); | ||
|
|
||
| isolationScope.setSDKProcessingMetadata({ | ||
| normalizedRequest: { | ||
| headers: headersDict, | ||
| } satisfies RequestEventData, | ||
| }); | ||
| return handleCallbackErrors( | ||
| () => originalFunction.apply(thisArg, args), | ||
| error => { | ||
| const span = getActiveSpan(); | ||
| const { componentRoute, componentType, generationFunctionIdentifier } = context; | ||
| let shouldCapture = true; | ||
| isolationScope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`); | ||
|
|
||
| const activeSpan = getActiveSpan(); | ||
| if (activeSpan) { | ||
| const rootSpan = getRootSpan(activeSpan); | ||
| const sentryTrace = headersDict?.['sentry-trace']; | ||
| if (sentryTrace) { | ||
| rootSpan.setAttribute(TRANSACTION_ATTR_SENTRY_TRACE_BACKFILL, sentryTrace); | ||
| if (span) { | ||
| if (isNotFoundNavigationError(error)) { | ||
| // We don't want to report "not-found"s | ||
| shouldCapture = false; | ||
| span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' }); | ||
| } else if (isRedirectNavigationError(error)) { | ||
| // We don't want to report redirects | ||
| shouldCapture = false; | ||
| span.setStatus({ code: SPAN_STATUS_OK }); | ||
| } else { | ||
| span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); | ||
| } | ||
| } | ||
|
|
||
| const propagationContext = commonObjectToPropagationContext( | ||
| headers, | ||
| propagationContextFromHeaders(headersDict?.['sentry-trace'], headersDict?.['baggage']), | ||
| ); | ||
|
|
||
| if (requestTraceId) { | ||
| propagationContext.traceId = requestTraceId; | ||
| } | ||
|
|
||
| scope.setPropagationContext(propagationContext); | ||
|
|
||
| return startSpanManual( | ||
| { | ||
| op: 'function.nextjs', | ||
| name: `${componentType}.${generationFunctionIdentifier} (${componentRoute})`, | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs', | ||
| 'sentry.nextjs.ssr.function.type': generationFunctionIdentifier, | ||
| 'sentry.nextjs.ssr.function.route': componentRoute, | ||
| }, | ||
| }, | ||
| span => { | ||
| return handleCallbackErrors( | ||
| () => originalFunction.apply(thisArg, args), | ||
| err => { | ||
| // When you read this code you might think: "Wait a minute, shouldn't we set the status on the root span too?" | ||
| // The answer is: "No." - The status of the root span is determined by whatever status code Next.js decides to put on the response. | ||
| if (isNotFoundNavigationError(err)) { | ||
| // We don't want to report "not-found"s | ||
| span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' }); | ||
| getRootSpan(span).setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' }); | ||
| } else if (isRedirectNavigationError(err)) { | ||
| // We don't want to report redirects | ||
| span.setStatus({ code: SPAN_STATUS_OK }); | ||
| } else { | ||
| span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); | ||
| getRootSpan(span).setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); | ||
| captureException(err, { | ||
| mechanism: { | ||
| handled: false, | ||
| type: 'auto.function.nextjs.generation_function', | ||
| data: { | ||
| function: generationFunctionIdentifier, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| () => { | ||
| span.end(); | ||
| if (shouldCapture) { | ||
| captureException(error, { | ||
| mechanism: { | ||
| handled: false, | ||
| type: 'auto.function.nextjs.generation_function', | ||
| data: { | ||
| function: generationFunctionIdentifier, | ||
| }, | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| () => { | ||
| waitUntil(flushSafelyWithTimeout()); | ||
| }, | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Navigation errors incorrectly captured when no span exists
High Severity
The
shouldCapturevariable defaults totrue, but the logic that sets it tofalsefor not-found and redirect navigation errors is nested insideif (span). WhengetActiveSpan()returnsundefined(no active span), the navigation error checks are skipped entirely, causingcaptureExceptionto be called for these expected navigation behaviors. This results in erroneous error reports to Sentry for redirects and 404s that should be silently ignored.