/
common.ts
61 lines (52 loc) 路 1.88 KB
/
common.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
import { Counter, MetricOptions } from '@opentelemetry/api-metrics';
import { copyMetadataFromFunctionToFunction } from '../../opentelemetry.utils';
import { getOrCreateCounter } from '../metric-data';
/**
* Create and increment a counter when a new instance is created
*
* @param originalClass
*/
export const OtelInstanceCounter = (options?: MetricOptions) => <
T extends { new (...args: any[]): {} },
>(
originalClass: T,
) => {
const name = `app_${originalClass.name}_instances_total`;
const description = `app_${originalClass.name} object instances total`;
let counterMetric: Counter;
const wrappedClass = class extends originalClass {
constructor(...args) {
if (!counterMetric) {
counterMetric = getOrCreateCounter(name, { description, ...options });
}
counterMetric.add(1);
super(...args);
}
};
copyMetadataFromFunctionToFunction(originalClass, wrappedClass);
return wrappedClass;
};
/**
* Create and increment a counter when the method is called
*/
export const OtelMethodCounter = (options?: MetricOptions) => (
target: Object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<(...args: any[]) => any>,
) => {
const className = target.constructor.name;
const name = `app_${className}_${propertyKey.toString()}_calls_total`;
const description = `app_${className}#${propertyKey.toString()} called total`;
let counterMetric: Counter;
const originalFunction = descriptor.value;
const wrappedFunction = function PropertyDescriptor(...args: any[]) {
if (!counterMetric) {
counterMetric = getOrCreateCounter(name, { description, ...options });
}
counterMetric.add(1);
return originalFunction.apply(this, args);
};
// eslint-disable-next-line no-param-reassign, func-names
descriptor.value = wrappedFunction;
copyMetadataFromFunctionToFunction(originalFunction, wrappedFunction);
};