Skip to content

Commit 5c66a56

Browse files
committed
biome :((
1 parent 7ef333d commit 5c66a56

File tree

4 files changed

+20
-166
lines changed

4 files changed

+20
-166
lines changed

packages/sveltekit/src/server-common/handle.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
44
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
55
continueTrace,
6-
getActiveSpan,
76
getCurrentScope,
87
getDefaultIsolationScope,
98
getIsolationScope,
@@ -130,7 +129,11 @@ async function instrumentHandle(
130129
},
131130
async (span?: Span) => {
132131
getCurrentScope().setSDKProcessingMetadata({
133-
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
132+
// We specifically avoid cloning the request here to avoid double read errors.
133+
// We only read request headers so we're not consuming the body anyway.
134+
// Note to future readers: This sounds counter-intuitive but please read
135+
// https://github.com/getsentry/sentry-javascript/issues/14583
136+
normalizedRequest: winterCGRequestToRequestData(event.request),
134137
});
135138
const res = await resolve(event, {
136139
transformPageChunk: addSentryCodeToPage({ injectFetchProxyScript: options.injectFetchProxyScript ?? true }),
@@ -173,31 +176,29 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
173176
};
174177

175178
const sentryRequestHandler: Handle = input => {
176-
// event.isSubRequest was added in SvelteKit 1.21.0 and we can use it to check
177-
// if we should create a new execution context or not.
179+
// Escape hatch to suppress request isolation and trace continuation (see initCloudflareSentryHandle)
180+
const skipIsolation =
181+
'_sentrySkipRequestIsolation' in input.event.locals && input.event.locals._sentrySkipRequestIsolation;
182+
178183
// In case of a same-origin `fetch` call within a server`load` function,
179184
// SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
180185
// to `true` so that no additional network call is made.
181186
// We want the `http.server` span of that nested call to be a child span of the
182187
// currently active span instead of a new root span to correctly reflect this
183188
// behavior.
184-
// As a fallback for Kit < 1.21.0, we check if there is an active span only if there's none,
185-
// we create a new execution context.
186-
const isSubRequest = typeof input.event.isSubRequest === 'boolean' ? input.event.isSubRequest : !!getActiveSpan();
187-
188-
// Escape hatch to suppress request isolation and trace continuation (see initCloudflareSentryHandle)
189-
const skipIsolation =
190-
'_sentrySkipRequestIsolation' in input.event.locals && input.event.locals._sentrySkipRequestIsolation;
191-
192-
if (isSubRequest || skipIsolation) {
189+
if (skipIsolation || input.event.isSubRequest) {
193190
return instrumentHandle(input, options);
194191
}
195192

196193
return withIsolationScope(isolationScope => {
197194
// We only call continueTrace in the initial top level request to avoid
198195
// creating a new root span for the sub request.
199196
isolationScope.setSDKProcessingMetadata({
200-
normalizedRequest: winterCGRequestToRequestData(input.event.request.clone()),
197+
// We specifically avoid cloning the request here to avoid double read errors.
198+
// We only read request headers so we're not consuming the body anyway.
199+
// Note to future readers: This sounds counter-intuitive but please read
200+
// https://github.com/getsentry/sentry-javascript/issues/14583
201+
normalizedRequest: winterCGRequestToRequestData(input.event.request),
201202
});
202203
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
203204
});

packages/sveltekit/src/server-common/rewriteFramesIntegration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
join,
88
rewriteFramesIntegration as originalRewriteFramesIntegration,
99
} from '@sentry/core';
10-
import type { GlobalWithSentryValues } from '../vite/injectGlobalValues';
1110
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
11+
import type { GlobalWithSentryValues } from '../vite/injectGlobalValues';
1212

1313
type StackFrameIteratee = (frame: StackFrame) => StackFrame;
1414
interface RewriteFramesOptions {

packages/sveltekit/src/server/handle.ts

+3-150
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,6 @@
1-
import type { Span } from '@sentry/core';
2-
import {
3-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5-
continueTrace,
6-
getCurrentScope,
7-
getDefaultIsolationScope,
8-
getIsolationScope,
9-
getTraceMetaTags,
10-
logger,
11-
setHttpStatus,
12-
startSpan,
13-
winterCGRequestToRequestData,
14-
withIsolationScope,
15-
} from '@sentry/core';
16-
import type { Handle, ResolveOptions } from '@sveltejs/kit';
17-
18-
import type { SentryHandleOptions } from '../server-common/handle';
19-
import { sentryHandleGeneric } from '../server-common/handle';
20-
21-
/**
22-
* A SvelteKit handle function that wraps the request for Sentry error and
23-
* performance monitoring.
24-
*
25-
* Usage:
26-
* ```
27-
* // src/hooks.server.ts
28-
* import { sentryHandle } from '@sentry/sveltekit';
29-
*
30-
* export const handle = sentryHandle();
31-
*
32-
* // Optionally use the `sequence` function to add additional handlers.
33-
* // export const handle = sequence(sentryHandle(), yourCustomHandler);
34-
* ```
35-
*/
36-
export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
37-
const { handleUnknownRoutes, ...rest } = handlerOptions ?? {};
38-
const options = {
39-
handleUnknownRoutes: handleUnknownRoutes ?? false,
40-
...rest,
41-
};
42-
43-
const sentryRequestHandler: Handle = input => {
44-
// In case of a same-origin `fetch` call within a server`load` function,
45-
// SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
46-
// to `true` so that no additional network call is made.
47-
// We want the `http.server` span of that nested call to be a child span of the
48-
// currently active span instead of a new root span to correctly reflect this
49-
// behavior.
50-
if (input.event.isSubRequest) {
51-
return instrumentHandle(input, options);
52-
}
53-
54-
return withIsolationScope(isolationScope => {
55-
// We only call continueTrace in the initial top level request to avoid
56-
// creating a new root span for the sub request.
57-
isolationScope.setSDKProcessingMetadata({
58-
// We specifically avoid cloning the request here to avoid double read errors.
59-
// We only read request headers so we're not consuming the body anyway.
60-
// Note to future readers: This sounds counter-intuitive but please read
61-
// https://github.com/getsentry/sentry-javascript/issues/14583
62-
normalizedRequest: winterCGRequestToRequestData(input.event.request),
63-
});
64-
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
65-
});
66-
};
67-
68-
return sentryRequestHandler;
69-
}
70-
71-
async function instrumentHandle(
72-
{ event, resolve }: Parameters<Handle>[0],
73-
options: SentryHandleOptions,
74-
): Promise<Response> {
75-
if (!event.route?.id && !options.handleUnknownRoutes) {
76-
return resolve(event);
77-
}
78-
79-
// caching the result of the version check in `options.injectFetchProxyScript`
80-
// to avoid doing the dynamic import on every request
81-
if (options.injectFetchProxyScript == null) {
82-
try {
83-
// @ts-expect-error - the dynamic import is fine here
84-
const { VERSION } = await import('@sveltejs/kit');
85-
options.injectFetchProxyScript = isFetchProxyRequired(VERSION);
86-
} catch {
87-
options.injectFetchProxyScript = true;
88-
}
89-
}
90-
91-
const routeName = `${event.request.method} ${event.route?.id || event.url.pathname}`;
92-
93-
if (getIsolationScope() !== getDefaultIsolationScope()) {
94-
getIsolationScope().setTransactionName(routeName);
95-
} else {
96-
DEBUG_BUILD && logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
97-
}
98-
99-
try {
100-
const resolveResult = await startSpan(
101-
{
102-
op: 'http.server',
103-
attributes: {
104-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
105-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: event.route?.id ? 'route' : 'url',
106-
'http.method': event.request.method,
107-
},
108-
name: routeName,
109-
},
110-
async (span?: Span) => {
111-
getCurrentScope().setSDKProcessingMetadata({
112-
// We specifically avoid cloning the request here to avoid double read errors.
113-
// We only read request headers so we're not consuming the body anyway.
114-
// Note to future readers: This sounds counter-intuitive but please read
115-
// https://github.com/getsentry/sentry-javascript/issues/14583
116-
normalizedRequest: winterCGRequestToRequestData(event.request),
117-
});
118-
const res = await resolve(event, {
119-
transformPageChunk: addSentryCodeToPage({ injectFetchProxyScript: options.injectFetchProxyScript ?? true }),
120-
});
121-
if (span) {
122-
setHttpStatus(span, res.status);
123-
}
124-
return res;
125-
},
126-
);
127-
return resolveResult;
128-
} catch (e: unknown) {
129-
sendErrorToSentry(e, 'handle');
130-
throw e;
131-
} finally {
132-
await flushIfServerless();
133-
}
134-
}
135-
136-
/**
137-
* We only need to inject the fetch proxy script for SvelteKit versions < 2.16.0.
138-
* Exported only for testing.
139-
*/
140-
export function isFetchProxyRequired(version: string): boolean {
141-
try {
142-
const [major, minor] = version.trim().replace(/-.*/, '').split('.').map(Number);
143-
if (major != null && minor != null && (major > 2 || (major === 2 && minor >= 16))) {
144-
return false;
145-
}
146-
} catch {
147-
// ignore
148-
}
149-
return true;
150-
}
1+
import type { CloudflareOptions } from '@sentry/cloudflare';
2+
import type { Handle } from '@sveltejs/kit';
3+
import { init } from './sdk';
1514

1525
/**
1536
* Actual implementation in ../worker/handle.ts

packages/sveltekit/src/vite/sourceMaps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { sentryVitePlugin } from '@sentry/vite-plugin';
99
import type { Plugin, UserConfig } from 'vite';
1010

1111
import MagicString from 'magic-string';
12+
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
1213
import type { GlobalSentryValues } from './injectGlobalValues';
1314
import { VIRTUAL_GLOBAL_VALUES_FILE, getGlobalValueInjectionCode } from './injectGlobalValues';
1415
import { getAdapterOutputDir, getHooksFileName, loadSvelteConfig } from './svelteConfig';
1516
import type { CustomSentryVitePluginOptions } from './types';
16-
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
1717

1818
// sorcery has no types, so these are some basic type definitions:
1919
type Chain = {

0 commit comments

Comments
 (0)