UNPKG

@dudousxd/nestjs-diagnostics

Version:

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

118 lines • 5.37 kB
import diagnostics_channel from 'node:diagnostics_channel'; import { resolveTraceId } from './context-accessor.js'; import { onRegistryReset, registerChannel } from './registry.js'; /** The prefix all channels created by this convention share. */ export const CHANNEL_PREFIX = 'aviary'; /** * Current envelope schema version, stamped onto every emitted * {@link DiagnosticEvent} as `v`. Bump this only when the wire shape changes in a * way observers must adapt to. Observers should tolerate envelopes without `v` * (legacy emitters published before versioning) and treat them as version `1`. */ export const SCHEMA_VERSION = 1; /** * The channel name for a `<lib>`/`<event>` pair: `aviary:<lib>:<event>`. This is * the cross-repo wire contract — keep it identical on producer and observer. */ export function channelName(lib, event) { return `${CHANNEL_PREFIX}:${lib}:${event}`; } /** * Per-`(lib, event)` cache of the resolved {@link Channel}, keyed by `lib` then * `event`. The first {@link getChannel}/{@link emit} for a pair pays the string * concat + `diagnostics_channel.channel()` lookup + registry insert; every call * after that is a pair of `Map.get`s and returns the SAME object — no string is * allocated on the steady state. A nested map (rather than one keyed by the full * `aviary:<lib>:<event>` name) is what lets us skip the concat entirely. * * Node already returns a process-stable {@link Channel} per name, so caching it * here changes nothing observable: `getChannel(a,b)` still returns * `diagnostics_channel.channel('aviary:a:b')`. The registry is still fed exactly * once per pair (on the cache miss), so discovery is unchanged. */ const channelCache = new Map(); // Drop the memo cache whenever the registry is reset (test-only), so the next // getChannel re-registers every channel. See onRegistryReset in registry.ts. onRegistryReset(() => channelCache.clear()); /** * The memoized `node:diagnostics_channel` for a `<lib>`/`<event>` pair. Node * returns the same {@link Channel} object for the same name, so reading * `.hasSubscribers` is the cheap gate before building an envelope. Touching the * channel also records its name in the {@link registerChannel registry} so a * generic observer can discover it. * * Memoized via {@link channelCache}: only the first call for a `(lib, event)` * pair builds the name, resolves the channel, and registers it; subsequent calls * return the cached object after two map lookups. */ export function getChannel(lib, event) { let byEvent = channelCache.get(lib); if (byEvent !== undefined) { const cached = byEvent.get(event); if (cached !== undefined) return cached; } else { byEvent = new Map(); channelCache.set(lib, byEvent); } const name = channelName(lib, event); const channel = diagnostics_channel.channel(name); registerChannel(name); byEvent.set(event, channel); return channel; } /** * Emit a diagnostics event on `aviary:<lib>:<event>`. * * - The envelope's `ts` is `Date.now()` evaluated *inside* this call (never at * module load). * - `traceId` is taken from `opts.traceId` if given, else auto-filled from the * registered context accessor (if resolvable), else left undefined. * - The envelope is built and published ONLY when the channel `hasSubscribers`, * so emitting is effectively free when nothing is listening. * - When `opts.sample` is given, it is consulted AFTER the `hasSubscribers` gate * and BEFORE the envelope is built: a falsy result sheds this event without * allocating anything. Default (no `sample`) always publishes when subscribed. * - The envelope carries the current {@link SCHEMA_VERSION} as `v`. * - Never throws: emitting observability must not break the caller. * * ```ts * emit('billing', 'invoice-paid', { invoiceId: 'inv_123', amount: 4200 }); * // Shed 90% of a hot event: * emit('authz', 'decision', payload, { sample: () => Math.random() < 0.1 }); * ``` * * When `(lib, event)` is declared in the typed * {@link import('./types.js').ChannelRegistry ChannelRegistry}, `payload` is * checked against the declared type at compile time; every other pair keeps the * untyped `unknown` payload. The runtime behavior is identical either way. */ export function emit(lib, event, payload, opts) { const channel = getChannel(lib, event); if (!channel.hasSubscribers) return; try { // Load-shedding gate: consulted only when subscribed, before any allocation. // A throwing sampler is treated as a skip (caught below) — never published. if (opts?.sample !== undefined && !opts.sample()) return; const traceId = opts?.traceId ?? resolveTraceId(); const envelope = { v: SCHEMA_VERSION, ts: Date.now(), lib, event, traceId, payload, // Only set the key when a duration was provided — omitting it keeps the // envelope shape monomorphic (no undefined-valued key on common events). ...(opts?.durationMs !== undefined && { durationMs: opts.durationMs }), }; channel.publish(envelope); } catch { // Observability must never break the emitting code path. } } //# sourceMappingURL=channel.js.map