|
| 1 | +import { isWrapped } from '@opentelemetry/core'; |
| 2 | +import type { InstrumentationConfig } from '@opentelemetry/instrumentation'; |
| 3 | +import { |
| 4 | + InstrumentationBase, |
| 5 | + InstrumentationNodeModuleDefinition, |
| 6 | + InstrumentationNodeModuleFile, |
| 7 | +} from '@opentelemetry/instrumentation'; |
| 8 | +import { captureException, startSpan } from '@sentry/core'; |
| 9 | +import { SDK_VERSION } from '@sentry/utils'; |
| 10 | +import { getEventSpanOptions } from './helpers'; |
| 11 | +import type { OnEventTarget } from './types'; |
| 12 | + |
| 13 | +const supportedVersions = ['>=2.0.0']; |
| 14 | + |
| 15 | +/** |
| 16 | + * Custom instrumentation for nestjs event-emitter |
| 17 | + * |
| 18 | + * This hooks into the `OnEvent` decorator, which is applied on event handlers. |
| 19 | + */ |
| 20 | +export class SentryNestEventInstrumentation extends InstrumentationBase { |
| 21 | + public static readonly COMPONENT = '@nestjs/event-emitter'; |
| 22 | + public static readonly COMMON_ATTRIBUTES = { |
| 23 | + component: SentryNestEventInstrumentation.COMPONENT, |
| 24 | + }; |
| 25 | + |
| 26 | + public constructor(config: InstrumentationConfig = {}) { |
| 27 | + super('sentry-nestjs-event', SDK_VERSION, config); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Initializes the instrumentation by defining the modules to be patched. |
| 32 | + */ |
| 33 | + public init(): InstrumentationNodeModuleDefinition { |
| 34 | + const moduleDef = new InstrumentationNodeModuleDefinition( |
| 35 | + SentryNestEventInstrumentation.COMPONENT, |
| 36 | + supportedVersions, |
| 37 | + ); |
| 38 | + |
| 39 | + moduleDef.files.push(this._getOnEventFileInstrumentation(supportedVersions)); |
| 40 | + return moduleDef; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Wraps the @OnEvent decorator. |
| 45 | + */ |
| 46 | + private _getOnEventFileInstrumentation(versions: string[]): InstrumentationNodeModuleFile { |
| 47 | + return new InstrumentationNodeModuleFile( |
| 48 | + '@nestjs/event-emitter/dist/decorators/on-event.decorator.js', |
| 49 | + versions, |
| 50 | + (moduleExports: { OnEvent: OnEventTarget }) => { |
| 51 | + if (isWrapped(moduleExports.OnEvent)) { |
| 52 | + this._unwrap(moduleExports, 'OnEvent'); |
| 53 | + } |
| 54 | + this._wrap(moduleExports, 'OnEvent', this._createWrapOnEvent()); |
| 55 | + return moduleExports; |
| 56 | + }, |
| 57 | + (moduleExports: { OnEvent: OnEventTarget }) => { |
| 58 | + this._unwrap(moduleExports, 'OnEvent'); |
| 59 | + }, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a wrapper function for the @OnEvent decorator. |
| 65 | + */ |
| 66 | + private _createWrapOnEvent() { |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 68 | + return function wrapOnEvent(original: any) { |
| 69 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 70 | + return function wrappedOnEvent(event: any, options?: any) { |
| 71 | + const eventName = Array.isArray(event) |
| 72 | + ? event.join(',') |
| 73 | + : typeof event === 'string' || typeof event === 'symbol' |
| 74 | + ? event.toString() |
| 75 | + : '<unknown_event>'; |
| 76 | + |
| 77 | + // Get the original decorator result |
| 78 | + const decoratorResult = original(event, options); |
| 79 | + |
| 80 | + // Return a new decorator function that wraps the handler |
| 81 | + return function (target: OnEventTarget, propertyKey: string | symbol, descriptor: PropertyDescriptor) { |
| 82 | + if (!descriptor.value || typeof descriptor.value !== 'function' || target.__SENTRY_INTERNAL__) { |
| 83 | + return decoratorResult(target, propertyKey, descriptor); |
| 84 | + } |
| 85 | + |
| 86 | + // Get the original handler |
| 87 | + const originalHandler = descriptor.value; |
| 88 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 89 | + const handlerName = originalHandler.name || propertyKey; |
| 90 | + |
| 91 | + // Instrument the handler |
| 92 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 93 | + descriptor.value = async function (...args: any[]) { |
| 94 | + return startSpan(getEventSpanOptions(eventName), async () => { |
| 95 | + try { |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 97 | + const result = await originalHandler.apply(this, args); |
| 98 | + return result; |
| 99 | + } catch (error) { |
| 100 | + // exceptions from event handlers are not caught by global error filter |
| 101 | + captureException(error); |
| 102 | + throw error; |
| 103 | + } |
| 104 | + }); |
| 105 | + }; |
| 106 | + |
| 107 | + // Preserve the original function name |
| 108 | + Object.defineProperty(descriptor.value, 'name', { |
| 109 | + value: handlerName, |
| 110 | + configurable: true, |
| 111 | + }); |
| 112 | + |
| 113 | + // Apply the original decorator |
| 114 | + return decoratorResult(target, propertyKey, descriptor); |
| 115 | + }; |
| 116 | + }; |
| 117 | + }; |
| 118 | + } |
| 119 | +} |
0 commit comments