NestJS Event Emitter
Learn about instrumenting NestJS event-based services.
The @nestjs/event-emitter module is auto-instrumented in @sentry/nestjs
8.39.0 and up.
The NestJS SDK wraps the @OnEvent
decorator automatically to:
- Create performance traces for event handler executions.
- Automatically capture any unhandled exceptions that occur in event handlers.
- Maintain visibility into asynchronous event-driven flows.
When an event handler is executed, a new span is created to track its performance, and any errors are automatically reported to Sentry while preserving the original error behavior.
Multiple decorators
Annotating one function with multiple @OnEvent
decorators is not recommended, as NestJS provides no way for us to determine the triggering event. Therefore, the resulting span name will include all decorated event names.
Instead, use one decorator per event name and handle any shared logic through a separate function.
@OnEvent('event.A')
function myHandlerA(payload: any) {
commonHandler(payload)
}
@OnEvent('event.B')
function myHandlerB(payload: any) {
commonHandler(payload)
}
function commonHandler(payload: any) {
// handle stuff
}
This instrumentation works transparently with existing NestJS event handlers without requiring any code changes to your application.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").