Skip to content

Nitro: instrument through the runtime SDKs and their bundler plugins, deprecate @sentry/nitro #24900

Open

Description

@mydea

Goal

Nitro users install the server SDK for their runtime and configure the build in one place: the Nitro module for Nitro CLI apps, sentryVitePlugin for Vite apps. Errors, tracing, tasks and source maps then work in dev and prod. @sentry/nitro gets deprecated, then removed in v12.

// Nitro CLI: nitro.config.ts
import { sentryNitro } from '@sentry/node/nitro';
export default defineNitroConfig({ modules: [sentryNitro({ org, project, authToken })] });

// Vite (nitro/vite): vite.config.ts. No module in nitro.config.ts.
import { sentryVitePlugin } from '@sentry/node/vite';
export default defineConfig({ plugins: [nitro(), sentryVitePlugin({ org, project, authToken })] });

// Both: sentry.server.config.ts
import * as Sentry from '@sentry/node';
Sentry.init({ dsn: '...', tracesSampleRate: 1 });

Design

The module lives in @sentry/server-utils and is re-exported as @sentry/{node,bun,deno,cloudflare}/nitro. Its setup(nitro) does four things:

  • Tracing channels: when tracingChannel is unset, it sets Nitro's defaults ({ srvx, h3, unstorage, db0 }) and adds #nitro/virtual/tracing. That repeats Nitro's resolver (src/config/resolvers/tracing.ts), because modules run after it. A user's false stays. Route names, Server-Timing, and the middleware, handler, srvx and cache spans come from the nitroIntegration subscribers.
  • Runtime plugin: it registers one Nitro plugin, first in plugins.
    • The plugin imports sentry.server.config.{ts,js,mjs,cjs,mts,cts} from rootDir, or the file in the serverConfigFile option (false turns this off).
    • It then registers the error hook and the task wrapper, like @sentry/nitro's plugin does.
    • Users need no definePlugin and no serverDir.
    • The app always has a plugin, so it always has hooks, and errors from later plugins are captured.
  • Orchestrion transform: added through Nitro's build:config hook, for third-party library spans. Errors and tasks don't need orchestrion.
  • Source maps: hidden source maps when sourcemap is unset, uploaded from .output/server on compiled.

When the Vite plugin installs the module, the Vite plugin does the transform and the source maps instead. Only one module instance runs per Nitro app; see Duplicates.

A prototype of the module with no Sentry bundler plugin ran on nitro 3.0.261103-rc under nitro build, vite build and vite dev. In all three, init() ran, the native spans had route names, and Prisma 8 was instrumented.

Configuration

Setup User adds Build options live in Transform Server source maps
Nitro CLI sentryNitro() in modules of nitro.config.ts module options module, via build:config module: Nitro's sourcemap, upload on compiled
Vite (nitro/vite), including frameworks on Nitro sentryVitePlugin() in vite.config.ts Vite plugin options Vite plugin, in every server environment (nitro and SSR services) Vite plugin: Vite's build.sourcemap, upload for every environment
  • Build options are org, project, authToken, release, sourcemaps, serverConfigFile and buildTimeInstrumentation. Sentry.init() options always go in sentry.server.config.ts.
  • Vite: sentryVitePlugin's nitro property installs the module with the Vite plugin's options. That module still sets the tracing channels and registers the runtime plugin, but it adds no transform and uploads nothing. Users don't add the module to nitro.config.ts.
  • SSR services: because the Vite plugin transforms every server environment, frameworks' SSR services are covered too. The module's build:config transform would only reach the nitro environment.
  • Module alone under Vite: it works (prototype) but isn't the documented setup. It doesn't cover client builds or SSR services, and the nitro environment likely takes its source maps from Vite's build.sourcemap, not Nitro's sourcemap.
  • First request under vite dev: Vite loads the server entry, and with it init(), on that request. Its h3 spans become segments without an http.server parent.

Duplicates

