Skip to content

Commit

Permalink
fix(nextjs): Set span source to url for middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Oct 24, 2024
1 parent c533d21 commit f2b019f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions dev-packages/e2e-tests/source-map
Submodule source-map added at 7c40f8
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('Should create a transaction for middleware', async ({ request }) => {
expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
expect(middlewareTransaction.transaction_info?.source).toBe('url');

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

await test.step('should record exceptions', async () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/nextjs/src/common/wrapMiddlewareWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
const currentScope = getCurrentScope();

let spanName: string;
let spanOrigin: TransactionSource;
let spanSource: TransactionSource;

if (req instanceof Request) {
isolationScope.setSDKProcessingMetadata({
request: winterCGRequestToRequestData(req),
});
spanName = `middleware ${req.method} ${new URL(req.url).pathname}`;
spanOrigin = 'url';
spanSource = 'url';
} else {
spanName = 'middleware';
spanOrigin = 'component';
spanSource = 'component';
}

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

const rootSpan = getRootSpan(activeSpan);
if (rootSpan) {
Expand All @@ -66,7 +66,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
name: spanName,
op: 'http.server.middleware',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanOrigin,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanSource,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs.wrapMiddlewareWithSentry',
},
},
Expand Down
8 changes: 8 additions & 0 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
applySdkMetadata,
getRootSpan,
registerSpanErrorInstrumentation,
Expand Down Expand Up @@ -52,9 +54,15 @@ export function init(options: VercelEdgeOptions = {}): void {
client?.on('spanStart', span => {
const spanAttributes = spanToJSON(span).data;

// Mark all spans generated by Next.js as 'auto'
if (spanAttributes?.['next.span_type'] !== undefined) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');
}

// Make sure middleware spans get the right op
if (spanAttributes?.['next.span_type'] === 'Middleware.execute') {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');
}
});

Expand Down

0 comments on commit f2b019f

Please sign in to comment.