Skip to content

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

Merged
JPeer264 merged 2 commits into
developfrom
jp/cloudflare-auto-instrument-read-wrangler
Jul 22, 2026
Merged

feat(cloudflare): Read wrangler config and resolve the Sentry options module#22421
JPeer264 merged 2 commits into
developfrom
jp/cloudflare-auto-instrument-read-wrangler

Conversation

@JPeer264

Copy link
Copy Markdown
Member

(replaces #22291)

part of #22066

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} via wrangler's own unstable_readConfig, 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 wrangler as an optional peer dependency (used to read the 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 21, 2026
@JPeer264
JPeer264 force-pushed the jp/cloudflare-auto-instrument-read-wrangler branch 2 times, most recently from 7fa0bdf to f794a64 Compare July 21, 2026 15:25
@JPeer264
JPeer264 marked this pull request as ready for review July 21, 2026 15:25
@JPeer264
JPeer264 requested a review from a team as a code owner July 21, 2026 15:25
@JPeer264
JPeer264 requested review from andreiborza, isaacs and timfish and removed request for a team July 21, 2026 15:25
try {
// `hideWarnings` keeps wrangler's config diagnostics (e.g. missing DO
// migrations) out of the Vite build output.
raw = unstable_readConfig({ config: configPath }, { hideWarnings: true });

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.

If you don't pass a config path, it says that wrangler will find the config. Would it be better to rely on the wrangler resolution?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

great suggestion, with that it gets way cleaner

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok I'll change that in a follow up PR. it gets too much of a mess with this stack otherwise

Comment thread scripts/ci-unit-tests.ts
];

// Packages that cannot run in Node 18
const SKIP_NODE_18_PACKAGES = ['@sentry/react-router'];

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.

We just dropped Node v18 testing so this entre line might be redundant?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

True, I'll remove this. I think this can also be cleaned up entirely ?
I could do this in a separate PR

@JPeer264
JPeer264 force-pushed the jp/cloudflare-auto-instrument-read-wrangler branch from f794a64 to e5d9096 Compare July 22, 2026 06:04
@JPeer264
JPeer264 force-pushed the jp/cloudflare-auto-instrument-read-wrangler branch 3 times, most recently from 2607d0b to 8864e2e Compare July 22, 2026 12:54
JPeer264 and others added 2 commits July 22, 2026 16:12
… 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}` via wrangler's
  own `unstable_readConfig`, 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 `wrangler` as an optional peer dependency (used to read the config).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/cloudflare-auto-instrument-read-wrangler branch from 8864e2e to 989fd3d Compare July 22, 2026 13:12
@JPeer264
JPeer264 merged commit 2dedd3f into develop Jul 22, 2026
85 checks passed
@JPeer264
JPeer264 deleted the jp/cloudflare-auto-instrument-read-wrangler branch July 22, 2026 15: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