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

未关闭
#24,900 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
活跃
技术栈
typescript

调研方向

Start by reading packages/nitro/src/runtime/plugins/server.ts, packages/nitro/src/runtime/hooks/captureErrorHook.ts, packages/nitro/src/config.ts, and the shared Nitro integration and orchestrion files referenced above. Determine whether Nitro can provide a diagnostics-channel error surface or whether runtime app registration is viable across runtimes, then confirm how tracingChannel should be enabled. Done means runtime SDKs receive full Nitro instrumentation, including error capture, without requiring @sentry/nitro.

由索引模型根据 Issue 内容生成。

描述

Feature

Follow-up to the nitro → @sentry/server-utils move (#24861). That move made the span, cache, and Server-Timing instrumentation shared (nitroIntegration + nitroServerTimingIntegration in @sentry/server-utils), and they auto-register on bundler-only SDKs like @sentry/cloudflare via orchestrion registration-only configs. But a plain Nitro app running on another runtime (Cloudflare/Deno/Bun/Node) via just its runtime SDK still does not get the full instrumentation, because two pieces remain tied to @sentry/nitro:

  1. The runtime error-hook plugin — registers captureErrorHook on the Nitro app's error hook.
  2. Enabling Nitro's tracingChannel — a build-time config flag that turns on the native h3/srvx/unstorage channels the integrations subscribe to.

(Source-map upload also lives in @sentry/nitro, but that's a build concern handled by the bundler/framework plugin and is out of scope here.)

Goal: a Nitro app deployed on any runtime gets the full Nitro instrumentation — including error capture — from its runtime SDK alone, without depending on @sentry/nitro.

Why the error hook can't be a runtime integration today

@sentry/nitro/src/runtime/plugins/server.ts is a Nitro app plugin:

export default definePlugin(nitroApp => {
  nitroApp.hooks.hook('error', captureErrorHook);
});
  • It uses nitroApp.hooks.hook('error', …) — a Nitro-app API. A Sentry integration's setup(client) only has the client, not the Nitro app, so it cannot register this hook.
  • The plugin is injected at build time by @sentry/nitro's module (instrumentServer → addPlugin). Without that build module, nothing injects it.
  • captureErrorHook is a broad error boundary that the tracing channels can't replace (this is why it wasn't moved with the rest). It catches errors from Nitro's captureError call sites: h3 onError, onRequest/onResponse hooks (tags: ['request'|'response']), plugin init (['plugin']), cache (['cache']), and process-level unhandledRejection/uncaughtException (error/hooks.mjs). It also filters HTTPError 3xx/4xx, attaches contexts.nitro (method/path/tags), and awaits flushIfServerless(). The tracing channels only see errors thrown through a traced handler, so they'd lose most of this.

Approaches to make it cross-runtime

1. (Preferred, needs Nitro upstream) Nitro emits errors on a diagnostics channel

If Nitro published its error boundary through a node:diagnostics_channel (e.g. nitro.error) alongside hooks.callHook('error', …), a runtime integration (nitroIntegration, or a dedicated nitroErrorIntegration) could subscribe to it — fully runtime-agnostic, no app plugin and no build-time injection needed, and it would auto-register on Cloudflare via the same orchestrion mechanism already used for spans. Blocked on a Nitro feature request / upstream change.

2. Register the hook at runtime via the Nitro app global

Nitro stores the app on globalThis.__nitro__[APP_ID] and exposes useNitroApp(). A runtime integration could obtain the app and call nitroApp.hooks.hook('error', captureErrorHook) itself. Challenges to work out:

  • Timing: the app is created lazily (on first useNitroApp()), typically after Sentry.init(). The integration would need to defer registration (poll, patch useNitroApp, or hook app creation) so it doesn't miss the app or register twice.
  • Coupling: depends on Nitro's internal global shape (__nitro__, APP_ID) and the hookable API, which are less stable than a public channel.
  • HTTPError typing: captureErrorHook imports HTTPError from nitro/h3; a runtime-registerable version in @sentry/server-utils would need to vendor a minimal type or duck-type status (server-utils is framework-neutral).
3. Enabling tracingChannel without @sentry/nitro

The native channels only fire if the Nitro build sets tracingChannel: true (resolveTracingOptions in Nitro bails otherwise). Today only @sentry/nitro's withSentryConfig sets it. For a runtime-SDK-only setup, either:

  • the app author sets tracingChannel: true in nitro.config.ts, or
  • the framework module (Nuxt/SolidStart/…) sets it (note: see the Nuxt-adoption caveats — enabling it there conflicts with Nuxt's own instrumentation), or
  • document it as a required user step.
    This is inherently build-time, so a runtime SDK can't do it alone.

What each runtime SDK would need

Once approach 1 or 2 makes error capture runtime-registerable in @sentry/server-utils:

  • Re-export the error integration from each runtime SDK (as they already do for nitroIntegration).
  • Extend the orchestrion registration-only config so the error integration registers on bundler-only SDKs too (same mechanism as the span integration).

Open questions / blockers

  • Does Nitro have — or would it accept — a diagnostics-channel error surface? (Approach 1 hinges on this.)
  • Is reading the Nitro app from globalThis.__nitro__ reliable across runtimes and Nitro versions? (Approach 2.)
  • Is there any non-build path to enable tracingChannel? (Likely no.)
  • Should source-map upload stay in @sentry/nitro / the bundler plugin, or is a per-runtime story wanted there too?

References

  • Move PR: #24861
  • Error hook: packages/nitro/src/runtime/plugins/server.ts, packages/nitro/src/runtime/hooks/captureErrorHook.ts
  • tracingChannel enablement: packages/nitro/src/config.ts
  • Shared integrations: packages/server-utils/src/integrations/nitro/*
  • Orchestrion registration: packages/server-utils/src/orchestrion/config/nitro.ts, channel-integration-definitions.ts
主要语言
TypeScript
星标
8.8k
派生
1.9k
平均合并
1 天 8 小时
30 天内合并 PR
744

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

getsentry/sentry-javascript 的其他 Issue

查看 getsentry/sentry-javascript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。