Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading