Skip to content

Commit 9ae209c

Browse files
committed
fix(nextjs): Set span source to url for middlewares
1 parent c533d21 commit 9ae209c

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

dev-packages/e2e-tests/test-applications/nextjs-app-dir/tests/middleware.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test('Should create a transaction for middleware', async ({ request }) => {
1414
expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
1515
expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware');
1616
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
17+
expect(middlewareTransaction.transaction_info?.source).toBe('url');
1718

1819
// Assert that isolation scope works properly
1920
expect(middlewareTransaction.tags?.['my-isolated-tag']).toBe(true);
@@ -38,6 +39,7 @@ test('Faulty middlewares', async ({ request }) => {
3839
expect(middlewareTransaction.contexts?.trace?.status).toBe('unknown_error');
3940
expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware');
4041
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
42+
expect(middlewareTransaction.transaction_info?.source).toBe('url');
4143
});
4244

4345
await test.step('should record exceptions', async () => {

packages/nextjs/src/common/wrapMiddlewareWithSentry.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
3232
const currentScope = getCurrentScope();
3333

3434
let spanName: string;
35-
let spanOrigin: TransactionSource;
35+
let spanSource: TransactionSource;
3636

3737
if (req instanceof Request) {
3838
isolationScope.setSDKProcessingMetadata({
3939
request: winterCGRequestToRequestData(req),
4040
});
4141
spanName = `middleware ${req.method} ${new URL(req.url).pathname}`;
42-
spanOrigin = 'url';
42+
spanSource = 'url';
4343
} else {
4444
spanName = 'middleware';
45-
spanOrigin = 'component';
45+
spanSource = 'component';
4646
}
4747

4848
currentScope.setTransactionName(spanName);
@@ -53,7 +53,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
5353
// If there is an active span, it likely means that the automatic Next.js OTEL instrumentation worked and we can
5454
// rely on that for parameterization.
5555
spanName = 'middleware';
56-
spanOrigin = 'component';
56+
spanSource = 'component';
5757

5858
const rootSpan = getRootSpan(activeSpan);
5959
if (rootSpan) {
@@ -66,7 +66,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
6666
name: spanName,
6767
op: 'http.server.middleware',
6868
attributes: {
69-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanOrigin,
69+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanSource,
7070
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs.wrapMiddlewareWithSentry',
7171
},
7272
},

packages/nextjs/src/edge/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
SEMANTIC_ATTRIBUTE_SENTRY_OP,
3+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
35
applySdkMetadata,
46
getRootSpan,
57
registerSpanErrorInstrumentation,
@@ -52,9 +54,15 @@ export function init(options: VercelEdgeOptions = {}): void {
5254
client?.on('spanStart', span => {
5355
const spanAttributes = spanToJSON(span).data;
5456

57+
// Mark all spans generated by Next.js as 'auto'
58+
if (spanAttributes?.['next.span_type'] !== undefined) {
59+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
60+
}
61+
5562
// Make sure middleware spans get the right op
5663
if (spanAttributes?.['next.span_type'] === 'Middleware.execute') {
5764
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');
65+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');
5866
}
5967
});
6068

0 commit comments

Comments
 (0)