-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nextjs): Make request data available to tracesSampler for edge middleware root spans
#22232
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
s1gr1d
wants to merge
1
commit into
develop
Choose a base branch
from
sig/nextjs-edge-middleware
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.
+365
−49
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
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
38 changes: 38 additions & 0 deletions
38
packages/nextjs/src/common/utils/forkIsolationScopeForRootSpan.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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { context } from '@opentelemetry/api'; | ||
| import type { Span, SpanAttributes } from '@sentry/core'; | ||
| import { | ||
| getCapturedScopesOnSpan, | ||
| getCurrentScope, | ||
| getIsolationScope, | ||
| getRootSpan, | ||
| setCapturedScopesOnSpan, | ||
| } from '@sentry/core'; | ||
| import { getScopesFromContext } from '@sentry/opentelemetry'; | ||
| import { ATTR_NEXT_SPAN_TYPE } from '../nextSpanAttributes'; | ||
|
|
||
| /** | ||
| * Forks the isolation scope for `BaseServer.handleRequest` / `Middleware.execute` root spans so that request-scoped | ||
| * data (e.g. `normalizedRequest`) stays isolated per request. | ||
| */ | ||
| export function maybeForkIsolationScopeForRootSpan(span: Span, spanAttributes: SpanAttributes | undefined): void { | ||
| const spanType = spanAttributes?.[ATTR_NEXT_SPAN_TYPE]; | ||
| if (spanType !== 'BaseServer.handleRequest' && spanType !== 'Middleware.execute') { | ||
| return; | ||
| } | ||
|
|
||
| if (span !== getRootSpan(span)) { | ||
| return; | ||
| } | ||
|
|
||
| const scopes = getCapturedScopesOnSpan(span); | ||
|
|
||
| const isolationScope = (scopes.isolationScope || getIsolationScope()).clone(); | ||
| const scope = scopes.scope || getCurrentScope(); | ||
|
|
||
| const currentScopesPointer = getScopesFromContext(context.active()); | ||
| if (currentScopesPointer) { | ||
| currentScopesPointer.isolationScope = isolationScope; | ||
| } | ||
|
|
||
| setCapturedScopesOnSpan(span, scope, isolationScope); | ||
| } |
48 changes: 48 additions & 0 deletions
48
packages/nextjs/src/common/utils/getNormalizedRequestFromAttributes.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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { | ||
| HTTP_METHOD, | ||
| HTTP_REQUEST_METHOD, | ||
| HTTP_TARGET, | ||
| HTTP_URL, | ||
| URL_FULL, | ||
| URL_PATH, | ||
| URL_QUERY, | ||
| } from '@sentry/conventions/attributes'; | ||
| import type { RequestEventData, SpanAttributes } from '@sentry/core'; | ||
|
|
||
| /** | ||
| * Builds a partial `normalizedRequest` from OTel HTTP span attributes. | ||
| * Only method, URL, and query string can be derived — headers are not available as span attributes. | ||
| */ | ||
| export function getNormalizedRequestFromAttributes(attributes: SpanAttributes): RequestEventData | undefined { | ||
| // eslint-disable-next-line typescript/no-deprecated | ||
| const method = attributes[HTTP_REQUEST_METHOD] || attributes[HTTP_METHOD]; | ||
|
|
||
| // eslint-disable-next-line typescript/no-deprecated | ||
| const url = attributes[URL_FULL] || attributes[HTTP_URL] || attributes[URL_PATH] || attributes[HTTP_TARGET]; | ||
|
|
||
| if (typeof method !== 'string' && typeof url !== 'string') { | ||
| return undefined; | ||
| } | ||
|
|
||
| const normalizedRequest: RequestEventData = {}; | ||
|
|
||
| if (typeof method === 'string') { | ||
| normalizedRequest.method = method; | ||
| } | ||
|
|
||
| if (typeof url === 'string') { | ||
| normalizedRequest.url = url; | ||
|
|
||
| const queryFromAttribute = attributes[URL_QUERY]; | ||
| if (typeof queryFromAttribute === 'string') { | ||
| normalizedRequest.query_string = queryFromAttribute; | ||
| } else { | ||
| const queryIndex = url.indexOf('?'); | ||
| if (queryIndex !== -1) { | ||
| normalizedRequest.query_string = url.slice(queryIndex + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return normalizedRequest; | ||
| } |
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
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
107 changes: 107 additions & 0 deletions
107
packages/nextjs/test/common/utils/getNormalizedRequestFromAttributes.test.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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { getNormalizedRequestFromAttributes } from '../../../src/common/utils/getNormalizedRequestFromAttributes'; | ||
|
|
||
| describe('getNormalizedRequestFromAttributes', () => { | ||
| it('builds a request from `http.method` and `http.target` (edge middleware sample-time attributes)', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.method': 'GET', | ||
| 'http.target': '/api/endpoint-behind-middleware?query=123', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'GET', | ||
| url: '/api/endpoint-behind-middleware?query=123', | ||
| query_string: 'query=123', | ||
| }); | ||
| }); | ||
|
|
||
| it('prefers the new `http.request.method` and `url.full` attributes', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.request.method': 'POST', | ||
| 'http.method': 'GET', | ||
| 'url.full': 'https://example.com/foo?a=1', | ||
| 'http.target': '/foo?a=1', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'POST', | ||
| url: 'https://example.com/foo?a=1', | ||
| query_string: 'a=1', | ||
| }); | ||
| }); | ||
|
|
||
| it('prefers the `url.query` attribute over parsing the url', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.method': 'GET', | ||
| 'url.full': 'https://example.com/foo?a=1&b=2', | ||
| 'url.query': 'a=1&b=2', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'GET', | ||
| url: 'https://example.com/foo?a=1&b=2', | ||
| query_string: 'a=1&b=2', | ||
| }); | ||
| }); | ||
|
|
||
| it('omits `query_string` when there is no query', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.method': 'GET', | ||
| 'http.target': '/foo', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'GET', | ||
| url: '/foo', | ||
| }); | ||
| }); | ||
|
|
||
| it('builds a request when only the method is available', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.method': 'GET', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ method: 'GET' }); | ||
| }); | ||
|
|
||
| it('builds a request when only the url is available', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.target': '/foo?a=1', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ url: '/foo?a=1', query_string: 'a=1' }); | ||
| }); | ||
|
|
||
| it('falls back to `url.path` when `url.full` and `http.url` are absent', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.request.method': 'GET', | ||
| 'url.path': '/api/resource', | ||
| 'url.query': 'page=2', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'GET', | ||
| url: '/api/resource', | ||
| query_string: 'page=2', | ||
| }); | ||
| }); | ||
|
|
||
| it('prefers `url.full` over `url.path`', () => { | ||
| const normalizedRequest = getNormalizedRequestFromAttributes({ | ||
| 'http.request.method': 'GET', | ||
| 'url.full': 'https://example.com/api/resource?page=2', | ||
| 'url.path': '/api/resource', | ||
| }); | ||
|
|
||
| expect(normalizedRequest).toEqual({ | ||
| method: 'GET', | ||
| url: 'https://example.com/api/resource?page=2', | ||
| query_string: 'page=2', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns undefined when neither method nor url is present', () => { | ||
| expect(getNormalizedRequestFromAttributes({})).toBeUndefined(); | ||
| expect(getNormalizedRequestFromAttributes({ 'next.span_type': 'Middleware.execute' })).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Bug: In warm serverless environments, stale
normalizedRequestdata from a previous request can be reused because the global default isolation scope is not reset between requests.Severity: MEDIUM
Suggested Fix
The global default isolation scope should be cleared or reset at the beginning of each request in serverless environments. This ensures that
buildContextWithSentryScopesstarts with a clean scope and does not reuse stale data from previous invocations in the same warm worker.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.