Observer
function in meocord/decorator Since 4.1.0
Observer(
options?: ObserverOptions,
): (
target: new (...args: any[]) => DispatchObserver,
propertyKey?: string | symbol,
) => voidMarks a class as a dispatch observer, told about every call once it has settled, for metrics and audit logs.
Use it on a class that implements DispatchObserver, listed in @MeoCord({ observers }), to record how
calls end and how long they take. To change or stop a call, use an interceptor or a guard: an observer
cannot.
Where it runs
- Observers: onStart — before anything else runs
- Observers: onSettled — once the call has settled and been answered, however it ended
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
options? | ObserverOptions |
| |
options.types? | readonly ExecutionContextType[] | every type | The context types the observer is told about, as |
Returns
Returns a property decorator.
(
target: new (...args: any[]) => DispatchObserver,
propertyKey?: string | symbol,
) => voidThrows
Error when the class has no
onSettledmethod, ortypesis empty, as the decorator applies.
Examples
@Observer()
export class SlowCallObserver implements DispatchObserver {
private readonly logger = new Logger(SlowCallObserver.name)
onSettled(context: ExecutionContext, { outcome, durationMs }: DispatchResult) {
if (durationMs > 1000) this.logger.warn(`${context.getHandlerName()} ${outcome} in ${durationMs} ms`)
}
}