@dudousxd/nestjs-diagnostics
Version:
A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.
60 lines • 3.04 kB
TypeScript
/** The minimal Redis pub/sub surface the relay uses. An ioredis instance satisfies it structurally. */
export interface RedisLike {
publish(channel: string, message: string): unknown;
/** When present, a tick's forwarded events go out as one pipeline instead of one round trip each. */
pipeline?(): RedisPipelineLike;
subscribe(channel: string, callback?: (err: Error | null, count: number) => void): unknown;
on(event: 'message', listener: (channel: string, message: string) => void): unknown;
removeListener(event: 'message', listener: (channel: string, message: string) => void): unknown;
unsubscribe(channel: string): unknown;
}
/** The slice of an ioredis pipeline the relay uses to batch publishes. */
export interface RedisPipelineLike {
publish(channel: string, message: string): unknown;
exec(): unknown;
}
export interface ChannelRef {
lib: string;
event: string;
}
export interface DiagnosticsRedisRelayOptions {
/** Publisher connection. */
pub: RedisLike;
/**
* Subscriber connection (separate from `pub`). For ioredis: `const sub = pub.duplicate()`.
* Required unless `receive` is `false`.
*/
sub?: RedisLike;
/**
* Re-emit events other processes forward onto this process's bus. Default `true`; `false` makes
* a publish-only relay that never subscribes (a `role: 'api'` process).
*/
receive?: boolean;
/** Forward every event of these libs (current + future channels). */
libs?: string[];
/** Forward these exact channels, in addition to `libs`. */
channels?: ChannelRef[];
/** Forward EVERY aviary channel (current + future). Overrides `libs`/`channels`. Default false. */
all?: boolean;
/** Redis channel to relay on. Default 'aviary:diagnostics:relay'. */
redisChannel?: string;
/** Unique id for THIS process, for echo suppression. Default a random id. */
nodeId?: string;
}
/**
* Relay diagnostics events across processes over Redis pub/sub. The envelope crosses as JSON with
* its `id` intact, and the re-emitted copy is marked as relayed (see {@link publishRelayed}). Forwards selected local
* `aviary:<lib>:<event>` channels to Redis and re-emits Redis-received events onto the local bus, so
* `@OnDiagnostic` handlers / `getChannel(...).subscribe(...)` fire cross-process. Loop-safe via nodeId
* echo suppression and a re-emit guard. Never throws into `emit()` or the Redis handler. Does NOT
* close the `pub`/`sub` connections — the caller owns them.
*
* Forwarding happens off the emitter's call stack: selected events are queued and, once per event
* loop turn, serialized and published in emit order (one Redis message per event, sent as a single
* pipeline when `pub` supports it).
*
* @returns a teardown that publishes any queued events, then removes all local subscriptions and
* the Redis message handler.
*/
export declare function createDiagnosticsRedisRelay(options: DiagnosticsRedisRelayOptions): () => void;
//# sourceMappingURL=redis-relay.d.ts.map