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
If multiple decorators are used, we will collect all event names to determine the span's name. In case you want to map each event to a specific handler, use one decorator per handler and handle any shared logic through a separate function.
Copied
@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.
Was this helpful?
Help improve this content
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").
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").