Skip to main content

Message Watcher

The Message Watcher records every message a microservice handler receives — @MessagePattern and @EventPattern, on any transport @nestjs/microservices offers — with the data it was given, what it answered, how long it took and, when it failed, why. It needs nothing from your code but the switch:

NestLensModule.forRoot({
watchers: {
message: true,
},
})

What it records​

One message entry per message a handler took, whichever way the microservice runs:

ArrangementRecorded
Hybrid — app.connectMicroservice() beside an HTTP applicationYes, with or without inheritAppConfig: true
Standalone — NestFactory.createMicroservice()Yes
Any transport: TCP, Redis, NATS, MQTT, RabbitMQ, Kafka, gRPC, or a custom strategyYes
  • The pattern the handler matched, as text (an object pattern is written as JSON: {"cmd":"sum"}), the transport, and whether it was a request (@MessagePattern, which answers) or an event (@EventPattern, which does not).

  • The data the handler was given and what it answered — the last value, for a handler that answers with a stream. Both are masked like a request body and bounded by maxPayloadSize.

  • Status and duration: completed or failed, and the thrown value's message when it failed. The exception itself is recorded by the Exception watcher, linked to the message.

  • What the transport says about it, where it says it:

    Transportcontext
    Kafkatopic, partition, offset, key, headers
    NATSsubject, headers
    RabbitMQexchange, routingKey, redelivered, the message's headers
    MQTTtopic, qos, retain, MQTT 5 user properties as headers
    Redischannel
    gRPCservice and method, and the call's metadata as headers
    TCPthe peer's remoteAddress, and from NestJS 12 the packet's metadata as headers

    Headers are masked with the sensitive headers list — authorization arrives as ***REDACTED*** — and at most 100 are kept.

Each message runs in a request context of its own, so what its handler does is attributed to it: a query, a log line, an outgoing HTTP call, a cache read made while handling the message carry its requestId, and the entry's detail page lists them as a request's does.

The watcher reports waiting when it is on and no controller has a @MessagePattern or @EventPattern handler: there is nothing for it to record. Otherwise it is recording, with the number of handlers it is bound to.

How handlers are reached​

Nest gives a hybrid application's microservice the global interceptors and filters only when it was connected with inheritAppConfig: true, which is off by default — so a global interceptor, the way the request watcher is installed, would miss most hybrid applications. The message watcher is bound to each handler instead, as @UseInterceptors() on the method would bind it, while the application's providers are being created: before connectMicroservice() builds the handlers, and before any lifecycle hook. Your own interceptors on a handler run around it, as they would around one of yours; an application that closes takes it off again.

Two things are outside its reach:

  • What a guard throws. Guards run before any interceptor. An exception a guard throws is recorded by the exception watcher's global filter, which Nest applies to a standalone microservice and to a hybrid one connected with inheritAppConfig: true — not to a hybrid one without it.
  • Outgoing messages. ClientProxy.send() and emit() are not recorded: Nest offers no hook for them, and wrapping every client a module registers would change the observables an application subscribes to. A message one NestLens application sends is recorded by the one that receives it.

A handler in a module loaded with LazyModuleLoader did not exist when the watcher was bound, and is not recorded. The binding is on the handler's class, so two applications open at once in one process — a test suite that boots a second before closing the first — share it: the one started later records its messages in both.

Configuration​

NestLensModule.forRoot({
watchers: {
message: {
captureData: true,
captureResponse: true,
maxPayloadSize: 64 * 1024,
ignorePatterns: ['health.check', '{"cmd":"ping"}'],
},
},
})
OptionTypeDefaultDescription
enabledbooleanfalseRecord microservice messages
captureDatabooleantrueRecord the data a handler was given
captureResponsebooleantrueRecord what a @MessagePattern handler answered
maxPayloadSizenumber65536Bytes of data and of response kept, each; a larger value is recorded as its size. 0 keeps none
ignorePatternsstring[][]Patterns not to record, written as the entry writes them

When an option is off​

captureData: false and captureResponse: false leave data and response out, and the entry lists each under payload.notCaptured with the option, such as { field: 'response', option: 'watchers.message.captureResponse' }. An @EventPattern handler answers nothing and a failed handler answered with its error, so neither lists a response. The dashboard's Data and Response tabs say Not captured — captureResponse is off for the Message watcher. captureData: false keeps the data out of the exception a failed handler records as well.

Payload Structure​

interface MessageEntry {
type: 'message';
payload: {
transport: string; // 'tcp', 'kafka', 'nats', 'grpc', ...
pattern: string; // 'sum', or '{"cmd":"sum"}'
kind: 'request' | 'event'; // @MessagePattern or @EventPattern
handler?: string; // 'MathController.sum'
data?: unknown;
response?: unknown;
status: 'completed' | 'failed';
error?: string;
duration: number; // ms
context?: {
topic?: string;
partition?: number;
offset?: string;
key?: string;
subject?: string;
channel?: string;
exchange?: string;
routingKey?: string;
redelivered?: boolean;
qos?: number;
retain?: boolean;
service?: string;
method?: string;
headers?: Record<string, string>;
remoteAddress?: string;
};
notCaptured?: { field: string; option: string }[]; // What a capture option kept out
};
}

Dashboard View​

The Messages page lists each message with its pattern, transport, kind, status and duration, and filters by status, transport and pattern (a pattern filter matches text inside the pattern, ignoring case). The detail page shows the data, the response, the transport's context and everything the handler did while answering.