-
Notifications
You must be signed in to change notification settings - Fork 2
/
decorator.ts
226 lines (217 loc) · 6.54 KB
/
decorator.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {Config} from '../config';
import {GeneratedDecoratorWithArgs, GenericMethod, MetricWrapper} from '../decorator';
import {Tags} from '../tags';
import {StatsDOptions} from './options';
import {getStatsdClient} from './utils';
import {StatsDClientAlike} from './options';
export type StatsDMetricNumericArgs = [string, number, Tags?];
export type StatsDMetricDateArgs = [string, Tags?];
export type StatsDMetricArgs = StatsDMetricNumericArgs | StatsDMetricDateArgs;
// jscpd:ignore-start
/**
* Generate a Metric Decorator. Can use wrappers like {@link metricIncrementWrapper},
* {@link metricGaugeDecrementWrapper}, {@link metricGaugeIncrementWrapper}, {@link metricGaugeSetWrapper},
* {@link metricTimingWrapper}, {@link metricObserveWrapper}, or custom defined metric wrappers.
*
* ```typescript
* import {Controller} from "@netsjs/common";
* import {MetricWrapper, GenericMethod} from "@mists/nestjs-metrics";
* import {StatsDMetricNumericArgs, generateStatsDDecorator} from "@mists/nestjs-metrics/dist/statsd";
*
* export const customMetricWrapper: MetricWrapper = (
* metricArgs: StatsDMetricNumericArgs,
* metric: any,
* oldMethod: GenericMethod,
* target: any,
* propertyKey: string | symbol,
* descriptor: PropertyDescriptor,
* ): GenericMethod => (...args: any[]): any => {
* (metric as StatsDClientAlike).increment(...metricArgs);
* return oldMethod.call(target, ...args);
* };
*
*
* const IncrementHttpCalls = generateStatsDDecorator(metricIncrementWrapper);
*
* const CustomIncrementHttpCalls = generateStatsDDecorator(customMetricWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @IncrementHttpCalls()
* @CustomIncrementHttpCalls(1, { serverId: 'server_1' })
* testMethod() {}
* }
* ```
*/
export const generateStatsDDecorator = (
wrapper: MetricWrapper,
options?: StatsDOptions,
): GeneratedDecoratorWithArgs => {
return (...args: StatsDMetricArgs): MethodDecorator => {
const [name] = args;
const metric = getStatsdClient(name, options || Config.getInstance().statsd || 'dummy');
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor): PropertyDescriptor => {
descriptor.value = wrapper(args, metric, descriptor.value, target, propertyKey, descriptor);
return descriptor;
};
};
};
/**
* Increment Wrapper
*
* ```typescript
* import { Controller, Get } from "@nestjs/common";
* import { generateStatsDDecorator, statsdIncrementWrapper } from "@mists/nestjs-metrics/dist/statsd";
*
* const Increment = generateStatsDDecorator(statsdIncrementWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @Increment()
* controllerAction() {}
* }
* ```
*/
export const statsdIncrementWrapper: MetricWrapper = (
metricArgs: StatsDMetricNumericArgs,
metric: any,
oldMethod: GenericMethod,
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): GenericMethod => (...args: any[]): any => {
(metric as StatsDClientAlike).increment(...metricArgs);
return oldMethod.call(target, ...args);
};
/**
* StatsD GaugeDelta Wrapper
*
* ```typescript
* import { Controller, Get } from "@nestjs/common";
* import { generateStatsDDecorator, statsdGaugeDeltaWrapper } from "@mists/nestjs-metrics/dist/statsd";
*
* const GaugeDelta = generateStatsDDecorator(statsdGaugeDeltaWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @GaugeDelta(-1)
* controllerAction() {}
* }
* ```
*/
export const statsdGaugeDeltaWrapper: MetricWrapper = (
metricArgs: StatsDMetricNumericArgs,
metric: any,
oldMethod: GenericMethod,
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): GenericMethod => (...args: any[]): any => {
(metric as StatsDClientAlike).gaugeDelta(...metricArgs);
return oldMethod.call(target, ...args);
};
/**
* StatsD Gauge Wrapper
*
* ```typescript
* import { Controller, Get } from "@nestjs/common";
* import { generateStatsDDecorator, statsdGaugeWrapper } from "@mists/nestjs-metrics/dist/statsd";
*
* const GaugeSet = generateStatsDDecorator(statsdGaugeWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @GaugeSet(1)
* controllerAction() {}
* }
* ```
*/
export const statsdGaugeWrapper = (
metricArgs: StatsDMetricNumericArgs,
metric: any,
oldMethod: GenericMethod,
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): GenericMethod => (...args: any[]): any => {
(metric as StatsDClientAlike).gauge(...metricArgs);
return oldMethod.call(target, ...args);
};
/**
* StatsD Histogram Wrapper
*
* ```typescript
* import { Controller, Get } from "@nestjs/common";
* import { generateStatsDDecorator, statsdHistogramWrapper } from "@mists/nestjs-metrics/dist/statsd";
*
* const Histogram = generateStatsDDecorator(statsdHistogramWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @Histogram(1)
* controllerAction() {}
* }
* ```
*/
export const statsdHistogramWrapper = (
metricArgs: StatsDMetricNumericArgs,
metric: any,
oldMethod: GenericMethod,
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): GenericMethod => (...args: any[]): any => {
(metric as StatsDClientAlike).histogram(...metricArgs);
return oldMethod.call(target, ...args);
};
/**
* StatsD Timing Wrapper
*
* ```typescript
* import { Controller, Get } from "@nestjs/common";
* import { generateStatsDDecorator, statsdTimingWrapper } from "@mists/nestjs-metrics/dist/statsd";
*
* const Timing = generateStatsDDecorator(statsdTimingWrapper);
*
* @Controller('/test')
* class CustomController {
* @Get()
* @Timing()
* controllerAction() {}
* }
* ```
*/
export const statsdTimingWrapper = (
metricArgs: StatsDMetricDateArgs,
metric: any,
oldMethod: GenericMethod,
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): GenericMethod => (...args: any[]): any => {
const [name, tags] = metricArgs;
const date = new Date();
const result = oldMethod.call(target, ...args);
if (result instanceof Promise) {
return result
.then((...args: any[]) => {
(metric as StatsDClientAlike).timing(name, date, tags);
return args;
})
.catch((error) => {
(metric as StatsDClientAlike).timing(name, date, tags);
throw error;
});
} else {
(metric as StatsDClientAlike).timing(name, date, tags);
return result;
}
};
// jscpd:ignore-end