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