Skip to content

feat(nuxt): Set up Cloudflare from sentry.cloudflare.config.ts - #26754

Open
JPeer264 wants to merge 9 commits into
developfrom
jp/nuxt-cloudflare-instrument-server
Open

JPeer264 wants to merge 9 commits into
developfrom
jp/nuxt-cloudflare-instrument-server

Conversation

@JPeer264

@JPeer264 JPeer264 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

follow up to #26285

Description

This sets up @sentry/nuxt on Cloudflare from a sentry.cloudflare.config.ts in the app root. On a Cloudflare preset, the module wraps the Worker entry of Nitro with withSentry and adds the Nitro plugins of the Node setup, so route names, meta tags, middleware, storage, cache and database spans work on Cloudflare too. This works with Nitro 2 (Nuxt 4) and Nitro 3 (Nuxt 5).

// sentry.cloudflare.config.ts
import { defineCloudflareOptions } from '@sentry/cloudflare';

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

The handlers run in withEnv with the env that withSentry instruments, so code that reads env from cloudflare:workers, like db0 of Nitro 3, gets the instrumented bindings too. On Nitro 2 and Nitro 3, @sentry/cloudflare creates the D1 spans. Like on Node, the SDK ignores the server spans of requests to the build output (app.baseURL + app.buildAssetsDir).

This also deprecates sentryCloudflareNitroPlugin. When an app has sentry.cloudflare.config.ts and the plugin, the plugin does nothing and logs a warning.

Why

With sentryCloudflareNitroPlugin, the module adds no middleware, storage, cache or database spans, and the baggage meta tag has no route, because the plugin creates it before the route is known.

Known issues

  • nuxt dev does not run the Worker entry, because the cloudflare-dev preset extends nitro-dev. So the module does not wrap anything in dev.
  • The cloudflare-worker (service worker) preset of Nitro 2 has no default export to wrap. There the module only logs a warning.

Sentry traces

Before, Nitro 2 with sentryCloudflareNitroPlugin, /api/storage-test has only the http.server span: https://sentry-sdks.sentry.io/explore/traces/trace/b35a22b46b6f422d96954dacdac2b169/?project=4510555608449024&timestamp=1795360390
Before, Nitro 2 with sentryCloudflareNitroPlugin, /api/db-test has no database spans: https://sentry-sdks.sentry.io/explore/traces/trace/9a45858334834dd7aa6128aeee46c5b0/?project=4510555608449024&timestamp=1795360390
After, Nitro 2, /api/storage-test has middleware and cache spans: https://sentry-sdks.sentry.io/explore/traces/trace/da68b8bebd09438baee03d50d4697300/?project=4510555608449024&timestamp=1795360223
After, Nitro 2, /api/db-test has D1 spans from @sentry/cloudflare: https://sentry-sdks.sentry.io/explore/traces/trace/3854bccf179e416e8c2329e5d2fca05a/?project=4510555608449024&timestamp=1795360223
After, Nitro 3, /api/storage-test has middleware and cache spans: https://sentry-sdks.sentry.io/explore/traces/trace/d628817679c542909abcd6cf31ffca66/?project=4510555608449024&timestamp=1795361326
After, Nitro 3, /api/db-test has D1 spans from @sentry/cloudflare: https://sentry-sdks.sentry.io/explore/traces/trace/98d7910d5b0c491dacf1b84ddbb6ae14/?project=4510555608449024&timestamp=1795365743
Node, Nitro 2, /api/storage-test has the same middleware and cache spans: https://sentry-sdks.sentry.io/explore/traces/trace/dedb120e79a84db48d42025513022e5e/?project=4510555608449024&timestamp=1795431729
Node, Nitro 2, /api/db-test has db0 spans from @sentry/nuxt: https://sentry-sdks.sentry.io/explore/traces/trace/3ec8c1026f824b6398120a7ec3fa4eee/?project=4510555608449024&timestamp=1795431729
Node, Nitro 3, /api/storage-test has the same middleware and cache spans: https://sentry-sdks.sentry.io/explore/traces/trace/015523693d2e4b08b0000b3c5cc16cae/?project=4510555608449024&timestamp=1795431770
Node, Nitro 3, /api/db-test has db0 spans from @sentry/nuxt: https://sentry-sdks.sentry.io/explore/traces/trace/ae507aa3f3d1487696f6745b340e7d5b/?project=4510555608449024&timestamp=1795431770

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 1823984 (PR test merge using develop at 1f32728)