At setup, each module instance checks nitro.options.modules for other Sentry instances. Nitro adds the Vite plugins' modules to the config's modules before createNitro, so every instance sees all of them.

  • One instance wins: the one from a Vite plugin, otherwise the first in modules. The others do nothing and warn that Sentry is configured more than once, naming whose options are used. Options are never merged, so a duplicated setting such as sourcemaps comes only from the winner.
  • Module in nitro.config.ts plus sentryVitePlugin: the Vite plugin's instance wins. The warning says to remove the module.
  • withSentryConfig (@sentry/nitro) plus the module or sentryVitePlugin: the same rule applies. withSentryConfig still sets tracingChannel: true itself.
  • Module plus sentryRollupPlugin in rollupConfig.plugins (Nitro CLI): not supported. A file transformed twice fails the build, so the module spots the Sentry Rollup plugin in build:config, skips its own transform and warns. The Rollup plugin would still upload source maps a second time.
  • Two Sentry Vite plugins: the existing guard lets only the first one transform, and only one module instance runs.
  • Source map generation set by the user (Nitro's sourcemap or Vite's build.sourcemap): the user's value wins. Sentry only fills it in (hidden) when it's unset, and logs at debug level when maps it needs are off.

@sentry/nitro

  • withSentryConfig keeps setting tracingChannel: true before Nitro resolves it. It installs the shared module instead of its own.
  • Its init (SDK metadata, dev server http filter) and /import stay for apps that preload instrument.mjs.
  • Both installed: see Duplicates.
  • instrument.mjs and sentry.server.config.ts both call init(): the first call wins (docs/repeated-init.md).
  • Migration:
    • replace withSentryConfig(config) with the module (Nitro CLI) or sentryVitePlugin (Vite), as in Configuration
    • move init into sentry.server.config.ts, imported from @sentry/node
    • drop --import ./instrument.mjs

When --import @sentry/node/import is needed

