MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / sentryHandle

Function sentryHandle

packages/sveltekit/src/server-common/handle.ts:244–293  ·  view source on GitHub ↗
(handlerOptions?: SentryHandleOptions)

Source from the content-addressed store, hash-verified

242 * ```
243 */
244export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
245 const { handleUnknownRoutes, ...rest } = handlerOptions ?? {};
246 const options = {
247 handleUnknownRoutes: handleUnknownRoutes ?? false,
248 ...rest,
249 };
250
251 const sentryRequestHandler: Handle = input => {
252 const backwardsForwardsCompatibleEvent = input.event as typeof input.event & BackwardsForwardsCompatibleEvent;
253
254 // Escape hatch to suppress request isolation and trace continuation (see initCloudflareSentryHandle)
255 const skipIsolation =
256 '_sentrySkipRequestIsolation' in backwardsForwardsCompatibleEvent.locals &&
257 backwardsForwardsCompatibleEvent.locals._sentrySkipRequestIsolation;
258
259 // In case of a same-origin `fetch` call within a server`load` function,
260 // SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
261 // to `true` so that no additional network call is made.
262 // We want the `http.server` span of that nested call to be a child span of the
263 // currently active span instead of a new root span to correctly reflect this
264 // behavior.
265 if (skipIsolation || input.event.isSubRequest) {
266 return instrumentHandle(input, {
267 ...options,
268 });
269 }
270
271 return withIsolationScope(isolationScope => {
272 // We only call continueTrace in the initial top level request to avoid
273 // creating a new root span for the sub request.
274 isolationScope.setSDKProcessingMetadata({
275 // We specifically avoid cloning the request here to avoid double read errors.
276 // We only read request headers so we're not consuming the body anyway.
277 // Note to future readers: This sounds counter-intuitive but please read
278 // https://github.com/getsentry/sentry-javascript/issues/14583
279 normalizedRequest: winterCGRequestToRequestData(input.event.request),
280 });
281
282 if (backwardsForwardsCompatibleEvent.tracing?.enabled) {
283 // if sveltekit tracing is enabled (since 2.31.0), trace continuation is handled by
284 // kit before our hook is executed. No noeed to call `continueTrace` from our end
285 return instrumentHandle(input, options);
286 }
287
288 return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
289 });
290 };
291
292 return sentryRequestHandler;
293}

Callers 2

handle.test.tsFile · 0.90
hooks.server.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected