Skip to content

Commit d566af5

Browse files
committed
edge hurts
1 parent be665a3 commit d566af5

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

dev-packages/e2e-tests/test-applications/nextjs-15/tests/route-handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ test('Should create a transaction for edge route handlers', async ({ request })
3434
expect(routehandlerTransaction.contexts?.trace?.status).toBe('ok');
3535
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
3636
// todo: check if we can set request headers for edge on sdkProcessingMetadata
37-
// expect(routehandlerTransaction.contexts?.trace?.data?.['http.request.header.x-charly']).toBe('gomez');
37+
// expect(routehandlerTransaction.contexts?.trace?.data?.['http.request.header.x_charly']).toBe('gomez');
3838
});

packages/nextjs/src/edge/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
applySdkMetadata,
3+
getClient,
34
getGlobalScope,
5+
getIsolationScope,
46
getRootSpan,
57
GLOBAL_OBJ,
68
registerSpanErrorInstrumentation,
@@ -13,6 +15,7 @@ import {
1315
} from '@sentry/core';
1416
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
1517
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
18+
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
1619
import { isBuild } from '../common/utils/isBuild';
1720
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1821
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';
@@ -59,6 +62,8 @@ export function init(options: VercelEdgeOptions = {}): void {
5962

6063
client?.on('spanStart', span => {
6164
const spanAttributes = spanToJSON(span).data;
65+
const rootSpan = getRootSpan(span);
66+
const isRootSpan = span === rootSpan;
6267

6368
// Mark all spans generated by Next.js as 'auto'
6469
if (spanAttributes?.['next.span_type'] !== undefined) {
@@ -70,6 +75,13 @@ export function init(options: VercelEdgeOptions = {}): void {
7075
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');
7176
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');
7277
}
78+
79+
const shouldSendDefaultPii = getClient()?.getOptions().sendDefaultPii ?? false;
80+
if (shouldSendDefaultPii && isRootSpan) {
81+
// todo: check if we can set request headers for edge on sdkProcessingMetadata
82+
const headers = getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers;
83+
addHeadersAsAttributes(headers, rootSpan);
84+
}
7385
});
7486

7587
// Use the preprocessEvent hook instead of an event processor, so that the users event processors receive the most

packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
winterCGRequestToRequestData,
1414
withIsolationScope,
1515
} from '@sentry/core';
16+
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
1617
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1718
import type { EdgeRouteHandler } from './types';
1819

@@ -31,13 +32,14 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
3132
const req: unknown = args[0];
3233
const currentScope = getCurrentScope();
3334

34-
const headerAttributes: Record<string, string> = {};
35+
let headerAttributes: Record<string, string> = {};
3536

3637
if (req instanceof Request) {
3738
isolationScope.setSDKProcessingMetadata({
3839
normalizedRequest: winterCGRequestToRequestData(req),
3940
});
4041
currentScope.setTransactionName(`${req.method} ${parameterizedRoute}`);
42+
headerAttributes = addHeadersAsAttributes(req.headers);
4143
} else {
4244
currentScope.setTransactionName(`handler (${parameterizedRoute})`);
4345
}

0 commit comments

Comments
 (0)