Against: 1f32728 (develop) · base run #67760

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.54 kB - -
@sentry/browser - with treeshaking flags 28.73 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.6 kB - -
@sentry/browser (incl. Tracing) 53.67 kB - -
@sentry/browser (incl. Tracing Core) 40.59 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 53.67 kB - -
@sentry/browser (incl. Tracing, Profiling) 56.57 kB - -
@sentry/browser (incl. Tracing, Replay) 93.54 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 82.41 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.93 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 111.34 kB - -
@sentry/browser (incl. Feedback) 48.4 kB - -
@sentry/browser (incl. sendFeedback) 31.11 kB - -
@sentry/browser (incl. FeedbackAsync) 36.17 kB - -
@sentry/browser (incl. Metrics) 31.86 kB - -
@sentry/browser (incl. Logs) 32.13 kB - -
@sentry/browser (incl. Metrics & Logs) 32.8 kB - -
@sentry/react 32.56 kB - -
@sentry/react (incl. Tracing) 56.38 kB - -
@sentry/vue 38.75 kB - -
@sentry/vue (incl. Tracing) 56.68 kB - -
@sentry/svelte 30.57 kB - -
@sentry/effect (client) 42.25 kB - -
@sentry/effect (client, incl. Tracing) 49.41 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 59.3 kB - -
@sentry/remix (Remix 3 client bundle) 59.39 kB - -
CDN Bundle 32.54 kB - -
CDN Bundle (incl. Tracing) 54.15 kB - -
CDN Bundle (incl. Logs, Metrics) 35.27 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 56.63 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 76.16 kB - -
CDN Bundle (incl. Tracing, Replay) 91.89 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 94.43 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.35 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.81 kB - -
CDN Bundle - uncompressed 94.92 kB - -
CDN Bundle (incl. Tracing) - uncompressed 160.25 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 103.26 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 167.88 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 233.5 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 280.54 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 288.17 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.47 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 300.09 kB - -
@sentry/nextjs (client) 60.09 kB - -
@sentry/sveltekit (client) 55.03 kB - -
@sentry/core/server 45.83 kB - -
@sentry/core/browser 14.46 kB - -
@sentry/node 156.42 kB -0.01% -2 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.45 kB - -
@sentry/node - without tracing 89.28 kB -0.01% -2 B 🔽
@sentry/node - without channel injection 145.35 kB -0.01% -2 B 🔽
@sentry/aws-serverless 99.92 kB -0.01% -3 B 🔽
@sentry/cloudflare (withSentry) - minified 211.93 kB - -
@sentry/cloudflare (withSentry) 491.77 kB - -
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 214.88 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 237.17 kB - -

View base workflow run

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

