@dudousxd/nestjs-diagnostics

中文文档
0.11.0发布于 2026/11/13MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1.2万

A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.

原始使用文档
文档目录11 节
  1. The convention
  2. Emitting
  3. Trace correlation
  4. Observing (the registry)
  5. First-party nestjs:* channels
  6. Recent events
  7. MCP dev tools
  8. API
  9. Reacting to events in NestJS — @OnDiagnostic
  10. Across processes: transports and role
  11. License

@dudousxd/nestjs-diagnostics

A tiny, standard convention for @dudousxd/nestjs-* libraries to emit observability events over Node's built-in diagnostics_channel — with zero overhead when nobody is listening, and a single generic Telescope watcher (@dudousxd/nestjs-diagnostics-telescope) that records them all.

The convention

Every event flows over a channel named:

aviary:<lib>:<event>
  • aviary is the fixed prefix for this family of libraries.
  • <lib> identifies the emitting library, e.g. billing, audit, jobs.
  • <event> is the event within that library, e.g. invoice-paid.

Each publish carries a DiagnosticEvent envelope:

interface DiagnosticEvent<TPayload = unknown> {
  ts: number; // Date.now() at publish time
  lib: string; // the <lib>
  event: string; // the <event>
  traceId?: string; // auto-filled from a context accessor when available
  payload: TPayload; // your library-defined data
}

Emitting

Call emit from your provider wherever something interesting happens:

import { Injectable } from '@nestjs/common';
import { emit } from '@dudousxd/nestjs-diagnostics';

@Injectable()
export class BillingService {
  async markInvoicePaid(invoiceId: string, amount: number) {
    // … your domain logic …
    emit('billing', 'invoice-paid', { invoiceId, amount });
  }
}

emit builds and publishes the envelope only when the channel has subscribers (channel.hasSubscribers), so a production process with no observer attached pays essentially nothing per call. It never throws — observability must not break the emitting code path.

Need an explicit trace id? Pass it in opts — e.g. inside a handler that already holds a correlation id:

emit('billing', 'invoice-paid', payload, { traceId });

Trace correlation

If your app uses @dudousxd/nestjs-context (an optional peer — never imported here), register its accessor once and every emit will auto-fill traceId:

import { setContextAccessor, CONTEXT_ACCESSOR } from '@dudousxd/nestjs-diagnostics';

// e.g. in a Nest module after resolving the optional CONTEXT_ACCESSOR provider:
setContextAccessor(accessor);

CONTEXT_ACCESSOR is Symbol.for('@dudousxd/nestjs-context:accessor') — the same token nestjs-context publishes under. Any object structurally matching the ContextAccessor interface works.

Observing (the registry)

diagnostics_channel has no wildcard subscription — you can only subscribe to a channel by exact name. So this package keeps a registry of every aviary:<lib>:<event> channel it has created or emitted on:

import { registeredChannels, onChannelRegistered } from '@dudousxd/nestjs-diagnostics';

// subscribe to all current channels…
for (const name of registeredChannels()) subscribe(name);
// …and any registered in the future
const off = onChannelRegistered(subscribe);

This is exactly how @dudousxd/nestjs-diagnostics-telescope's single generic watcher records every diagnostic event in the Telescope dashboard.

First-party nestjs:* channels

The official NestJS packages publish their own events on diagnostics_channel with the convention nestjs:<lib>:<type>: a raw payload, with no envelope and no traceId (see docs.nestjs.com). nestjs:* is the first-party convention, and Aviary never publishes on it. aviary:* is only for what the official packages don't emit.

subscribeOfficial lets one consumer record both. It wraps each official payload in the Aviary envelope, with traceId taken from the context accessor and source: 'nestjs':

import { subscribeOfficial } from '@dudousxd/nestjs-diagnostics';

const off = subscribeOfficial('resilience', ['retry', 'circuit-open'], (event, channel) => {
  // event: { v, ts, lib: 'resilience', event: 'retry', traceId, payload, source: 'nestjs' }
});
subscribeOfficial('authentication', 'all', record);
off();

The channel lists are exported per lib in one file, pinned to the official versions they were verified against (OFFICIAL_PACKAGE_VERSIONS). A test re-reads the installed packages and fails if a channel is added, renamed or removed.

LibConstantChannels
@nestjs/resilience 0.0.3NESTJS_RESILIENCE_CHANNELS9
@nestjs/workflows 0.0.5NESTJS_WORKFLOWS_CHANNELS19
@nestjs/authentication 0.1.5NESTJS_AUTHENTICATION_CHANNELS13
@nestjs/authorization 0.1.3NESTJS_AUTHORIZATION_CHANNELS1 (denied)
@nestjs/mail 0.0.2NESTJS_MAIL_CHANNELS2 (sent, failed)

These channels are not added to the aviary:* registry (registeredChannels/onChannelRegistered). Their lists are fixed, so subscribe to them explicitly.

Recent events

DiagnosticsModule keeps the last events of the process (aviary:* events, trace() span phases and nestjs:* events) in a fixed-size ring, with secret-looking keys redacted and payloads cut to 2 KB. It is on unless NODE_ENV is 'production'; recent: true | { size } | false overrides that.

DiagnosticsModule.forRoot({ recent: { size: 1000 } });

// inject DiagnosticsRecentEvents, or anywhere:
import { recentEvents } from '@dudousxd/nestjs-diagnostics';
recentEvents({ lib: 'durable', event: /run\.failed/, traceId: 'req-42', limit: 50 });

