Skip to content

feat(sveltekit): Ensure AsyncLocalStorage async context strategy is used in Cloudflare Pages #15557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cloudflare/src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { AsyncLocalStorage } from 'node:async_hooks';
*
* AsyncLocalStorage is only available in the cloudflare workers runtime if you set
* compatibility_flags = ["nodejs_compat"] or compatibility_flags = ["nodejs_als"]
*
* @internal Only exported to be used in higher-level Sentry packages
* @hidden Only exported to be used in higher-level Sentry packages
*/
export function setAsyncLocalStorageAsyncContextStrategy(): void {
const asyncStorage = new AsyncLocalStorage<{
Expand Down
2 changes: 2 additions & 0 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ export { getDefaultIntegrations } from './sdk';
export { fetchIntegration } from './integrations/fetch';

export { instrumentD1WithSentry } from './d1';

export { setAsyncLocalStorageAsyncContextStrategy } from './async';
8 changes: 7 additions & 1 deletion packages/sveltekit/src/worker/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type CloudflareOptions, wrapRequestHandler } from '@sentry/cloudflare';
import {
type CloudflareOptions,
wrapRequestHandler,
setAsyncLocalStorageAsyncContextStrategy,
} from '@sentry/cloudflare';
import { getDefaultIntegrations as getDefaultCloudflareIntegrations } from '@sentry/cloudflare';
import type { Handle } from '@sveltejs/kit';

Expand All @@ -17,6 +21,8 @@ export function initCloudflareSentryHandle(options: CloudflareOptions): Handle {
...options,
};

setAsyncLocalStorageAsyncContextStrategy();

const handleInitSentry: Handle = ({ event, resolve }) => {
// if event.platform exists (should be there in a cloudflare worker), then do the cloudflare sentry init
if (event.platform) {
Expand Down
Loading