@dudousxd/nestjs-diagnostics
Version:
A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.
297 lines • 11.5 kB
JavaScript
import diagnostics_channel from 'node:diagnostics_channel';
import { CHANNEL_PREFIX } from './channel.js';
import { OFFICIAL_CHANNELS, officialChannelNames, subscribeOfficial, } from './official.js';
import { onChannelRegistered, registeredChannels } from './registry.js';
/** Default number of events the recent-events buffer keeps. */
export const DEFAULT_RECENT_SIZE = 500;
/** Bytes of serialized payload kept per buffered event. */
export const RECENT_PAYLOAD_BYTES = 2048;
/** The span phases the buffer keeps (`asyncStart` carries nothing of its own). */
export const RECENT_SPAN_PHASES = ['start', 'end', 'asyncEnd', 'error'];
const SECRET_KEY = /(secret|token|password|passwd|authorization|cookie|api[_-]?key|credential|private[_-]?key)/i;
const MAX_DEPTH = 8;
function describeError(error) {
if (error instanceof Error)
return { name: error.name, message: error.message.slice(0, 500) };
return { message: String(error).slice(0, 500) };
}
/** A JSON-safe copy of `value` with secret-looking keys replaced by `'[redacted]'`. */
function redact(value, depth, seen) {
if (typeof value === 'bigint')
return value.toString();
if (typeof value === 'function' || typeof value === 'symbol')
return undefined;
if (value === null || typeof value !== 'object')
return value;
if (value instanceof Error)
return describeError(value);
if (seen.has(value))
return '[circular]';
if (depth >= MAX_DEPTH)
return '[…]';
const toJSON = value.toJSON;
if (typeof toJSON === 'function') {
try {
return redact(toJSON.call(value), depth + 1, seen);
}
catch {
return '[unserializable]';
}
}
seen.add(value);
try {
if (Array.isArray(value))
return value.map((item) => redact(item, depth + 1, seen));
if (value instanceof Map) {
return redact(Object.fromEntries(value), depth, seen);
}
if (value instanceof Set)
return redact([...value], depth, seen);
const out = {};
for (const [key, inner] of Object.entries(value)) {
out[key] = SECRET_KEY.test(key) ? '[redacted]' : redact(inner, depth + 1, seen);
}
return out;
}
finally {
seen.delete(value);
}
}
/**
* A payload as the buffer keeps it: secret-looking keys (`password`, `token`, `authorization`,
* `cookie`, `apiKey`, …) redacted, and cut to {@link RECENT_PAYLOAD_BYTES} of JSON.
*/
export function boundPayload(payload) {
let safe;
let json;
try {
safe = redact(payload, 0, new WeakSet());
json = JSON.stringify(safe);
}
catch {
return '[unserializable]';
}
if (json === undefined)
return undefined;
const bytes = Buffer.byteLength(json);
if (bytes <= RECENT_PAYLOAD_BYTES)
return safe;
return {
truncated: true,
bytes,
preview: `${Buffer.from(json).subarray(0, RECENT_PAYLOAD_BYTES).toString('utf8')}…`,
};
}
/** `'run.done'` → exact match; `/run\./` (RegExp or its string form) → test. */
function toMatcher(event) {
if (event === undefined || event === '')
return undefined;
if (event instanceof RegExp) {
// A caller's /g or /y pattern keeps `lastIndex` between calls: compile a stateless copy.
const pattern = new RegExp(event.source, event.flags.replace(/[gy]/g, ''));
return (name) => pattern.test(name);
}
const literal = /^\/(.+)\/([a-z]*)$/.exec(event);
if (literal) {
let pattern;
try {
pattern = new RegExp(literal[1], (literal[2] ?? '').replace(/[gy]/g, ''));
}
catch {
throw new Error(`invalid event pattern ${event}`);
}
return (name) => pattern.test(name);
}
return (name) => name === event;
}
/**
* A fixed-size ring of the last events published in this process: every `aviary:*` channel (the
* `emit()` events and the `trace()` span phases) and every first-party `nestjs:*` channel.
*
* Subscribing to every channel turns off `emit`'s no-subscriber fast path, so the buffer only runs
* where it is wanted: `DiagnosticsModule` starts it outside production unless `recent: false`.
*/
export class RecentEventsBuffer {
size;
#ring;
#next = 0;
#count = 0;
#subscribed = [];
#names = new Set();
#offOfficial = [];
#offRegistered = null;
constructor(size = DEFAULT_RECENT_SIZE) {
if (!Number.isInteger(size) || size < 1) {
throw new Error(`/nestjs-diagnostics: recent.size must be a positive integer (got ${String(size)})`);
}
this.size = size;
this.#ring = new Array(size);
}
/** Whether the buffer is subscribed to the bus. */
get started() {
return this.#offRegistered !== null;
}
/** How many events are buffered. */
get count() {
return this.#count;
}
/**
* Subscribe to every current and future `aviary:*` channel with its `trace()` span sub-channels
* (`:start`, `:end`, `:asyncEnd`, `:error`), and to every first-party `nestjs:*` channel.
* Idempotent.
*/
start() {
if (this.started)
return;
const listen = (name) => {
const listener = (message) => this.push(message);
diagnostics_channel.channel(name).subscribe(listener);
this.#subscribed.push({ name, listener });
this.#names.add(name);
};
const subscribe = (name) => {
if (!name.startsWith(`${CHANNEL_PREFIX}:`))
return;
listen(name);
for (const phase of RECENT_SPAN_PHASES)
listen(`${name}:${phase}`);
};
for (const name of registeredChannels())
subscribe(name);
this.#offRegistered = onChannelRegistered(subscribe);
for (const lib of Object.keys(OFFICIAL_CHANNELS)) {
this.#offOfficial.push(subscribeOfficial(lib, 'all', (event) => this.push(event)));
}
for (const name of officialChannelNames())
this.#names.add(name);
}
/** Unsubscribe from the bus. The buffered events stay readable. */
stop() {
this.#offRegistered?.();
this.#offRegistered = null;
for (const { name, listener } of this.#subscribed) {
diagnostics_channel.channel(name).unsubscribe(listener);
}
this.#subscribed = [];
for (const off of this.#offOfficial)
off();
this.#offOfficial = [];
this.#names.clear();
}
/** Whether this buffer is one of the subscribers of channel `name`. */
isSubscribedTo(name) {
return this.#names.has(name);
}
/** Record one envelope (normally called by the bus subscription). Never throws. */
push(envelope) {
try {
if (envelope === null || typeof envelope !== 'object')
return;
if (typeof envelope.lib !== 'string' || typeof envelope.event !== 'string')
return;
const span = 'phase' in envelope && typeof envelope.phase === 'string' ? envelope : null;
const source = 'source' in envelope && envelope.source === 'nestjs' ? 'nestjs' : undefined;
const entry = {
...(typeof envelope.id === 'string' ? { id: envelope.id } : {}),
ts: typeof envelope.ts === 'number' ? envelope.ts : Date.now(),
lib: envelope.lib,
event: envelope.event,
...(typeof envelope.traceId === 'string' ? { traceId: envelope.traceId } : {}),
...(typeof envelope.durationMs === 'number' ? { durationMs: envelope.durationMs } : {}),
...('app' in envelope && typeof envelope.app === 'string' && envelope.app !== ''
? { app: envelope.app }
: {}),
...(source !== undefined ? { source } : {}),
payload: boundPayload(envelope.payload),
...(span !== null ? { phase: span.phase, spanId: span.spanId } : {}),
...(span !== null && span.result !== undefined
? { result: boundPayload(span.result) }
: {}),
...(span !== null && span.error !== undefined ? { error: describeError(span.error) } : {}),
};
this.#ring[this.#next] = entry;
this.#next = (this.#next + 1) % this.size;
this.#count = Math.min(this.#count + 1, this.size);
}
catch {
// Recording must never break an emitter.
}
}
/** The newest events matching `filter`, newest first. */
list(filter = {}) {
const limit = Math.max(1, Math.floor(filter.limit ?? 20));
let skip = Math.max(0, Math.floor(filter.offset ?? 0));
const event = toMatcher(filter.event);
const out = [];
for (let i = 1; i <= this.#count && out.length < limit; i++) {
const entry = this.#ring[(this.#next - i + this.size) % this.size];
if (entry === undefined)
continue;
if (filter.lib !== undefined && entry.lib !== filter.lib)
continue;
if (filter.traceId !== undefined && entry.traceId !== filter.traceId)
continue;
if (event !== undefined && !event(entry.event))
continue;
if (skip > 0) {
skip--;
continue;
}
out.push(entry);
}
return out;
}
/** Drop every buffered event. */
clear() {
this.#ring = new Array(this.size);
this.#next = 0;
this.#count = 0;
}
}
/**
* The process-wide buffer lives on `globalThis` (like the channel registry), so every copy of this
* package loaded in one process reads the same one.
*/
const RECENT_KEY = Symbol.for('@dudousxd/nestjs-diagnostics:recent');
function active() {
return globalThis[RECENT_KEY] ?? null;
}
/**
* Start the process-wide recent-events buffer and return it. `DiagnosticsModule` calls this; call
* it yourself only without the module. Starting again with the same size keeps the buffered
* events; another size replaces the buffer.
*/
export function startRecentEvents(size = DEFAULT_RECENT_SIZE) {
const current = active();
if (current !== null && current.size === size) {
current.start();
return current;
}
// Built first: an invalid size throws before the running buffer is touched.
const buffer = new RecentEventsBuffer(size);
current?.stop();
buffer.start();
globalThis[RECENT_KEY] = buffer;
return buffer;
}
/** Stop the process-wide buffer and forget it. */
export function stopRecentEvents() {
active()?.stop();
globalThis[RECENT_KEY] = null;
}
/** The process-wide buffer, or `null` when it is not running. */
export function recentEventsBuffer() {
return active();
}
/**
* The last diagnostics events of this process, newest first, when the recent-events buffer runs
* (outside production with `DiagnosticsModule`, or wherever `recent` turns it on). `[]` otherwise.
*
* ```ts
* recentEvents({ lib: 'durable', event: /run\.(failed|cancelled)/, limit: 50 });
* ```
*/
export function recentEvents(filter) {
return active()?.list(filter) ?? [];
}
//# sourceMappingURL=recent.js.map