-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhandle.ts
More file actions
291 lines (260 loc) · 10.3 KB
/
handle.ts
File metadata and controls
291 lines (260 loc) · 10.3 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import type { Span } from '@sentry/core';
import {
continueTrace,
debug,
flushIfServerless,
getCurrentScope,
getDefaultIsolationScope,
getIsolationScope,
getTraceMetaTags,
httpHeadersToSpanAttributes,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
setHttpStatus,
spanToJSON,
startSpan,
updateSpanName,
winterCGHeadersToDict,
winterCGRequestToRequestData,
withIsolationScope,
} from '@sentry/core';
import { getClient } from '@sentry/svelte';
import type { Handle, ResolveOptions } from '@sveltejs/kit';
import { DEBUG_BUILD } from '../common/debug-build';
import { getTracePropagationData, sendErrorToSentry } from './utils';
export type SentryHandleOptions = {
/**
* Controls whether the SDK should capture errors and traces in requests that don't belong to a
* route defined in your SvelteKit application.
*
* By default, this option is set to `false` to reduce noise (e.g. bots sending random requests to your server).
*
* Set this option to `true` if you want to monitor requests events without a route. This might be useful in certain
* scenarios, for instance if you registered other handlers that handle these requests.
* If you set this option, you might want adjust the the transaction name in the `beforeSendTransaction`
* callback of your server-side `Sentry.init` options. You can also use `beforeSendTransaction` to filter out
* transactions that you still don't want to be sent to Sentry.
*
* @default false
*/
handleUnknownRoutes?: boolean;
/**
* Controls if `sentryHandle` should inject a script tag into the page that enables instrumentation
* of `fetch` calls in `load` functions.
*
* @default true
*/
injectFetchProxyScript?: boolean;
};
export const FETCH_PROXY_SCRIPT = `
const f = window.fetch;
if(f){
window._sentryFetchProxy = function(...a){return f(...a)}
window.fetch = function(...a){return window._sentryFetchProxy(...a)}
}
`;
/**
* Adds Sentry tracing <meta> tags to the returned html page.
* Adds Sentry fetch proxy script to the returned html page if enabled in options.
*
* Exported only for testing
*/
export function addSentryCodeToPage(options: {
injectFetchProxyScript: boolean;
}): NonNullable<ResolveOptions['transformPageChunk']> {
return ({ html }) => {
const metaTags = getTraceMetaTags();
const headWithMetaTags = metaTags ? `<head>\n${metaTags}` : '<head>';
const headWithFetchScript = options.injectFetchProxyScript ? `\n<script>${FETCH_PROXY_SCRIPT}</script>` : '';
const modifiedHead = `${headWithMetaTags}${headWithFetchScript}`;
return html.replace('<head>', modifiedHead);
};
}
/**
* We only need to inject the fetch proxy script for SvelteKit versions < 2.16.0.
* Exported only for testing.
*/
export function isFetchProxyRequired(version: string): boolean {
try {
const [major, minor] = version.trim().replace(/-.*/, '').split('.').map(Number);
if (major != null && minor != null && (major > 2 || (major === 2 && minor >= 16))) {
return false;
}
} catch {
// ignore
}
return true;
}
interface BackwardsForwardsCompatibleEvent {
/**
* For now taken from: https://github.com/sveltejs/kit/pull/13899
* Access to spans for tracing. If tracing is not enabled or the function is being run in the browser, these spans will do nothing.
* @since 2.31.0
*/
tracing?: {
/** Whether tracing is enabled. */
enabled: boolean;
current: Span;
root: Span;
};
}
async function instrumentHandle(
{
event,
resolve,
}: {
event: Parameters<Handle>[0]['event'] & BackwardsForwardsCompatibleEvent;
resolve: Parameters<Handle>[0]['resolve'];
},
options: SentryHandleOptions,
): Promise<Response> {
const routeId = event.route?.id;
if (!routeId && !options.handleUnknownRoutes) {
return resolve(event);
}
// caching the result of the version check in `options.injectFetchProxyScript`
// to avoid doing the dynamic import on every request
if (options.injectFetchProxyScript == null) {
try {
// @ts-expect-error - the dynamic import is fine here
const { VERSION } = await import('@sveltejs/kit');
options.injectFetchProxyScript = isFetchProxyRequired(VERSION);
} catch {
options.injectFetchProxyScript = true;
}
}
const routeName = `${event.request.method} ${routeId || event.url.pathname}`;
if (getIsolationScope() !== getDefaultIsolationScope()) {
getIsolationScope().setTransactionName(routeName);
} else {
DEBUG_BUILD && debug.warn('Isolation scope is default isolation scope - skipping setting transactionName');
}
// We only start a span if SvelteKit's native tracing is not enabled. Two reasons:
// - Used Kit version doesn't yet support tracing
// - Users didn't enable tracing
const kitTracingEnabled = event.tracing?.enabled;
try {
const resolveWithSentry: (sentrySpan?: Span) => Promise<Response> = async (sentrySpan?: Span) => {
getCurrentScope().setSDKProcessingMetadata({
// We specifically avoid cloning the request here to avoid double read errors.
// We only read request headers so we're not consuming the body anyway.
// Note to future readers: This sounds counter-intuitive but please read
// https://github.com/getsentry/sentry-javascript/issues/14583
normalizedRequest: winterCGRequestToRequestData(event.request),
});
const kitRootSpan = event.tracing?.enabled ? event.tracing?.root : undefined;
if (kitRootSpan) {
// Update the root span emitted from SvelteKit to resemble a `http.server` span
// We're doing this here instead of an event processor to ensure we update the
// span name as early as possible (for dynamic sampling, et al.)
// Other spans are enhanced in the `processKitSpans` integration.
const spanJson = spanToJSON(kitRootSpan);
const kitRootSpanAttributes = spanJson.data;
const originalName = spanJson.description;
const routeName = kitRootSpanAttributes['http.route'];
if (routeName && typeof routeName === 'string') {
updateSpanName(kitRootSpan, `${event.request.method ?? 'GET'} ${routeName}`);
}
kitRootSpan.setAttributes({
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeName ? 'route' : 'url',
'sveltekit.tracing.original_name': originalName,
...httpHeadersToSpanAttributes(winterCGHeadersToDict(event.request.headers)),
});
}
const res = await resolve(event, {
transformPageChunk: addSentryCodeToPage({
injectFetchProxyScript: options.injectFetchProxyScript ?? true,
}),
});
if (sentrySpan) {
setHttpStatus(sentrySpan, res.status);
}
return res;
};
const resolveResult = kitTracingEnabled
? await resolveWithSentry()
: await startSpan(
{
op: 'http.server',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
'http.method': event.request.method,
...httpHeadersToSpanAttributes(
winterCGHeadersToDict(event.request.headers),
getClient()?.getOptions().sendDefaultPii ?? false,
),
},
name: routeName,
},
resolveWithSentry,
);
return resolveResult;
} catch (e: unknown) {
sendErrorToSentry(e, 'handle');
throw e;
} finally {
await flushIfServerless();
}
}
/**
* A SvelteKit handle function that wraps the request for Sentry error and
* performance monitoring.
*
* Usage:
* ```
* // src/hooks.server.ts
* import { sentryHandle } from '@sentry/sveltekit';
*
* export const handle = sentryHandle();
*
* // Optionally use the `sequence` function to add additional handlers.
* // export const handle = sequence(sentryHandle(), yourCustomHandler);
* ```
*/
export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
const { handleUnknownRoutes, ...rest } = handlerOptions ?? {};
const options = {
handleUnknownRoutes: handleUnknownRoutes ?? false,
...rest,
};
const sentryRequestHandler: Handle = input => {
const backwardsForwardsCompatibleEvent = input.event as typeof input.event & BackwardsForwardsCompatibleEvent;
// Escape hatch to suppress request isolation and trace continuation (see initCloudflareSentryHandle)
const skipIsolation =
'_sentrySkipRequestIsolation' in backwardsForwardsCompatibleEvent.locals &&
backwardsForwardsCompatibleEvent.locals._sentrySkipRequestIsolation;
// In case of a same-origin `fetch` call within a server`load` function,
// SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
// to `true` so that no additional network call is made.
// We want the `http.server` span of that nested call to be a child span of the
// currently active span instead of a new root span to correctly reflect this
// behavior.
if (skipIsolation || input.event.isSubRequest) {
return instrumentHandle(input, {
...options,
});
}
return withIsolationScope(isolationScope => {
// We only call continueTrace in the initial top level request to avoid
// creating a new root span for the sub request.
isolationScope.setSDKProcessingMetadata({
// We specifically avoid cloning the request here to avoid double read errors.
// We only read request headers so we're not consuming the body anyway.
// Note to future readers: This sounds counter-intuitive but please read
// https://github.com/getsentry/sentry-javascript/issues/14583
normalizedRequest: winterCGRequestToRequestData(input.event.request),
});
if (backwardsForwardsCompatibleEvent.tracing?.enabled) {
// if sveltekit tracing is enabled (since 2.31.0), trace continuation is handled by
// kit before our hook is executed. No noeed to call `continueTrace` from our end
return instrumentHandle(input, options);
}
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
});
};
return sentryRequestHandler;
}