Skip to content

Commit 1bfe76b

Browse files
committed
do wrap :')
1 parent 7f54152 commit 1bfe76b

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ test('Faulty middlewares', async ({ request }) => {
4848
// Assert that isolation scope works properly
4949
expect(errorEvent.tags?.['my-isolated-tag']).toBe(true);
5050
expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
51-
expect(errorEvent.transaction).toBe('/middleware');
51+
// this differs between webpack and turbopack
52+
expect(['middleware GET', '/middleware']).toContain(errorEvent.transaction);
5253
});
5354
});
5455

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
33

44
test('Should create a transaction for middleware', async ({ request }) => {
55
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
6-
return transactionEvent?.transaction === 'middleware GET /api/endpoint-behind-middleware';
6+
return transactionEvent?.transaction === 'middleware GET';
77
});
88

99
const response = await request.get('/api/endpoint-behind-middleware');
@@ -23,10 +23,12 @@ test('Should create a transaction for middleware', async ({ request }) => {
2323

2424
test('Faulty middlewares', async ({ request }) => {
2525
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
26-
return transactionEvent?.transaction === 'middleware GET /api/endpoint-behind-faulty-middleware';
26+
console.log('transactionEvent', transactionEvent.transaction);
27+
return transactionEvent?.transaction === 'middleware GET';
2728
});
2829

2930
const errorEventPromise = waitForError('nextjs-pages-dir', errorEvent => {
31+
console.log('errorEvent', errorEvent.transaction);
3032
return errorEvent?.exception?.values?.[0]?.value === 'Middleware Error';
3133
});
3234

@@ -48,14 +50,14 @@ test('Faulty middlewares', async ({ request }) => {
4850
// Assert that isolation scope works properly
4951
expect(errorEvent.tags?.['my-isolated-tag']).toBe(true);
5052
expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
51-
expect(errorEvent.transaction).toBe('middleware GET /api/endpoint-behind-faulty-middleware');
53+
expect(errorEvent.transaction).toBe('middleware GET');
5254
});
5355
});
5456

5557
test('Should trace outgoing fetch requests inside middleware and create breadcrumbs for it', async ({ request }) => {
5658
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
5759
return (
58-
transactionEvent?.transaction === 'middleware GET /api/endpoint-behind-middleware' &&
60+
transactionEvent?.transaction === 'middleware GET' &&
5961
!!transactionEvent.spans?.find(span => span.op === 'http.client')
6062
);
6163
});

packages/nextjs/src/common/wrapMiddlewareWithSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
6464
isolationScope.setSDKProcessingMetadata({
6565
normalizedRequest: winterCGRequestToRequestData(req),
6666
});
67-
spanName = `middleware ${req.method} ${new URL(req.url).pathname}`;
67+
spanName = `middleware ${req.method}`;
6868
spanSource = 'url';
6969
} else {
7070
spanName = 'middleware';

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ export function constructWebpackConfigFunction({
283283
});
284284

285285
// Wrap middleware
286-
// if (userSentryOptions.autoInstrumentMiddleware ?? true) {
287-
// eslint-disable-next-line no-constant-condition
288-
if (false) {
286+
if (userSentryOptions.autoInstrumentMiddleware ?? true) {
289287
newConfig.module.rules.unshift({
290288
test: isMiddlewareResource,
291289
use: [

packages/nextjs/src/edge/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { context } from '@opentelemetry/api';
12
import {
23
applySdkMetadata,
34
getCapturedScopesOnSpan,
@@ -15,14 +16,13 @@ import {
1516
stripUrlQueryAndFragment,
1617
vercelWaitUntil,
1718
} from '@sentry/core';
19+
import { getScopesFromContext } from '@sentry/opentelemetry';
1820
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
1921
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
2022
import { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';
2123
import { isBuild } from '../common/utils/isBuild';
2224
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
2325
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';
24-
import { getScopesFromContext } from '@sentry/opentelemetry';
25-
import { context } from '@opentelemetry/api';
2626

2727
export * from '@sentry/vercel-edge';
2828
export * from '../common';
@@ -111,7 +111,19 @@ export function init(options: VercelEdgeOptions = {}): void {
111111
event.contexts?.trace?.data?.['next.span_name']
112112
) {
113113
if (event.transaction) {
114-
event.transaction = stripUrlQueryAndFragment(event.contexts.trace.data['next.span_name']);
114+
// Older nextjs versions pass the full url appended to the middleware name, which results in high cardinality transaction names.
115+
// We want to remove the url from the name here.
116+
const spanName = event.contexts.trace.data['next.span_name'];
117+
118+
if (typeof spanName === 'string') {
119+
const match = spanName.match(/^middleware (GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)/);
120+
if (match) {
121+
const normalizedName = `middleware ${match[1]}`;
122+
event.transaction = normalizedName;
123+
} else {
124+
event.transaction = stripUrlQueryAndFragment(event.contexts.trace.data['next.span_name']);
125+
}
126+
}
115127
}
116128
}
117129
});

0 commit comments

Comments
 (0)