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 | 1
1
1
1
1
1
1
1
4
4
4
4
4
8
4
4890983
8
8
8
8
4890983
4
1
3
3
3
3
3
3
3
4
3
3
3
3
3
1
2
3
3
1
4
4
4
4
4
4
4
5
2
2
2
2
2
2
1
5
5
4
2
1
4
4
| import {accept, Filter} from '../decorators';
import {Lookup} from '../decorators/lookup';
import {internal} from '../internal';
import {Providers} from '../providers';
import {Numeric} from '../types';
import {joinFqn, Tags} from '../util';
import {Metric} from './local/metric';
const { logger, MSG_FMT } = internal;
export type Counter<T extends Numeric> = Metric & {
increment: (val: T, tags?: Tags) => void;
}
export abstract class CounterFactory<T extends Numeric> {
abstract counter(name: string, tags?: Tags): Counter<T>;
}
export type CountOptions<T extends Numeric> = {
name?: string;
filter?: Filter;
counterFactory?: CounterFactory<T>;
tags?: Tags;
};
export function CountInvocations(options: CountOptions<number> = {}): MethodDecorator {
return function countInvocationsDecorator(
target: Record<string, any>,
prop: PropertyKey,
desc: PropertyDescriptor,
): PropertyDescriptor {
const original = desc.value;
const fqn = options.name || joinFqn(prop, target.constructor.name);
const log = logger('@CountInvocations');
log.debug(MSG_FMT.annotatingMethod, target.constructor.name, prop, 'CountInvocations', options);
const counters = new Lookup<CounterFactory<number>, Counter<number>>(() => Providers.provide(CounterFactory));
desc.value = function countInvocation(...args: unknown[]): unknown {
if (accept(options)) {
const metric = counters.getInstance(
this,
fqn,
() => counters.getFactory(this, fqn, options.counterFactory)?.counter(fqn, options.tags),
);
Iif (!metric) {
log.warn(
'Unable to decorate %s.%s with @CountInvocations! No CounterFactory available!',
target.constructor.name,
prop,
);
}
metric?.increment(1);
}
return original.apply(this, ...args);
};
return desc;
}
}
export function CountSuccess(options: CountOptions<number> = {}): MethodDecorator {
return function countSuccessDecorator(
target: Record<string, any>,
prop: PropertyKey,
desc: PropertyDescriptor,
): PropertyDescriptor {
const original = desc.value;
const fqn = options?.name || joinFqn(prop, target.constructor.name);
const log = logger('@CountSuccess');
log.debug(MSG_FMT.annotatingMethod, target.constructor.name, prop, 'CountSuccess', options);
const counters = new Lookup<CounterFactory<number>, Counter<number>>(() => Providers.provide(CounterFactory));
desc.value = function countSuccess(...args: unknown[]): unknown {
const result = original.apply(this, ...args);
Eif (accept(options)) {
const metric = counters.getInstance(
this,
fqn,
() => counters.getFactory(this, fqn, options?.counterFactory)?.counter(fqn, options?.tags),
);
Iif (!metric) {
log.warn(
'Unable to decorate %s.%s with @CountSuccess! No CounterFactory available!',
target.constructor.name,
prop,
);
}
if (result instanceof Promise) {
result.then(() => metric?.increment(1));
}
else {
metric?.increment(1);
}
}
return result;
};
return desc;
}
}
export function CountErrors(options: CountOptions<number> = {}): MethodDecorator {
return function countErrorsDecorator(
target: Record<string, any>,
prop: PropertyKey,
desc: PropertyDescriptor,
): PropertyDescriptor {
const original = desc.value;
const fqn = options.name || joinFqn(prop, target.constructor.name);
const log = logger('@CountErrors');
log.debug(MSG_FMT.annotatingMethod, target.constructor.name, prop, 'CountErrors', options);
const counters = new Lookup<CounterFactory<number>, Counter<number>>(() => Providers.provide(CounterFactory));
desc.value = function countErrors(...args: unknown[]): unknown {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const caller = this;
function countErr(err, promise = false): void {
const metric = counters.getInstance(
caller,
fqn,
() => counters.getFactory(caller, fqn, options.counterFactory)?.counter(fqn, options.tags),
);
Iif (!metric) {
log.warn(
'Unable to decorate %s.%s with @CountErrors! No CounterFactory available!',
target.constructor.name,
prop,
);
}
Eif (metric && accept(options)) {
metric.increment(1);
}
if (!promise) {
throw err;
}
}
let result: unknown;
try {
result = original.apply(this, ...args);
if (result instanceof Promise) {
result.catch(err => countErr(err, true));
}
}
catch (error) {
countErr(error);
}
return result;
};
return desc;
}
}
|