Vite Plugin for Cloudflare
Learn how to use the Sentry Cloudflare Vite plugin to instrument bundled dependencies at build time.
Available since: v10.68.0
The Sentry Cloudflare Vite plugin (sentryCloudflareVitePlugin) instruments your Worker at build time. It can:
- Instrument bundled dependencies: instruments supported packages in your bundle, such as database clients and AI SDKs, giving you more traces out of the box.
- Auto-instrument your Worker entry: wraps your default export with
Sentry.withSentry(), and Durable Object, Workflow, and Agents SDK classes with the matchinginstrument*WithSentryhelper at build time, so you don't need to modify your code.
Both are on by default. Turn either off with buildTimeInstrumentation and autoInstrumentation.
The quick start covers adding the plugin and creating instrument.server.ts. This page documents what it instruments, its options, and the details that matter once it's running.
The plugin reads your wrangler config (probing wrangler.json, wrangler.jsonc, and wrangler.toml at the Vite root, or the file set with wranglerConfigPath) to find the entry point, Durable Objects, workflows, and Agents SDK classes. It wraps Agents SDK classes (Agent, AIChatAgent, McpAgent) with instrumentAgentWithSentry, which also gives them automatic conversation IDs (see Cloudflare Agents SDK).
An entry you wrapped with withSentry yourself is left untouched, so manual instrumentation keeps working next to the plugin. To keep the plugin out of your entry entirely, set autoInstrumentation to false and wrap it yourself as shown in the Wrangler setup.
With auto-instrumentation, you can optionally provide Sentry options via a co-located instrument.server.* file (.ts, .mts, .js, .mjs, or .cjs) next to your Worker entry. The plugin resolves this location from main in your wrangler config. For example, if main is src/worker/index.ts, place the file at src/worker/instrument.server.ts, not at the project root. Use defineCloudflareOptions for full type-checking:
instrument.server.tsimport { defineCloudflareOptions } from "@sentry/cloudflare";
export default defineCloudflareOptions((env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}));
import { defineCloudflareOptions } from "@sentry/cloudflare";
export default defineCloudflareOptions((env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}));
If no instrument.server.* file exists, the SDK reads all configuration (DSN, release, environment, sample rate, etc.) from the Worker's env bindings at runtime.
Configured classes are wrapped wherever they reach the Worker entry, whether they're declared there, imported from another module and exported by name, or re-exported straight through. A class you wrapped yourself is left as it is.
Available since: v10.72.0
The plugin knows which bindings point at classes it wrapped itself: Durable Object bindings without a script_name, and service bindings naming this Worker. Those receivers are guaranteed to strip the trailing trace argument again, so the plugin adds their binding names to rpcTracePropagationBindings, which covers the calling and the receiving side alike. Traces then connect across RPC calls within one deployment, with no configuration of your own.
Bindings to other Workers stay opt-in, because their receivers may not run Sentry. List those yourself in instrument.server.*; whatever you list is added on top of the derived names.
The plugin derives only the classes it wrapped itself. A class you wrapped by hand runs on its own options and stays out.
This applies to Vite builds only. At runtime a DurableObjectNamespace exposes no origin and a Fetcher does not say which service it points at, so a plain wrangler build still has to list its bindings.
wranglerConfigPath
| Available since | 10.69.0 |
|---|---|
| Type | string |
Path to your wrangler config file. By default the plugin probes wrangler.json, wrangler.jsonc, and wrangler.toml at the Vite root. Set this when your config lives at a custom path, for example to mirror the configPath option of the Cloudflare Vite plugin:
vite.config.tsexport default defineConfig({
plugins: [
cloudflare({ configPath: "./wrangler.agent.jsonc" }),
sentryCloudflareVitePlugin({
wranglerConfigPath: "./wrangler.agent.jsonc",
}),
],
});
export default defineConfig({
plugins: [
cloudflare({ configPath: "./wrangler.agent.jsonc" }),
sentryCloudflareVitePlugin({
wranglerConfigPath: "./wrangler.agent.jsonc",
}),
],
});
buildTimeInstrumentation
| Type | boolean |
|---|---|
| Default | true |
Build-time instrumentation of supported dependencies. The plugin injects diagnostics_channel.tracingChannel calls into the bundled packages, and next to each one a snippet that registers the matching Sentry channel subscriber, which the SDK picks up in Sentry.withSentry(). This is how those packages get traced in the Workers runtime, where the SDK can't monkey-patch them. Both vite build and vite dev are instrumented. Set to false to opt out.
A package is only instrumented if it's actually bundled. A dependency you mark as external is resolved at runtime and never passes through the build, so it stays untraced.
autoInstrumentation
| Type | boolean |
|---|---|
| Default | true |
Wraps your Worker at build time so you don't have to edit your entry. The plugin reads your wrangler config, wraps the default export with Sentry.withSentry() (sourcing options from a co-located instrument.server.* file, falling back to env), and wraps configured classes with the matching helper: Durable Objects with instrumentDurableObjectWithSentry, Workflows with instrumentWorkflowWithSentry, and Agents SDK classes with instrumentAgentWithSentry. Both vite build and vite dev are instrumented. Entries you wrapped yourself are left alone, so this is safe alongside manual instrumentation. The plugin also adds the bindings that resolve to the wrapped classes to rpcTracePropagationBindings. Set to false to opt out.
If you deploy with wrangler directly, moving to Vite is straightforward:
- Set up the Cloudflare Vite plugin and add a
vite.config.tswith thecloudflare()andsentryCloudflareVitePlugin()plugins as shown above. - Run
vite buildbeforewrangler deploy, and usevite devin place ofwrangler devfor local development.
Your existing wrangler.jsonc becomes the input config, and the plugin generates the deployed output during the build. For the full list of fields that change or become redundant, see Cloudflare's Migrating from Wrangler guide.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").