All optional checks were successful. 12 successful. See the run.

Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 39s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps enabled by Sentry) Test (optional) Successful in 42s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel Test (optional) Successful in 39s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-cloudflare Test (optional) Successful in 36s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (kv-cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-4-sourcemaps (Vite server) Test (optional) Successful in 42s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (development) Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 44s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel (cloudflare) Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (no upload) Test (optional) Successful in 39s Details

@JPeer264
JPeer264 added this pull request to stack #26767 November 23, 2026 13:15
@JPeer264
JPeer264 force-pushed the jp/nuxt-cloudflare-instrument-server branch from 3a602e9 to 20c3ddf Compare November 24, 2026 07:05
@JPeer264 JPeer264 self-assigned this Nov 24, 2026
@JPeer264
JPeer264 marked this pull request as ready for review November 24, 2026 07:37
@JPeer264
JPeer264 requested a review from s1gr1d November 24, 2026 07:37
Comment on lines +72 to +73
* @deprecated Add an `instrument.server.ts` file to the root of the Nuxt app instead. Its default export returns the
* options of `@sentry/cloudflare`, and the Sentry Nuxt module wraps the Worker with `withSentry`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* @deprecated Add an `instrument.server.ts` file to the root of the Nuxt app instead. Its default export returns the
* options of `@sentry/cloudflare`, and the Sentry Nuxt module wraps the Worker with `withSentry`.
* @deprecated Add an `instrument.server.ts` file with the Sentry setup to the root of the Nuxt app instead. Its default export returns the
* options of `@sentry/cloudflare`, and the Sentry Nuxt module wraps the Worker with `withSentry`.

Not sure if we should leave the second sentence "Its default export ..." as it's almost too internal-technical information for users.

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.

removed without hesitation

Comment thread packages/nuxt/src/vite/cloudflare.ts Outdated
*/
export function setupCloudflareEntry(nuxt: Nuxt, optionsFile: string): void {
nuxt.hook('nitro:init', nitro => {
if (nuxt.options._prepare || !isCloudflarePreset(nitro.options.preset)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (nuxt.options._prepare || !isCloudflarePreset(nitro.options.preset)) {
if (nuxt.options._prepare || nitro.options.dev || !isCloudflarePreset(nitro.options.preset)) {

cloudflare-dev (which is used in dev) would also be matched here. But we should probably skip this in dev, no? Did you check/test devmode already?

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.

Good catch. In nuxt dev, with Nitro 2, uses the cloudflare-dev preset, which apparently extends nitro-dev, so there is no Worker entry to wrap as it's not really using miniflare, and the module printed a wrong warning which is gone now.

Nitro 3 dev, which actually runs miniflare fails currently also without Sentry, so I couldn't test it there. I will do a follow up there.

Comment on lines +38 to +51
export async function findCloudflareOptionsFile(
nuxt?: Nuxt,
options?: SentryNuxtModuleOptions,
): Promise<string | undefined> {
return findConfigFile('instrument.server', nuxt, options);
}

async function findConfigFile(
basename: string,
nuxt?: Nuxt,
options?: SentryNuxtModuleOptions,
): Promise<string | undefined> {
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'];
const relativePaths = possibleFileExtensions.map(ext => `sentry.${type}.config.${ext}`);
const relativePaths = possibleFileExtensions.map(ext => `${basename}.${ext}`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We deprecated and removed support for the instrument.server file in v11. It's a bit weird if this file comes back again. Maybe we could rename it to sentry.cf.config.ts, sentry.cloudflare.config.ts or sentry.workers.config.ts?

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.

Good call. I like sentry.cloudflare.config.ts actually most (since it is our Cloudflare SDK, even though "workers" would be more accurate). I like it so much that is something we could and should actually support everywhere by default.

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.

moved it now to the new config file name.

// eslint-disable-next-line no-console
console.warn(
`[Sentry] Found \`${basename(configPath)}\`, but the Nitro preset targets Cloudflare, where this file is not used. Set up the SDK with \`sentryCloudflareNitroPlugin\` instead: https://docs.sentry.io/platforms/javascript/guides/nuxt/install/cloudflare-workers/`,
`[Sentry] Found \`${basename(configPath)}\`, but the Nitro preset targets Cloudflare, where this file is not used. Set up the SDK in \`instrument.server.ts\` instead: https://docs.sentry.io/platforms/javascript/guides/nuxt/install/cloudflare-workers/`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
`[Sentry] Found \`${basename(configPath)}\`, but the Nitro preset targets Cloudflare, where this file is not used. Set up the SDK in \`instrument.server.ts\` instead: https://docs.sentry.io/platforms/javascript/guides/nuxt/install/cloudflare-workers/`,
`[Sentry] Found \`${basename(configPath)}\`, but the Nitro preset targets Cloudflare, where this file is not used. Set up the SDK in \`......\` instead: https://docs.sentry.io/platforms/javascript/guides/nuxt/install/cloudflare-workers/`,

Make sure this name is matched with whatever is used in the end.

Also, we need to update the docs because this is showing the old setup. I would propose we still show the deprecated setup steps in the docs (in case people are on old versions - we can also add versioned docs for that) and also show a mini migration guide.

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.

Let's work on this together since it seems you already have a clear idea in your head on how this could work out.


// `withSentry` sets no transaction name, so errors captured during the request would have none.
// This is the same best-effort name the Node SDK sets when a request starts.
nitroApp.hooks.hook('request', (event: H3Event) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a test for this request hook for a v1 and v2 H3Event?

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.

Yap, added

@JPeer264 JPeer264 changed the title feat(nuxt): Set up Cloudflare from instrument.server.ts feat(nuxt): Set up Cloudflare from sentry.cloudflare.config.ts Nov 24, 2026
JPeer264 and others added 8 commits November 24, 2026 14:56
On a Cloudflare preset, the Nuxt module now wraps the Nitro Worker entry
with `withSentry` of `@sentry/cloudflare` and gets the options from the
default export of `instrument.server.ts` in the app root. This is the same
file that the Cloudflare Vite plugin reads. Nitro builds the Worker itself,
with Rollup in Nitro 2 and with its own Vite build in Nitro 3, so the
Cloudflare Vite plugin cannot wrap it. The client reports the Nuxt SDK name.

With Rollup, the bundler plugin of the module replaces the entry when Rollup
resolves it. Vite resolves the entry before it asks the plugin, so there the
plugin replaces the code of the entry module in `transform`.

When the Nitro entry exports a `$DurableObject` class, as with the
`cloudflare_durable` preset, the module also wraps it with
`instrumentDurableObjectWithSentry`, so a request that Nitro sends to it
with `durableFetch` continues the trace of the Worker.

With this file, the module also adds the Nitro plugins of the Node setup
(errors, route names, meta tags, middleware, storage and database spans)
and names the transaction of each request. On Cloudflare, the orchestrion
transform now imports `tracingChannel` from the workerd facade of
`@sentry/cloudflare`, which defers the events of module-scope calls until a
request runs.

`withSentry` instruments the bindings of the `env` that it gives to the
handlers. db0 of Nitro 2 reads the D1 bindings from this `env`, but db0 of
Nitro 3 reads them from the `env` of `cloudflare:workers`. So the wrapped
entry runs each handler, and each method of the Nitro Durable Object, in
`withEnv` of `cloudflare:workers` with the instrumented `env`. Then the
code of the app that reads `env` from `cloudflare:workers` also gets the
instrumented bindings. The database instrumentation of the module leaves
out the databases with the `cloudflare-d1` connector, so on Nitro 2 and
Nitro 3, `@sentry/cloudflare` creates the one span of each D1 query.

`sentryCloudflareNitroPlugin` is deprecated. In an app that has
`instrument.server.ts` and also the plugin, the plugin does nothing and
prints a warning, so `withSentry` is the only wrapper of a request.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JPeer264
JPeer264 requested a review from s1gr1d November 24, 2026 14:17
@JPeer264
JPeer264 force-pushed the jp/nuxt-cloudflare-instrument-server branch from cd30cce to 5edb5da Compare November 24, 2026 14:48
@JPeer264

Copy link
Copy Markdown
Member Author

@s1gr1d FYI after the rebase I had to adjust the code to make the tests pass: 5edb5da. So the PR got a bit bigger.

@s1gr1d s1gr1d left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a mini comment, otherwise good

Comment on lines +37 to +41
const options = optionsCallbackWithMetadata(env);
return {
...options,
ignoreSpans: [...(options.ignoreSpans || []), ...getBuildAssetRequestFilters(buildAssetsPath)],
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The sdk setup uses _INTERNAL_captureOptionsSetByUser, should we maybe also check that here? so it captures the user options first

This branch has not been deployed

No deployments
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