@@ -58,31 +58,46 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
58
58
private _createWrapOnEvent ( ) {
59
59
// eslint-disable-next-line @typescript-eslint/no-explicit-any
60
60
return function wrapOnEvent ( original : any ) {
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- return function wrappedOnEvent ( event : any , options ?: any ) {
63
- const eventName = Array . isArray ( event )
64
- ? event . join ( ',' )
65
- : typeof event === 'string' || typeof event === 'symbol'
66
- ? event . toString ( )
67
- : '<unknown_event>' ;
68
-
61
+ return function wrappedOnEvent ( event : unknown , options ?: unknown ) {
69
62
// Get the original decorator result
70
63
const decoratorResult = original ( event , options ) ;
71
64
72
65
// Return a new decorator function that wraps the handler
73
- return function ( target : OnEventTarget , propertyKey : string | symbol , descriptor : PropertyDescriptor ) {
74
- if ( ! descriptor . value || typeof descriptor . value !== 'function' || target . __SENTRY_INTERNAL__ ) {
66
+ return ( target : OnEventTarget , propertyKey : string | symbol , descriptor : PropertyDescriptor ) => {
67
+ if (
68
+ ! descriptor . value ||
69
+ typeof descriptor . value !== 'function' ||
70
+ target . __SENTRY_INTERNAL__ ||
71
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
72
+ descriptor . value . __SENTRY_INSTRUMENTED__
73
+ ) {
75
74
return decoratorResult ( target , propertyKey , descriptor ) ;
76
75
}
77
76
78
- // Get the original handler
79
77
const originalHandler = descriptor . value ;
80
78
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
81
79
const handlerName = originalHandler . name || propertyKey ;
80
+ let eventName = typeof event === 'string' ? event : String ( event ) ;
81
+
82
+ // Instrument the actual handler
83
+ descriptor . value = async function ( ...args : unknown [ ] ) {
84
+ // When multiple @OnEvent decorators are used on a single method, we need to get all event names
85
+ // from the reflector metadata as there is no information during execution which event triggered it
86
+ if ( Reflect . getMetadataKeys ( descriptor . value ) . includes ( 'EVENT_LISTENER_METADATA' ) ) {
87
+ const eventData = Reflect . getMetadata ( 'EVENT_LISTENER_METADATA' , descriptor . value ) ;
88
+ if ( Array . isArray ( eventData ) ) {
89
+ eventName = eventData
90
+ . map ( ( data : unknown ) => {
91
+ if ( data && typeof data === 'object' && 'event' in data && data . event ) {
92
+ return data . event ;
93
+ }
94
+ return '' ;
95
+ } )
96
+ . reverse ( ) // decorators are evaluated bottom to top
97
+ . join ( '|' ) ;
98
+ }
99
+ }
82
100
83
- // Instrument the handler
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
- descriptor . value = async function ( ...args : any [ ] ) {
86
101
return startSpan ( getEventSpanOptions ( eventName ) , async ( ) => {
87
102
try {
88
103
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
@@ -96,6 +111,9 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
96
111
} ) ;
97
112
} ;
98
113
114
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
115
+ descriptor . value . __SENTRY_INSTRUMENTED__ = true ;
116
+
99
117
// Preserve the original function name
100
118
Object . defineProperty ( descriptor . value , 'name' , {
101
119
value : handlerName ,
0 commit comments