-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
handle.ts
192 lines (173 loc) · 6.68 KB
/
handle.ts
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
import type { Span } from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
getActiveSpan,
getCurrentScope,
getDefaultIsolationScope,
getIsolationScope,
getTraceMetaTags,
logger,
setHttpStatus,
startSpan,
winterCGRequestToRequestData,
withIsolationScope,
} from '@sentry/core';
import { continueTrace } from '@sentry/node';
import type { Handle, ResolveOptions } from '@sveltejs/kit';
import { DEBUG_BUILD } from '../common/debug-build';
import { flushIfServerless, 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;
/**
* If this option is set, the `sentryHandle` handler will add a nonce attribute to the script
* tag it injects into the page. This script is used to enable instrumentation of `fetch` calls
* in `load` functions.
*
* Use this if your CSP policy blocks the fetch proxy script injected by `sentryHandle`.
*/
fetchProxyScriptNonce?: string;
};
/**
* Exported only for testing
*/
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.
* Also adds a nonce attribute to the script tag if users specified one for CSP.
*
* Exported only for testing
*/
export function addSentryCodeToPage(options: SentryHandleOptions): NonNullable<ResolveOptions['transformPageChunk']> {
const { fetchProxyScriptNonce, injectFetchProxyScript } = options;
// if injectFetchProxyScript is not set, we default to true
const shouldInjectScript = injectFetchProxyScript !== false;
const nonce = fetchProxyScriptNonce ? `nonce="${fetchProxyScriptNonce}"` : '';
return ({ html }) => {
const metaTags = getTraceMetaTags();
const headWithMetaTags = metaTags ? `<head>\n${metaTags}` : '<head>';
const headWithFetchScript = shouldInjectScript ? `\n<script ${nonce}>${FETCH_PROXY_SCRIPT}</script>` : '';
const modifiedHead = `${headWithMetaTags}${headWithFetchScript}`;
return html.replace('<head>', modifiedHead);
};
}
/**
* 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 options = {
handleUnknownRoutes: false,
injectFetchProxyScript: true,
...handlerOptions,
};
const sentryRequestHandler: Handle = input => {
// event.isSubRequest was added in SvelteKit 1.21.0 and we can use it to check
// if we should create a new execution context or not.
// 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.
// As a fallback for Kit < 1.21.0, we check if there is an active span only if there's none,
// we create a new execution context.
const isSubRequest = typeof input.event.isSubRequest === 'boolean' ? input.event.isSubRequest : !!getActiveSpan();
if (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({
normalizedRequest: winterCGRequestToRequestData(input.event.request.clone()),
});
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
});
};
return sentryRequestHandler;
}
async function instrumentHandle(
{ event, resolve }: Parameters<Handle>[0],
options: SentryHandleOptions,
): Promise<Response> {
if (!event.route?.id && !options.handleUnknownRoutes) {
return resolve(event);
}
const routeName = `${event.request.method} ${event.route?.id || event.url.pathname}`;
if (getIsolationScope() !== getDefaultIsolationScope()) {
getIsolationScope().setTransactionName(routeName);
} else {
DEBUG_BUILD && logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
}
try {
const resolveResult = await startSpan(
{
op: 'http.server',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: event.route?.id ? 'route' : 'url',
'http.method': event.request.method,
},
name: routeName,
},
async (span?: Span) => {
getCurrentScope().setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
});
const res = await resolve(event, {
transformPageChunk: addSentryCodeToPage(options),
});
if (span) {
setHttpStatus(span, res.status);
}
return res;
},
);
return resolveResult;
} catch (e: unknown) {
sendErrorToSentry(e, 'handle');
throw e;
} finally {
await flushIfServerless();
}
}