@dudousxd/nestjs-diagnostics
Version:
A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.
106 lines • 4.89 kB
TypeScript
import { type OfficialDiagnosticEvent } from './official.js';
import type { DiagnosticEvent, SpanEvent, SpanPhase } from './types.js';
/** Default number of events the recent-events buffer keeps. */
export declare const DEFAULT_RECENT_SIZE = 500;
/** Bytes of serialized payload kept per buffered event. */
export declare const RECENT_PAYLOAD_BYTES = 2048;
/** One event in the recent-events buffer: the envelope, with its payload bounded and redacted. */
export interface RecentEvent {
id?: string;
ts: number;
lib: string;
event: string;
traceId?: string;
durationMs?: number;
/** The tenant app the event is about, when the emitter named one. */
app?: string;
/** `'nestjs'` for an event of a first-party `nestjs:*` channel. Absent on `aviary:*` events. */
source?: 'nestjs';
/** The payload with secret-looking keys replaced, or a `{ truncated, bytes, preview }` cut. */
payload: unknown;
/**
* For `trace()` spans: the phase (`start`, `end`, `asyncEnd`, `error`). Absent on `emit` events.
* An async span records `start`, then `end` when the function returns its promise (no `result`),
* then `asyncEnd` (or `error`) when it settles: the span is over at `asyncEnd`, not at `end`.
*/
phase?: SpanPhase;
/** For `trace()` spans: the id every phase of one span shares. */
spanId?: string;
/** For a span's `end`/`asyncEnd`: the result, bounded like `payload`. */
result?: unknown;
/** For a span's `error` (or failed `asyncEnd`): the error's name and message. */
error?: {
name?: string;
message: string;
};
}
/** Filters for {@link recentEvents}. */
export interface RecentEventsFilter {
lib?: string;
/** An exact event name, or a RegExp (the string form `/run\./` is accepted too). */
event?: string | RegExp;
traceId?: string;
/** How many of the newest matches to return (default 20). */
limit?: number;
/** Skip this many of the newest matches first (paging). */
offset?: number;
}
/** The span phases the buffer keeps (`asyncStart` carries nothing of its own). */
export declare const RECENT_SPAN_PHASES: readonly SpanPhase[];
/**
* 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 declare function boundPayload(payload: unknown): unknown;
/**
* 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 declare class RecentEventsBuffer {
#private;
readonly size: number;
constructor(size?: number);
/** Whether the buffer is subscribed to the bus. */
get started(): boolean;
/** How many events are buffered. */
get count(): number;
/**
* 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(): void;
/** Unsubscribe from the bus. The buffered events stay readable. */
stop(): void;
/** Whether this buffer is one of the subscribers of channel `name`. */
isSubscribedTo(name: string): boolean;
/** Record one envelope (normally called by the bus subscription). Never throws. */
push(envelope: DiagnosticEvent | SpanEvent | OfficialDiagnosticEvent): void;
/** The newest events matching `filter`, newest first. */
list(filter?: RecentEventsFilter): RecentEvent[];
/** Drop every buffered event. */
clear(): void;
}
/**
* 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 declare function startRecentEvents(size?: number): RecentEventsBuffer;
/** Stop the process-wide buffer and forget it. */
export declare function stopRecentEvents(): void;
/** The process-wide buffer, or `null` when it is not running. */
export declare function recentEventsBuffer(): RecentEventsBuffer | null;
/**
* 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 declare function recentEvents(filter?: RecentEventsFilter): RecentEvent[];
//# sourceMappingURL=recent.d.ts.map