-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwrapMiddlewareWithSentry.ts
More file actions
119 lines (109 loc) · 4.07 KB
/
wrapMiddlewareWithSentry.ts
File metadata and controls
119 lines (109 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { TransactionSource } from '@sentry/core';
import {
captureException,
getActiveSpan,
getCurrentScope,
getRootSpan,
handleCallbackErrors,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
setCapturedScopesOnSpan,
startSpan,
winterCGRequestToRequestData,
withIsolationScope,
} from '@sentry/core';
import { flushSafelyWithTimeout, waitUntil } from '../common/utils/responseEnd';
import { isPathnameUnderSentryTunnelRoute } from '../common/utils/tunnelPathnameMatch';
import type { EdgeRouteHandler } from '../edge/types';
/**
* Wraps Next.js middleware with Sentry error and performance instrumentation.
*
* @param middleware The middleware handler.
* @returns a wrapped middleware handler.
*/
export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
middleware: H,
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
return new Proxy(middleware, {
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
const tunnelRoute =
'_sentryRewritesTunnelPath' in globalThis
? (globalThis as Record<string, unknown>)._sentryRewritesTunnelPath
: undefined;
if (tunnelRoute && typeof tunnelRoute === 'string') {
const req: unknown = args[0];
// Check if the current request matches the tunnel route
if (req instanceof Request) {
const url = new URL(req.url);
const isTunnelRequest = isPathnameUnderSentryTunnelRoute(url.pathname, tunnelRoute);
if (isTunnelRequest) {
// Create a simple response that mimics NextResponse.next() so we don't need to import internals here
// which breaks next 13 apps
// https://github.com/vercel/next.js/blob/c12c9c1f78ad384270902f0890dc4cd341408105/packages/next/src/server/web/spec-extension/response.ts#L146
return new Response(null, {
status: 200,
headers: {
'x-middleware-next': '1',
},
}) as ReturnType<H>;
}
}
}
// TODO: We still should add central isolation scope creation for when our build-time instrumentation does not work anymore with turbopack.
return withIsolationScope(isolationScope => {
const req: unknown = args[0];
const currentScope = getCurrentScope();
let spanName: string;
let spanSource: TransactionSource;
if (req instanceof Request) {
isolationScope.setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(req),
});
spanName = `middleware ${req.method}`;
spanSource = 'url';
} else {
spanName = 'middleware';
spanSource = 'component';
}
currentScope.setTransactionName(spanName);
const activeSpan = getActiveSpan();
if (activeSpan) {
// 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';
spanSource = 'component';
const rootSpan = getRootSpan(activeSpan);
if (rootSpan) {
setCapturedScopesOnSpan(rootSpan, currentScope, isolationScope);
}
}
return startSpan(
{
name: spanName,
op: 'http.server.middleware',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanSource,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs.wrap_middleware',
},
},
() => {
return handleCallbackErrors(
() => wrappingTarget.apply(thisArg, args),
error => {
captureException(error, {
mechanism: {
type: 'auto.function.nextjs.wrap_middleware',
handled: false,
},
});
},
() => {
waitUntil(flushSafelyWithTimeout());
},
);
},
);
});
},
});
}