Skip to content

Commit 68a27e5

Browse files
committed
ref: Set normalizedRequest instead of request various places
The request data integration prefers this over `request`, we want to get rid of `request` in v9. Part of #14298
1 parent f4c5900 commit 68a27e5

13 files changed

+52
-40
lines changed

packages/astro/src/server/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async function instrumentRequest(
111111
getCurrentScope().setSDKProcessingMetadata({
112112
// We store the request on the current scope, not isolation scope,
113113
// because we may have multiple requests nested inside each other
114-
request: isDynamicPageRequest ? winterCGRequestToRequestData(request) : { method, url: request.url },
114+
normalizedRequest: isDynamicPageRequest ? winterCGRequestToRequestData(request) : { method, url: request.url },
115115
});
116116

117117
if (options.trackClientIp && isDynamicPageRequest) {

packages/bun/src/integrations/bunserver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
startSpan,
1010
withIsolationScope,
1111
} from '@sentry/core';
12-
import type { IntegrationFn, SpanAttributes } from '@sentry/types';
12+
import type { IntegrationFn, Request, SpanAttributes } from '@sentry/types';
1313
import { getSanitizedUrlString, parseUrl } from '@sentry/utils';
1414

1515
const INTEGRATION_NAME = 'BunServer';
@@ -76,11 +76,11 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
7676
const url = getSanitizedUrlString(parsedUrl);
7777

7878
isolationScope.setSDKProcessingMetadata({
79-
request: {
79+
normalizedRequest: {
8080
url,
8181
method: request.method,
8282
headers: request.headers.toJSON(),
83-
},
83+
} satisfies Request,
8484
});
8585

8686
return continueTrace(

packages/cloudflare/src/scope-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ export function addCultureContext(scope: Scope, cf: IncomingRequestCfProperties)
2525
* Set request data on scope
2626
*/
2727
export function addRequest(scope: Scope, request: Request): void {
28-
scope.setSDKProcessingMetadata({ request: winterCGRequestToRequestData(request) });
28+
scope.setSDKProcessingMetadata({ normalizedRequest: winterCGRequestToRequestData(request) });
2929
}

packages/nextjs/src/common/captureRequestError.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { captureException, withScope } from '@sentry/core';
2+
import type { Request } from '@sentry/types';
3+
import { headersToDict } from '@sentry/utils';
24

35
type RequestInfo = {
46
path: string;
@@ -18,10 +20,10 @@ type ErrorContext = {
1820
export function captureRequestError(error: unknown, request: RequestInfo, errorContext: ErrorContext): void {
1921
withScope(scope => {
2022
scope.setSDKProcessingMetadata({
21-
request: {
22-
headers: request.headers,
23+
normalizedRequest: {
24+
headers: headersToDict(request.headers),
2325
method: request.method,
24-
},
26+
} satisfies Request,
2527
});
2628

2729
scope.setContext('nextjs', {

packages/nextjs/src/common/withServerActionInstrumentation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
startSpan,
1010
withIsolationScope,
1111
} from '@sentry/core';
12+
import type { Request } from '@sentry/types';
1213
import { logger, vercelWaitUntil } from '@sentry/utils';
1314

1415
import { DEBUG_BUILD } from './debug-build';
@@ -89,9 +90,9 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
8990

9091
isolationScope.setTransactionName(`serverAction/${serverActionName}`);
9192
isolationScope.setSDKProcessingMetadata({
92-
request: {
93+
normalizedRequest: {
9394
headers: fullHeadersObject,
94-
},
95+
} satisfies Request,
9596
});
9697

9798
return continueTrace(

packages/nextjs/src/common/wrapGenerationFunctionWithSentry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
withIsolationScope,
1515
withScope,
1616
} from '@sentry/core';
17-
import type { WebFetchHeaders } from '@sentry/types';
17+
import type { Request, WebFetchHeaders } from '@sentry/types';
1818
import { propagationContextFromHeaders, uuid4, winterCGHeadersToDict } from '@sentry/utils';
1919

2020
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
@@ -68,9 +68,9 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
6868
scope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`);
6969

7070
isolationScope.setSDKProcessingMetadata({
71-
request: {
71+
normalizedRequest: {
7272
headers: headersDict,
73-
},
73+
} satisfies Request,
7474
});
7575

7676
const activeSpan = getActiveSpan();

packages/nextjs/src/common/wrapMiddlewareWithSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
3636

3737
if (req instanceof Request) {
3838
isolationScope.setSDKProcessingMetadata({
39-
request: winterCGRequestToRequestData(req),
39+
normalizedRequest: winterCGRequestToRequestData(req),
4040
});
4141
spanName = `middleware ${req.method} ${new URL(req.url).pathname}`;
4242
spanSource = 'url';

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
withIsolationScope,
1414
withScope,
1515
} from '@sentry/core';
16-
16+
import type { Request as SentryRequest } from '@sentry/types';
1717
import type { RouteHandlerContext } from './types';
1818

1919
import { propagationContextFromHeaders, winterCGHeadersToDict } from '@sentry/utils';
@@ -64,10 +64,10 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
6464
);
6565
scope.setPropagationContext(incomingPropagationContext);
6666
scope.setSDKProcessingMetadata({
67-
request: {
67+
normalizedRequest: {
6868
method,
6969
headers: completeHeadersDict,
70-
},
70+
} satisfies SentryRequest,
7171
});
7272
}
7373

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
withIsolationScope,
1414
withScope,
1515
} from '@sentry/core';
16+
import type { Request as SentryRequest } from '@sentry/types';
1617
import { propagationContextFromHeaders, uuid4, vercelWaitUntil, winterCGHeadersToDict } from '@sentry/utils';
1718

1819
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
@@ -49,9 +50,9 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
4950
const headersDict = context.headers ? winterCGHeadersToDict(context.headers) : undefined;
5051

5152
isolationScope.setSDKProcessingMetadata({
52-
request: {
53+
normalizedRequest: {
5354
headers: headersDict,
54-
},
55+
} satisfies SentryRequest,
5556
});
5657

5758
return withIsolationScope(isolationScope, () => {

packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
3232

3333
if (req instanceof Request) {
3434
isolationScope.setSDKProcessingMetadata({
35-
request: winterCGRequestToRequestData(req),
35+
normalizedRequest: winterCGRequestToRequestData(req),
3636
});
3737
currentScope.setTransactionName(`${req.method} ${parameterizedRoute}`);
3838
} else {

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { PolymorphicRequest, Request, SanitizedRequestData } from '@sentry/
1010
import {
1111
getBreadcrumbLogLevelFromHttpStatusCode,
1212
getSanitizedUrlString,
13+
headersToDict,
1314
logger,
1415
parseUrl,
1516
stripUrlQueryAndFragment,
@@ -455,20 +456,3 @@ function extractQueryParams(req: IncomingMessage): string | undefined {
455456
return undefined;
456457
}
457458
}
458-
459-
function headersToDict(reqHeaders: Record<string, string | string[] | undefined>): Record<string, string> {
460-
const headers: Record<string, string> = Object.create(null);
461-
462-
try {
463-
Object.entries(reqHeaders).forEach(([key, value]) => {
464-
if (typeof value === 'string') {
465-
headers[key] = value;
466-
}
467-
});
468-
} catch (e) {
469-
DEBUG_BUILD &&
470-
logger.warn('Sentry failed extracting headers from a request object. If you see this, please file an issue.');
471-
}
472-
473-
return headers;
474-
}

packages/sveltekit/src/server/handle.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
131131
return withIsolationScope(isolationScope => {
132132
// We only call continueTrace in the initial top level request to avoid
133133
// creating a new root span for the sub request.
134-
isolationScope.setSDKProcessingMetadata({ request: winterCGRequestToRequestData(input.event.request.clone()) });
134+
isolationScope.setSDKProcessingMetadata({
135+
normalizedRequest: winterCGRequestToRequestData(input.event.request.clone()),
136+
});
135137
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
136138
});
137139
};
@@ -167,7 +169,9 @@ async function instrumentHandle(
167169
name: routeName,
168170
},
169171
async (span?: Span) => {
170-
getCurrentScope().setSDKProcessingMetadata({ request: winterCGRequestToRequestData(event.request.clone()) });
172+
getCurrentScope().setSDKProcessingMetadata({
173+
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
174+
});
171175
const res = await resolve(event, {
172176
transformPageChunk: addSentryCodeToPage(options),
173177
});

packages/utils/src/requestdata.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,30 @@ export function winterCGHeadersToDict(winterCGHeaders: WebFetchHeaders): Record<
434434
return headers;
435435
}
436436

437+
/**
438+
* Convert common request headers to a simple dictionary.
439+
*/
440+
export function headersToDict(reqHeaders: Record<string, string | string[] | undefined>): Record<string, string> {
441+
const headers: Record<string, string> = Object.create(null);
442+
443+
try {
444+
Object.entries(reqHeaders).forEach(([key, value]) => {
445+
if (typeof value === 'string') {
446+
headers[key] = value;
447+
}
448+
});
449+
} catch (e) {
450+
DEBUG_BUILD &&
451+
logger.warn('Sentry failed extracting headers from a request object. If you see this, please file an issue.');
452+
}
453+
454+
return headers;
455+
}
456+
437457
/**
438458
* Converts a `Request` object that implements the `Web Fetch API` (https://developer.mozilla.org/en-US/docs/Web/API/Headers) into the format that the `RequestData` integration understands.
439459
*/
440-
export function winterCGRequestToRequestData(req: WebFetchRequest): PolymorphicRequest {
460+
export function winterCGRequestToRequestData(req: WebFetchRequest): Request {
441461
const headers = winterCGHeadersToDict(req.headers);
442462
return {
443463
method: req.method,

0 commit comments

Comments
 (0)