MCP dev tools

With @dudousxd/nestjs-agent-mcp-server mounted, DiagnosticsModule registers three read-only tools on the Aviary dev MCP endpoint: diagnostics_list_channels, diagnostics_list_listeners and diagnostics_list_recent_events. Diagnostics has no console, so the endpoint's devTools.authorize gates them: without it they are open outside production and denied in production. devTools: false leaves them out.

API

ExportDescription
emit(lib, event, payload, opts?)Build + publish a DiagnosticEvent on aviary:<lib>:<event> (only when subscribed).
getChannel(lib, event)The memoized diagnostics_channel for a pair (also registers its name).
channelName(lib, event)The aviary:<lib>:<event> string.
CHANNEL_PREFIX'aviary'.
registeredChannels()Snapshot of every registered channel name.
onChannelRegistered(cb)Notified once per future channel registration; returns an unsubscribe.
setContextAccessor(accessor | null)Register the accessor emit reads traceId from.
CONTEXT_ACCESSORShared DI token for the optional context accessor.
subscribeOfficial(lib, types | 'all', fn)Subscribe to first-party nestjs:<lib>:<type> channels, each payload wrapped in the envelope (+ source: 'nestjs'). Returns an unsubscribe.
OFFICIAL_CHANNELS, NESTJS_*_CHANNELS, OFFICIAL_PACKAGE_VERSIONS, officialChannelNames()The pinned first-party channel lists.
recentEvents(filter?)The newest buffered events (newest first), [] when the buffer is off.
startRecentEvents(size?), stopRecentEvents(), recentEventsBuffer()Run the recent-events buffer without DiagnosticsModule.
DiagnosticEvent, EmitOptions, ContextAccessor, OfficialDiagnosticEventTypes.

Reacting to events in NestJS — @OnDiagnostic

The @dudousxd/nestjs-diagnostics/nestjs subpath adds an ergonomic way to react to any aviary:<lib>:<event> in a NestJS app — no extra event library. Register the module once, then decorate provider methods:

import { Module } from '@nestjs/common';
import { DiagnosticsModule } from '@dudousxd/nestjs-diagnostics/nestjs';

@Module({ imports: [DiagnosticsModule.forRoot()] })
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { OnDiagnostic } from '@dudousxd/nestjs-diagnostics/nestjs';
import type { DiagnosticEvent } from '@dudousxd/nestjs-diagnostics';

@Injectable()
export class Reactions {
  @OnDiagnostic('resilience', 'circuit-opened') // one exact channel
  onCircuitOpen(e: DiagnosticEvent) { /* … */ }

  @OnDiagnostic('resilience')                   // every aviary:resilience:* channel
  onAnyResilience(e: DiagnosticEvent) { /* … */ }
}

A handler runs on the DI-resolved instance (injected dependencies work). A handler that throws or rejects is logged and swallowed — it can never break the code that emitted. @nestjs/common + @nestjs/core are optional peers; the main barrel stays framework-agnostic.

event also takes a RegExp. Each listener has a stable id, <ClassName>#<method>.

Across processes: transports and role

Pick a default transport to run listeners across processes:

import { DiagnosticsModule, transports } from '@dudousxd/nestjs-diagnostics/nestjs';

DiagnosticsModule.forRoot({
  role: 'api', // 'all' (default) | 'api' | 'worker'
  default: 'queue',
  transports: {
    redis: transports.redis({ pub: redis, sub: redis.duplicate() }), // broadcast, best-effort
    queue: transports.queue({ connection: { host: 'localhost', port: 6379 } }), // BullMQ, retried
    durable: transports.durable({ retries: 5 }), // durable runs (@dudousxd/nestjs-durable-diagnostics)
  },
});
transportwhere the listener runslost if the process dies?if the handler throws
none (local)in the emitting processyeslogged, dropped
redisin every process (broadcast)yeslogged, dropped
queuein one workernoBullMQ retries the job (at-least-once)
durablein one durable workernodurable retries with backoff; idempotent per event id

See the Listeners and transports docs.

License

MIT

版本与发布记录

@dudousxd/nestjs-diagnostics 共发布 17 个版本,首次发布于 2026/06/18;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.11.0latest2026/11/13今天
0.10.02026/11/121 天前
0.9.02026/11/121 天前
0.8.12026/11/094 天前
0.8.02026/11/085 天前
0.7.22026/09/122 个月前

关于 @dudousxd/nestjs-diagnostics 的常见问题

@dudousxd/nestjs-diagnostics 是什么?

A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.

如何安装 @dudousxd/nestjs-diagnostics?

在项目目录运行 npm install @dudousxd/nestjs-diagnostics,或使用 pnpm add / yarn add / bun add。

@dudousxd/nestjs-diagnostics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dudousxd/nestjs-diagnostics 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dudousxd/nestjs-diagnostics 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dudousxd/nestjs-diagnostics 可以商用吗?

@dudousxd/nestjs-diagnostics 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dudousxd/nestjs-diagnostics 的下载量有多少?

根据 npm 官方下载统计,@dudousxd/nestjs-diagnostics 在 2026/10/13 – 2026/11/11 这 30 天内被下载 11,583 次,日均约 386 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dudousxd/nestjs-diagnostics 还在维护吗?

最新版本 0.11.0 发布于 2026/11/13(今天);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/06/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。