Skip to content

Isahara86/tracker-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published