|
5 | 5 | InstrumentationNodeModuleFile,
|
6 | 6 | isWrapped,
|
7 | 7 | } from '@opentelemetry/instrumentation';
|
8 | | -import { captureException, handleCallbackErrors,SDK_VERSION, startSpan } from '@sentry/core'; |
| 8 | +import { captureException, SDK_VERSION, startSpan } from '@sentry/core'; |
9 | 9 | import { getEventSpanOptions } from './helpers';
|
10 | 10 | import type { OnEventTarget } from './types';
|
11 | 11 |
|
@@ -64,68 +64,70 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
|
64 | 64 |
|
65 | 65 | // Return a new decorator function that wraps the handler
|
66 | 66 | return (target: OnEventTarget, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
67 | | - const originalHandler = descriptor.value; |
68 | | - |
69 | 67 | if (
|
70 | | - !descriptorValueIsFunction(originalHandler) || |
| 68 | + !descriptor.value || |
| 69 | + typeof descriptor.value !== 'function' || |
71 | 70 | target.__SENTRY_INTERNAL__ ||
|
72 | 71 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
73 | 72 | descriptor.value.__SENTRY_INSTRUMENTED__
|
74 | 73 | ) {
|
75 | 74 | return decoratorResult(target, propertyKey, descriptor);
|
76 | 75 | }
|
77 | 76 |
|
| 77 | + const originalHandler = descriptor.value; |
| 78 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 79 | + const handlerName = originalHandler.name || propertyKey; |
78 | 80 | let eventName = typeof event === 'string' ? event : String(event);
|
79 | 81 |
|
80 | 82 | // Instrument the actual handler
|
81 | | - descriptor.value = new Proxy(originalHandler, { |
82 | | - apply: async function (target, thisArg, args) { |
83 | | - // When multiple @OnEvent decorators are used on a single method, we need to get all event names |
84 | | - // from the reflector metadata as there is no information during execution which event triggered it |
| 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 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 87 | + // @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect |
| 88 | + if (Reflect.getMetadataKeys(descriptor.value).includes('EVENT_LISTENER_METADATA')) { |
85 | 89 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
86 | 90 | // @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect
|
87 | | - if (Reflect.getMetadataKeys(descriptor.value).includes('EVENT_LISTENER_METADATA')) { |
88 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
89 | | - // @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect |
90 | | - const eventData = Reflect.getMetadata('EVENT_LISTENER_METADATA', descriptor.value); |
91 | | - if (Array.isArray(eventData)) { |
92 | | - eventName = eventData |
93 | | - .map((data: unknown) => { |
94 | | - if (data && typeof data === 'object' && 'event' in data && data.event) { |
95 | | - return data.event; |
96 | | - } |
97 | | - return ''; |
98 | | - }) |
99 | | - .reverse() // decorators are evaluated bottom to top |
100 | | - .join('|'); |
101 | | - } |
| 91 | + const eventData = Reflect.getMetadata('EVENT_LISTENER_METADATA', descriptor.value); |
| 92 | + if (Array.isArray(eventData)) { |
| 93 | + eventName = eventData |
| 94 | + .map((data: unknown) => { |
| 95 | + if (data && typeof data === 'object' && 'event' in data && data.event) { |
| 96 | + return data.event; |
| 97 | + } |
| 98 | + return ''; |
| 99 | + }) |
| 100 | + .reverse() // decorators are evaluated bottom to top |
| 101 | + .join('|'); |
102 | 102 | }
|
| 103 | + } |
103 | 104 |
|
104 | | - return startSpan(getEventSpanOptions(eventName), () => { |
105 | | - return handleCallbackErrors( |
106 | | - () => target.apply(thisArg, args), |
107 | | - error => { |
108 | | - captureException(error); |
109 | | - }, |
110 | | - ); |
111 | | - }); |
112 | | - }, |
113 | | - }); |
| 105 | + return startSpan(getEventSpanOptions(eventName), async () => { |
| 106 | + try { |
| 107 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 108 | + const result = await originalHandler.apply(this, args); |
| 109 | + return result; |
| 110 | + } catch (error) { |
| 111 | + // exceptions from event handlers are not caught by global error filter |
| 112 | + captureException(error); |
| 113 | + throw error; |
| 114 | + } |
| 115 | + }); |
| 116 | + }; |
114 | 117 |
|
115 | 118 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
116 | 119 | descriptor.value.__SENTRY_INSTRUMENTED__ = true;
|
117 | 120 |
|
| 121 | + // Preserve the original function name |
| 122 | + Object.defineProperty(descriptor.value, 'name', { |
| 123 | + value: handlerName, |
| 124 | + configurable: true, |
| 125 | + }); |
| 126 | + |
118 | 127 | // Apply the original decorator
|
119 | 128 | return decoratorResult(target, propertyKey, descriptor);
|
120 | 129 | };
|
121 | 130 | };
|
122 | 131 | };
|
123 | 132 | }
|
124 | 133 | }
|
125 | | - |
126 | | -function descriptorValueIsFunction( |
127 | | - value: unknown, |
128 | | - // eslint-disable-next-line @typescript-eslint/ban-types |
129 | | -): value is Function & { __SENTRY_INSTRUMENTED__?: boolean; name?: string } { |
130 | | - return !!value && typeof value === 'function'; |
131 | | -} |
|
0 commit comments