Skip to content

Commit ce93824

Browse files
SG60Lms24
authored andcommitted
fix(sveltekit): provide a handle function to init cloudflare workers sdk in sveltekit
Also reexports the Cloudflare SDK.
1 parent 36a85a0 commit ce93824

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

packages/sveltekit/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"engines": {
1010
"node": ">=18"
1111
},
12-
"files": [
13-
"/build"
14-
],
12+
"files": ["/build"],
1513
"main": "build/cjs/index.server.js",
1614
"module": "build/esm/index.server.js",
1715
"browser": "build/esm/index.client.js",
@@ -44,6 +42,7 @@
4442
}
4543
},
4644
"dependencies": {
45+
"@sentry/cloudflare": "9.1.0",
4746
"@sentry/core": "9.1.0",
4847
"@sentry/node": "9.1.0",
4948
"@sentry/opentelemetry": "9.1.0",

packages/sveltekit/src/worker/handle.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
startSpan,
1313
winterCGRequestToRequestData,
1414
withIsolationScope,
15-
continueTrace,
1615
} from '@sentry/core';
16+
import { CloudflareOptions, continueTrace, wrapRequestHandler } from '@sentry/cloudflare';
1717
import type { Handle, ResolveOptions } from '@sveltejs/kit';
1818

1919
import { DEBUG_BUILD } from '../common/debug-build';
@@ -192,3 +192,27 @@ async function instrumentHandle(
192192
await flushIfServerless();
193193
}
194194
}
195+
196+
/** Initializes Sentry SvelteKit Cloudflare SDK
197+
* This should be before the sentryHandle() call.
198+
* */
199+
export function initCloudflareSentryHandle(handlerOptions: CloudflareOptions): Handle {
200+
const options = { ...handlerOptions };
201+
202+
const handleInitSentry: Handle = ({ event, resolve }) => {
203+
// if event.platform exists (should be there in a cloudflare worker), then do the cloudflare sentry init
204+
return event.platform
205+
? wrapRequestHandler(
206+
{
207+
options,
208+
request: event.request,
209+
// @ts-expect-error This will exist in Cloudflare
210+
context: event.platform.context,
211+
},
212+
() => resolve(event),
213+
)
214+
: resolve(event);
215+
};
216+
217+
return handleInitSentry;
218+
}

packages/sveltekit/src/worker/handleError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { consoleSandbox, captureException } from '@sentry/core';
1+
import { consoleSandbox } from '@sentry/core';
2+
import { captureException } from '@sentry/cloudflare';
23
import type { HandleServerError } from '@sveltejs/kit';
34

45
import { flushIfServerless } from './utils';

packages/sveltekit/src/worker/index.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,33 @@
1010
// SvelteKit SDK exports:
1111
export { handleErrorWithSentry } from './handleError';
1212
export { wrapLoadWithSentry, wrapServerLoadWithSentry } from './load';
13-
export { sentryHandle } from './handle';
13+
export { sentryHandle, initCloudflareSentryHandle } from './handle';
1414
export { wrapServerRouteWithSentry } from './serverRoute';
1515

16-
// Re-export some functions from core SDK
16+
// Re-export some functions from Cloudflare SDK
1717
export {
1818
addBreadcrumb,
1919
addEventProcessor,
2020
addIntegration,
21-
// eslint-disable-next-line deprecation/deprecation
22-
addRequestDataToEvent,
2321
captureCheckIn,
2422
captureConsoleIntegration,
2523
captureEvent,
2624
captureException,
2725
captureFeedback,
2826
captureMessage,
29-
captureSession,
3027
close,
3128
continueTrace,
3229
createTransport,
3330
// eslint-disable-next-line deprecation/deprecation
3431
debugIntegration,
3532
dedupeIntegration,
36-
DEFAULT_USER_INCLUDES,
37-
endSession,
38-
// eslint-disable-next-line deprecation/deprecation
39-
extractRequestData,
4033
extraErrorDataIntegration,
4134
flush,
4235
functionToStringIntegration,
4336
getActiveSpan,
4437
getClient,
45-
// eslint-disable-next-line deprecation/deprecation
46-
getCurrentHub,
4738
getCurrentScope,
39+
getDefaultIntegrations,
4840
getGlobalScope,
4941
getIsolationScope,
5042
getRootSpan,
@@ -58,7 +50,6 @@ export {
5850
linkedErrorsIntegration,
5951
// eslint-disable-next-line deprecation/deprecation
6052
metrics,
61-
parameterize,
6253
requestDataIntegration,
6354
rewriteFramesIntegration,
6455
Scope,
@@ -67,8 +58,6 @@ export {
6758
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
6859
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
6960
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
70-
// eslint-disable-next-line deprecation/deprecation
71-
sessionTimingIntegration,
7261
setContext,
7362
setCurrentClient,
7463
setExtra,
@@ -84,7 +73,6 @@ export {
8473
startInactiveSpan,
8574
startNewTrace,
8675
suppressTracing,
87-
startSession,
8876
startSpan,
8977
startSpanManual,
9078
trpcMiddleware,
@@ -93,7 +81,7 @@ export {
9381
withMonitor,
9482
withScope,
9583
zodErrorsIntegration,
96-
} from '@sentry/core';
84+
} from '@sentry/cloudflare';
9785

9886
/**
9987
* Tracks the Svelte component's initialization and mounting operation as well as

packages/sveltekit/src/worker/load.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
addNonEnumerableProperty,
3-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5-
startSpan,
6-
} from '@sentry/core';
1+
import { addNonEnumerableProperty } from '@sentry/core';
2+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, startSpan } from '@sentry/cloudflare';
73
import type { LoadEvent, ServerLoadEvent } from '@sveltejs/kit';
84

95
import type { SentryWrappedFlag } from '../common/utils';

packages/sveltekit/src/worker/serverRoute.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
addNonEnumerableProperty,
3-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5-
startSpan,
6-
} from '@sentry/core';
1+
import { addNonEnumerableProperty } from '@sentry/core';
2+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, startSpan } from '@sentry/cloudflare';
73
import type { RequestEvent } from '@sveltejs/kit';
84
import { flushIfServerless, sendErrorToSentry } from './utils';
95

packages/sveltekit/src/worker/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { logger, objectify, captureException, flush } from '@sentry/core';
1+
import { logger, objectify } from '@sentry/core';
2+
import { captureException, flush } from '@sentry/cloudflare';
23
import type { RequestEvent } from '@sveltejs/kit';
34

45
import { DEBUG_BUILD } from '../common/debug-build';

0 commit comments

Comments
 (0)