Skip to content

Nest-js instrumentation breaks @nestjs/event-emitter when using multiple @OnEvent on the same method #15218

Open
@matthieuMay

Description

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nestjs

SDK Version

8.52.0

Framework Version

Nestjs 10.4.7

Link to Sentry event

No response

Reproduction Example/SDK Setup

I set up a minimal repro here :
https://github.com/matthieuMay/sentry_nest_event_repro

This is a minimal nestjs + nestjs/event-emitter + sentry configuration.

It contains:

  • instrument.ts : sentry initialization
  • app.module.ts: main nestjs module
  • app.controller.ts: main nestjs module
  • app.service.ts: service setting a event listener on two events and emitting the two events when application is bootstrapped

Steps to Reproduce

  1. npm install
  2. set environnement SENTRY_DSN variable
  3. run npm run start:dev
  4. see that event handler is called only once
Image
  1. comment the import of instrument.js or unset SENTRY_DSN variable
  2. run npm run start:dev
  3. see that event handler is called twice as expected
Image

Expected Result

We should see the handler being called twice

Explanation of the bug

Nestjs initialize in two steps : first the decorators set metadata on the decorated functions and once all decorators have been read, this metadata is traducted in method modifications.

Sentry instrumentation overrides the OnEvent decorator in SentryNestEventInstrumentation._createWrapOnEvent method. This new decorator ovverides descriptor.value on every call.
Therefore, when decorating the same handler with multiple @onevent, descriptor.value changes on every call changing the target of the metadata on every call. As a consequence, only the last called decorator is actually applied

Actual Result

Image

Metadata

Assignees

Labels

Package: nestjsIssues related to the Sentry Nestjs SDK

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions