How to measure of typescript method execution time using decorators and publish it to Prometheus?

TL;DR perf_hooks + wrapper which will redefine the original method to new method who will call the original method + publish information to Prometheus. Define a Histogram from prom-client library: import { Histogram } from 'prom-client' const requestDuration = new Histogram({ name: 'request_duration_ms', help: 'Duration of requests', labelNames: ['method'], buckets: [ 0.1, 50, 100, 200, 300, 400, 500, 1000 ] // buckets for response time from 0.1ms to 1000ms }) The decorator for non-async methods will be simple - redefine the method to this new method who will call the original method. Make sure that you use the function’s this context instead of the value of this when it is called (no arrow function) ...

March 24, 2020