|
5 | 5 | InstrumentationNodeModuleFile,
|
6 | 6 | isWrapped,
|
7 | 7 | } from '@opentelemetry/instrumentation';
|
8 |
| -import { captureException, SDK_VERSION, startSpan } from '@sentry/core'; |
| 8 | +import { captureException, handleCallbackErrors, SDK_VERSION, startSpan } from '@sentry/core'; |
9 | 9 | import { getEventSpanOptions } from './helpers';
|
10 | 10 | import type { OnEventTarget } from './types';
|
11 | 11 |
|
@@ -64,70 +64,68 @@ 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 | + |
67 | 69 | if (
|
68 |
| - !descriptor.value || |
69 |
| - typeof descriptor.value !== 'function' || |
| 70 | + !descriptorValueIsFunction(originalHandler) || |
70 | 71 | target.__SENTRY_INTERNAL__ ||
|
71 | 72 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
72 | 73 | descriptor.value.__SENTRY_INSTRUMENTED__
|
73 | 74 | ) {
|
74 | 75 | return decoratorResult(target, propertyKey, descriptor);
|
75 | 76 | }
|
76 | 77 |
|
77 |
| - const originalHandler = descriptor.value; |
78 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
79 |
| - const handlerName = originalHandler.name || propertyKey; |
80 | 78 | let eventName = typeof event === 'string' ? event : String(event);
|
81 | 79 |
|
82 | 80 | // 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 |
| - // 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')) { |
| 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 |
89 | 85 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
90 | 86 | // @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect
|
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('|'); |
| 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 | + } |
102 | 102 | }
|
103 |
| - } |
104 | 103 |
|
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 |
| - }; |
| 104 | + return startSpan(getEventSpanOptions(eventName), () => { |
| 105 | + return handleCallbackErrors( |
| 106 | + () => target.apply(thisArg, args), |
| 107 | + error => { |
| 108 | + captureException(error); |
| 109 | + }, |
| 110 | + ); |
| 111 | + }); |
| 112 | + }, |
| 113 | + }); |
117 | 114 |
|
118 | 115 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
119 | 116 | descriptor.value.__SENTRY_INSTRUMENTED__ = true;
|
120 | 117 |
|
121 |
| - // Preserve the original function name |
122 |
| - Object.defineProperty(descriptor.value, 'name', { |
123 |
| - value: handlerName, |
124 |
| - configurable: true, |
125 |
| - }); |
126 |
| - |
127 | 118 | // Apply the original decorator
|
128 | 119 | return decoratorResult(target, propertyKey, descriptor);
|
129 | 120 | };
|
130 | 121 | };
|
131 | 122 | };
|
132 | 123 | }
|
133 | 124 | }
|
| 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