Where Needed Why
Prod not by default Nitro bundles every module Sentry instruments. Only needed for instrumented packages that stay external (nf3's lists, traceDeps).
nitro dev for third-party library spans Nitro keeps node_modules external in dev.
vite dev for most third-party libraries Vite keeps dependencies external in dev. Prisma 8 fired without it.
No module always, plus tracingChannel: true Best effort.

Steps

  1. Core module and @sentry/node/nitro:
    • shared capture helpers: HTTPError filtering, mechanism, contexts.nitro, flushIfServerless
    • tracing channels, the runtime plugin with the server config, the transform
    • choosing one instance and warning about the others (Duplicates)
    • the node-nitro-3 e2e app on the module with the Nitro CLI, dev and prod, with and without serverDir
  2. Vite:
    • sentryVitePlugin's nitro property installs the module with the Vite plugin's options, without its transform or upload
    • the Vite variants of node-nitro-3 use only sentryVitePlugin
    • an e2e variant with both the module and the Vite plugin checks that the warning appears and nothing is duplicated
  3. Source maps:
  4. @sentry/nitro on the shared module, with a test that installs both.
  5. Bun and Deno: /nitro exports, plus e2e tests for their presets.
  6. Cloudflare /nitro: investigate first. It needs init per request through fetch, the dcModule façade, and workerd tests.
  7. Migrate and deprecate: move SolidStart off setupSentryNitroModule, rewrite the Nitro guide in getsentry/sentry-docs, deprecate @sentry/nitro, and remove it in v12.

Asks to Nitro, in parallel: a native tasks channel, and tracingChannel on by default.

Open questions

  • Where do the SDK metadata (nitro) and the tunnel base path come from without @sentry/nitro?
  • Mechanism types: keep auto.function.nitro.captureErrorHook and auto.function.nitro.task?
  • Nuxt 5: does Nuxt's Nitro also get the module through sentryVitePlugin? Nuxt registers sentry.server.config itself.
  • Prod with external instrumented packages: --import needs @sentry/node in node_modules, which an .output-only deployment doesn't have.

Findings (nitro 3.0.261103-rc)

  • Prod bundling: Nitro bundles everything except nf3's lists, which are native addons plus 19 packages such as prisma, @prisma/client and typeorm. None of the 67 modules Sentry instruments are on them.
  • Prisma 8 (@prisma/orm-postgres) is bundled and instrumented in prod. Prisma 5–7 use the global tracing helper, so @prisma/client staying external doesn't matter.
  • serverDir: Nitro scans <serverDir>/plugins/ only when serverDir is set, and Nitro 3 defaults it to false.
  • Plugin order: a plugin the module puts first runs before the plugins Nitro scans, both when the modules load and when the plugins run.
  • Hooks: with tracing channels on, h3's onError reaches captureError and the error hook. Without plugins, the app has no hooks.

Earlier approach

The first plan put Nitro hooks in the Vite and Rollup plugins (#26809, #26858). It was dropped for three reasons:

  • the Rollup path needed the private globalThis.__nitro_instances__
  • the Rollup path ran after build:before
  • it put Nitro-only options on generic plugins

via @mydea.

--

View Session [Sentry]

Activity

  1. linear-code commented on Sep 30, 2026

    @linear-code
  2. changed the title [-]Nitro: move the runtime error-hook plugin out of @sentry/nitro for full cross-runtime instrumentation[/-] [+]Nitro: instrument through the runtime SDKs and their bundler plugins, deprecate `@sentry/nitro`[/+] on Nov 23, 2026
  3. sentry-junior commented on Nov 24, 2026

    @sentry-junior
    Contributor

    Step 0 update

    • Shared capture helpers: ref(nitro): Move request and task error capture into server-utils #26805.
    • Marking primitives: these don't land up front anymore. Each step 1 PR adds the marker it needs next to the code that reads it.
      • @sentry/nitro's runtime plugin bundles its own copy of @sentry/server-utils, because nf3 doesn't externalize it. A marker that crosses between @sentry/nitro and the orchestrion path therefore has to sit on a shared object, such as a non-enumerable property on the Nitro app. Module-scope state won't work.
      • The task flag is likely unnecessary. @sentry/nitro's wrapper captures first and core marks the error as captured, so the orchestrion side can skip errors where isAlreadyCaptured is true.
      • Both sides of the WeakSet<H3Event> live in nitroIntegration, so module scope is fine there.
    • Orchestrion fixture: I closed test(nitro): Add an orchestrion fixture for Nitro and h3 internals #26806 without merging. The errors PR in step 1 creates the e2e app instead: it adds its targets straight to nitroConfig and asserts real events. The feat/nitro-orchestrion-fixture branch has the scaffolding to reuse: the four build modes, the runtime hook preload, and no event proxy.

    Findings from #26806

    Tested with nitro 3.0.261103-rc, h3 2.0.8, vite 8.3.8 and rolldown 1.3.3, under vite build, vite dev, nitro build and nitro dev.

    • Nitro has left beta, and latest is now an -rc build. Version ranges need >=3.0.0-0.
    • Every target fired:
      • useNitroApp
      • runTask
      • prepareResponse
      • H3Core#handler, via { className: 'H3Core', methodName: 'handler' } in dist/cache.mjs
      • callLayer in dist/middleware.mjs, which only fires when there is global middleware
      • the NitroDevApp constructor, only through the runtime hook on the main thread
    • The useNitroApp end event carries the app as result, with the keys captureError, fetch, h3 and hooks. This settles that item under "Not verified yet".
    • Gap for error capture: with no Nitro plugins, Nitro compiles hooks: void 0 and a captureError that never calls the error hook. Registering the shared hook from the useNitroApp end event only works when the app has a plugin or sets features.runtimeHooks: true. The errors PR has to decide how to handle this, for example by wrapping captureError on the app.
    • useNitroApp caches the app, but its channel fires on every call, and serverFetch calls it on every request. The subscriber has to be idempotent.
    • prepareResponse calls itself again on the error path (nested), so it fires twice for one error.
    • Nitro's Vite plugin merges rollupConfig.plugins into Vite builds. A setup with both sentryVitePlugin and sentryRollupPlugin would transform the same modules twice.
    • Under nitro dev without the runtime hook, only useNitroApp and runTask fire, because h3 stays external. Under vite dev, the plugin alone covers everything except NitroDevApp.
    • @sentry/node/import only applies the built-in configs. Until the targets are in nitroConfig, testing them through the runtime hook needs a custom load hook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions