-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdecorators.ts
87 lines (76 loc) · 2.83 KB
/
decorators.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
import { captureException } from '@sentry/core';
import * as Sentry from '@sentry/node';
import { startSpan } from '@sentry/node';
import type { MonitorConfig } from '@sentry/types';
import { isExpectedError } from './helpers';
/**
* A decorator wrapping the native nest Cron decorator, sending check-ins to Sentry.
*/
export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig): MethodDecorator => {
return (target: unknown, propertyKey, descriptor: PropertyDescriptor) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = function (...args: any[]) {
return Sentry.withMonitor(
monitorSlug,
() => {
return originalMethod.apply(this, args);
},
monitorConfig,
);
};
return descriptor;
};
};
/**
* A decorator usable to wrap arbitrary functions with spans.
*/
export function SentryTraced(op: string = 'function') {
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalMethod = descriptor.value as (...args: any[]) => Promise<any> | any; // function can be sync or async
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = function (...args: any[]) {
return startSpan(
{
op: op,
name: propertyKey,
},
() => {
return originalMethod.apply(this, args);
},
);
};
// preserve the original name on the decorated function
Object.defineProperty(descriptor.value, 'name', {
value: originalMethod.name,
configurable: true,
enumerable: true,
writable: true,
});
return descriptor;
};
}
/**
* A decorator to wrap user-defined exception filters and add Sentry error reporting.
*/
export function SentryExceptionCaptured() {
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalCatch = descriptor.value as (exception: unknown, host: unknown, ...args: any[]) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = function (exception: unknown, host: unknown, ...args: any[]) {
if (isExpectedError(exception)) {
return originalCatch.apply(this, [exception, host, ...args]);
}
captureException(exception);
return originalCatch.apply(this, [exception, host, ...args]);
};
return descriptor;
};
}
/**
* A decorator to wrap user-defined exception filters and add Sentry error reporting.
*/
export const WithSentry = SentryExceptionCaptured;