UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

61 lines (60 loc) 2.75 kB
/** * @file src/brokers/InstrumentedBrokerClient.ts * @description The core instrumentation class. It wraps any `IBrokerAdapter` * implementation and adds logging and automatic context propagation for * distributed tracing. */ import { ILogger } from '../logger'; import { IContextManager } from '../context'; import { IBrokerAdapter, BrokerMessage, MessageHandler } from './adapter.types'; import { BrokerInstanceConfig } from '../config'; /** * @class InstrumentedBrokerClient * @description Wraps a user-provided broker adapter to automatically handle * logging, context propagation, and distributed tracing. */ export declare class InstrumentedBrokerClient { private readonly adapter; private readonly logger; private readonly contextManager; private readonly config; readonly instanceName: string; /** * @constructor * @param {IBrokerAdapter} adapter - The concrete broker adapter implementation (e.g., for RabbitMQ, Kafka). * @param {ILogger} logger - The logger instance for this client. * @param {IContextManager} contextManager - The manager for handling asynchronous contexts. * @param {BrokerInstanceConfig} config - The configuration for this specific instance. */ constructor(adapter: IBrokerAdapter, logger: ILogger, contextManager: IContextManager, config: BrokerInstanceConfig); /** * Establishes a connection to the broker, wrapping the adapter's connect * method with logging. * @returns {Promise<void>} */ connect(): Promise<void>; /** * Disconnects from the broker, wrapping the adapter's disconnect method * with logging. * @returns {Promise<void>} */ disconnect(): Promise<void>; /** * Publishes a message, automatically injecting the current `correlation-id` * from the active context into the message headers. * @param {string} topic - The destination topic or routing key for the message. * @param {BrokerMessage} message - The message to be published. The `correlation-id` * will be added to its headers if not present. * @returns {Promise<void>} */ publish(topic: string, message: BrokerMessage): Promise<void>; /** * Subscribes to a topic. It wraps the user's message handler to automatically * create a new asynchronous context for each incoming message. If a `correlation-id` * is found in the message headers, it is used to initialize the new context. * @param {string} topic - The topic or queue to subscribe to. * @param {MessageHandler} handler - The user-provided function to process incoming messages. * @returns {Promise<void>} */ subscribe(topic: string, handler: MessageHandler): Promise<void>; }