A very simple decorator for metrics gathering.
Metrics are vital for any high load project and there are many ways to implement metrics
You can always do it like that:
//-------- ANTI PATTERN --------//
function myFunc() {
const start = Date.now();
... your logic ...
metrics.add('metric name', Date.now() - start);
}
Byt there is a cleaner way:
@Tracker()
function myFunc() {
... your logic ...
}
}
npm install @isahara/tracker-decorator
import { Tracker, metrics } from '@isahara/tracker-decorator';
class MyClass {
@Tracker()
method() { // if the method retuns a Promise it will work any way
...
}
}
const myClassObj = new MyClass();
myClassObj.method();
console.log(metrics);
// { 'MyClass>method':{
// counter: 75,
// minTime: 53,
// avgTime: 112,
// maxTime: 232
// }
// }
import { setTrackerCallback } from '@isahara/tracker-decorator';
setTrackerCallback((name, time, dataIn, dataOut)=>{
console.log(name, time, dataIn, dataOut);
});
By default metric name is '[ClassName] + [MethodName]' When you need to override default you can add custom name.
@Tracker({name: 'my-metric'})