Skip to content

feat(cloudflare): Read wrangler config and resolve the Sentry options module#22291

Draft
JPeer264 wants to merge 1 commit into
jp/orchestrion-cloudflarefrom
jp/orchestrion-cloudflare-wrangler-read
Draft

feat(cloudflare): Read wrangler config and resolve the Sentry options module#22291
JPeer264 wants to merge 1 commit into
jp/orchestrion-cloudflarefrom
jp/orchestrion-cloudflare-wrangler-read

Conversation

@JPeer264

Copy link
Copy Markdown
Member

Add the building blocks the auto-instrument Vite plugin will use, with no wiring into a plugin yet. These will come in a follow-up PR one by one.

  • wranglerConfig: locate and parse wrangler.{json,jsonc,toml}, returning the worker entry (main) and the configured Durable Object class names.
  • instrumentFile: find a conventional instrument.server.{ts,js,mjs,cjs} next to the entry and build the options import, falling back to an env-based callback when absent.
  • defineCloudflareOptions: identity helper that gives the options callback its type in that module.

jsonc-parser and smol-toml are also used by wrangler itself, unfortunately there is no library from Cloudflare (yet) that is reading the wrangler config.

The instrument.server.* config can and should be written like the following:

import { defineCloudflareOptions } from '@sentry/cloudflare';

export default defineCloudflareOptions((env) => ({
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
}));

defineCloudflareOptions is only adding typings for env so it is easier it can also be used like the following:

import { defineCloudflareOptions } from '@sentry/cloudflare';

export default defineCloudflareOptions({
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
});

or

import type { CloudflareOptions } from '@sentry/cloudflare';

export default {
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
} satisfies CloudflareOptions;

Future possibilities

For now there is a shared config for all instrumentations. In the future there would be the possibility to have different exports and map them to the actual bindings. E.g.:

import { defineCloudflareOptions } from '@sentry/cloudflare';

// specifically for the durable object called `MY_DURABLE_OBJECT`. This is defined in the wrangler config
export const MY_DURABLE_OBJECT = defineCloudflareOptions((env) => ({
  dsn: env.OTHER_SENTRY_DSN,
  tracesSampleRate: 0.5,
}));

// for the default worker and all the other bindings
export default defineCloudflareOptions((env) => ({
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
}));

@JPeer264 JPeer264 self-assigned this Jul 15, 2026
@JPeer264 JPeer264 force-pushed the jp/orchestrion-cloudflare-wrangler-read branch from 8343656 to eb2d012 Compare July 15, 2026 11:58
@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.65 kB - -
@sentry/browser - with treeshaking flags 26.09 kB - -
@sentry/browser (incl. Tracing) 46.41 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.2 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.19 kB - -
@sentry/browser (incl. Tracing, Replay) 85.67 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.3 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.38 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103.04 kB - -
@sentry/browser (incl. Feedback) 44.83 kB - -
@sentry/browser (incl. sendFeedback) 32.45 kB - -
@sentry/browser (incl. FeedbackAsync) 37.57 kB - -
@sentry/browser (incl. Metrics) 28.73 kB - -
@sentry/browser (incl. Logs) 28.97 kB - -
@sentry/browser (incl. Metrics & Logs) 29.66 kB - -
@sentry/react 29.45 kB - -
@sentry/react (incl. Tracing) 48.67 kB - -
@sentry/vue 33.08 kB - -
@sentry/vue (incl. Tracing) 48.39 kB - -
@sentry/svelte 27.67 kB - -
CDN Bundle 30.05 kB - -
CDN Bundle (incl. Tracing) 48.4 kB - -
CDN Bundle (incl. Logs, Metrics) 31.63 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.7 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.86 kB - -
CDN Bundle (incl. Tracing, Replay) 85.89 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.21 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.69 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.96 kB - -
CDN Bundle - uncompressed 89.58 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.32 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.28 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.3 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.01 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.53 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.49 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.23 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.18 kB - -
@sentry/nextjs (client) 51.22 kB - -
@sentry/sveltekit (client) 46.86 kB - -
@sentry/core/server 78.5 kB - -
@sentry/core/browser 64.85 kB - -
@sentry/node-core 62.79 kB - -
@sentry/node 125.2 kB -0.01% -1 B 🔽
@sentry/node (incl. diagnostics channel injection) 140.23 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.96 kB - -
@sentry/node/light 50.79 kB -0.01% -2 B 🔽
@sentry/node - without tracing 74.17 kB -0.01% -1 B 🔽
@sentry/aws-serverless 83.38 kB -0.01% -4 B 🔽
@sentry/cloudflare (withSentry) - minified 181.92 kB +0.08% +135 B 🔺
@sentry/cloudflare (withSentry) 450.7 kB +0.17% +756 B 🔺

View base workflow run

@timfish timfish left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately there is no library from Cloudflare (yet) that is reading the wrangler config.

Wrangler has a unstable_readConfig API briefly mentioned here although not fully documented. This is what their Vite plugin uses and it's used a lot elsewhere so should be stable enough. It resolves and normalise the wrangler config for you.

@JPeer264 JPeer264 force-pushed the jp/orchestrion-cloudflare branch from afcb71d to 8517822 Compare July 16, 2026 06:41
@JPeer264 JPeer264 force-pushed the jp/orchestrion-cloudflare-wrangler-read branch from eb2d012 to a7e002e Compare July 16, 2026 06:41
… module

Add the building blocks the auto-instrument Vite plugin will use, with no wiring
into a plugin yet:

- `wranglerConfig`: locate and parse `wrangler.{json,jsonc,toml}`, returning the
  worker entry (`main`) and the configured Durable Object class names.
- `instrumentFile`: find a conventional `instrument.server.{ts,js,mjs,cjs}` next
  to the entry and build the options import, falling back to an env-based
  callback when absent.
- `defineCloudflareOptions`: identity helper that gives the options callback its
  type in that module.

Adds `jsonc-parser`, `smol-toml`, and `magic-string` as dependencies.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@JPeer264 JPeer264 force-pushed the jp/orchestrion-cloudflare branch from 8517822 to 7e8f420 Compare July 16, 2026 07:34
@JPeer264 JPeer264 force-pushed the jp/orchestrion-cloudflare-wrangler-read branch from a7e002e to b5454ef Compare July 16, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants