@isahara/tracker-decorator
v0.4.1
Published
Tracker decorator
Downloads
1
Readme
Agenda
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 ...
}
}
Description
Installation
npm install @isahara/tracker-decorator
Example
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
// }
// }
Supported features
Set custom metrics callback
import { setTrackerCallback } from '@isahara/tracker-decorator';
setTrackerCallback((name, time, dataIn, dataOut)=>{
console.log(name, time, dataIn, dataOut);
});
Specify custom metric name
By default metric name is '[ClassName] + [MethodName]' When you need to override default you can add custom name.
@Tracker({name: 'my-metric'})