Skip to content

Commit 1a8666d

Browse files
Lms24claude
andcommitted
ref(nextjs): Look up the request through the httpServerRequest hook
Instead of keeping the incoming request on a hidden field of the isolation scope in core, the Next.js SDK keeps it in its own map, filled from the `httpServerRequest` hook that the Node.js SDK emits for every incoming request. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1930c84 commit 1a8666d

5 files changed

Lines changed: 19 additions & 40 deletions

File tree

‎packages/core/src/index.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ export {
123123
INTERNAL_setSegmentNameSourceIfSegment,
124124
} from './utils/spanUtils';
125125
export { _setSpanForScope as _INTERNAL_setSpanForScope } from './utils/spanOnScope';
126-
export {
127-
_getIncomingRequestForScope as _INTERNAL_getIncomingRequestForScope,
128-
_setIncomingRequestForScope as _INTERNAL_setIncomingRequestForScope,
129-
} from './utils/incomingRequestOnScope';
130126
export { parseSampleRate } from './utils/parseSampleRate';
131127
export { applySdkMetadata } from './utils/sdkMetadata';
132128
export { getTraceData } from './utils/traceData';

‎packages/core/src/integrations/http/server-subscription.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
import { patchRequestToCaptureBody } from './patch-request-to-capture-body';
3535
import { getUrlFragment, getUrlQuery, parseStringToURLObject, stripUrlQueryAndFragment } from '../../utils/url';
3636
import { recordRequestSession } from './record-request-session';
37-
import { _setIncomingRequestForScope } from '../../utils/incomingRequestOnScope';
3837
import { generateSpanId, generateTraceId } from '../../utils/propagationContext';
3938
import { continueTrace, startSpanManual } from '../../tracing/trace';
4039
import { getSpanStatusFromHttpCode, SPAN_STATUS_ERROR } from '../../tracing';
@@ -154,7 +153,6 @@ export function instrumentServer(options: HttpInstrumentationOptions, server: Ht
154153

155154
// Update the isolation scope, isolate this request
156155
isolationScope.setSDKProcessingMetadata({ normalizedRequest, ipAddress });
157-
_setIncomingRequestForScope(isolationScope, request);
158156

159157
// attempt to update the scope's `transactionName` based on the request
160158
// URL. Ideally, framework instrumentations coming after the

‎packages/core/src/utils/incomingRequestOnScope.ts‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎packages/nextjs/src/server/serverSpanHooks.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {
99
URL_QUERY,
1010
} from '@sentry/conventions/attributes';
1111
import { MIDDLEWARE } from '@sentry/conventions/op';
12-
import type { Client, Options, Span } from '@sentry/core';
12+
import type { Client, Options, Scope, Span } from '@sentry/core';
1313
import {
14-
_INTERNAL_getIncomingRequestForScope,
1514
getIsolationScope,
1615
getRootSpan,
1716
GLOBAL_OBJ,
@@ -41,6 +40,10 @@ import {
4140
const NEXTJS_ERROR_PAGE_ROUTES = ['/500', '/_error'];
4241
const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta');
4342

43+
// Spans of Next.js start without access to the request they belong to, so the request is looked up through the
44+
// isolation scope of the request
45+
const requestsByIsolationScope = new WeakMap<Scope, object>();
46+
4447
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
4548
_sentryNextJsVersion?: string;
4649
};
@@ -85,6 +88,12 @@ export function addNextjsServerSpanHooks(client: Client): void {
8588
}
8689
});
8790

91+
client.on('httpServerRequest', request => {
92+
if (typeof request === 'object' && request) {
93+
requestsByIsolationScope.set(getIsolationScope(), request);
94+
}
95+
});
96+
8897
client.on('beforeSpanStart', ({ attributes }) => {
8998
if (isProxyInvocationSpan(attributes)) {
9099
attributes[TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION] = true;
@@ -137,7 +146,7 @@ function isProxyInvocationSpan(spanAttributes: Record<string, unknown>): boolean
137146

138147
// Next.js sets this flag on the request before the call that only runs the proxy, and resets it before rendering
139148
const request: { [NEXT_REQUEST_META]?: { middlewareInvoke?: boolean } } | undefined =
140-
_INTERNAL_getIncomingRequestForScope(getIsolationScope());
149+
requestsByIsolationScope.get(getIsolationScope());
141150
return request?.[NEXT_REQUEST_META]?.middlewareInvoke === true;
142151
}
143152

‎packages/nextjs/test/server/serverSpanHooks.test.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
SENTRY_SEGMENT_NAME_SOURCE,
77
} from '@sentry/conventions/attributes';
88
import { MIDDLEWARE } from '@sentry/conventions/op';
9-
import type { Client, SpanAttributes } from '@sentry/core';
9+
import type { Client } from '@sentry/core';
1010
import {
11-
_INTERNAL_setIncomingRequestForScope,
1211
addChildSpanToSpan,
1312
GLOBAL_OBJ,
1413
SentrySpan,
@@ -194,21 +193,19 @@ describe('addNextjsServerSpanHooks beforeSpanStart', () => {
194193
function isRequestSpanIgnored(nextVersion: string | undefined, middlewareInvoke: boolean | undefined): boolean {
195194
globalWithInjectedValues._sentryNextJsVersion = nextVersion;
196195

197-
let beforeSpanStart: ((data: { name: string | undefined; attributes: SpanAttributes }) => void) | undefined;
196+
const hooks: Record<string, (...args: unknown[]) => void> = {};
198197
addNextjsServerSpanHooks({
199-
on: (hook: string, callback: typeof beforeSpanStart) => {
200-
if (hook === 'beforeSpanStart') {
201-
beforeSpanStart = callback;
202-
}
198+
on: (hook: string, callback: (...args: unknown[]) => void) => {
199+
hooks[hook] = callback;
203200
},
204201
getOptions: () => ({}),
205202
} as unknown as Client);
206203

207204
const request = { [Symbol.for('NextInternalRequestMeta')]: { middlewareInvoke } };
208205
const spanStartData = { name: 'GET', attributes: { [ATTR_NEXT_SPAN_TYPE]: 'BaseServer.handleRequest' } };
209-
withIsolationScope(isolationScope => {
210-
_INTERNAL_setIncomingRequestForScope(isolationScope, request);
211-
beforeSpanStart?.(spanStartData);
206+
withIsolationScope(() => {
207+
hooks['httpServerRequest']?.(request, {}, {});
208+
hooks['beforeSpanStart']?.(spanStartData);
212209
});
213210
return shouldIgnoreSpan(
214211
{ description: spanStartData.name, attributes: spanStartData.attributes },

0 commit comments

Comments
 (0)