How to call Prometheus Histogram.observe() on serveral Functions at once in NodeJS

不羁的心 提交于 2021-02-11 15:12:16

问题


I am monitoring a NodeJS app using Prometheus.

Creating a Histogram like this

const histogram = new client.Histogram({
    name: 'node_request_duration_seconds',
    help: 'Histogram for the duration in seconds.',
    buckets: [1, 2, 5, 6, 10]
});

Now I am calling histogram.observe() like this to monitor a request to the path '/'

const app = express();
app.get('/', (req, res) => {

    //Simulate a sleep
    var start = new Date()
    var simulateTime = 1000

    setTimeout(function(argument) {
        // execution time simulated with setTimeout function
        var end = new Date() - start
        histogram.observe(end / 1000); //convert to seconds
    }, simulateTime)

    counter.inc();

    res.send('Hello world\n');
});

Now the problem is I have many other requests paths in the NodesJS app, So in order to monitor on the every request path, Should I manually edit every function that serves a request.

OR

Is there any other way so we can call histogram.observe() on every function from outside without editing manually?


回答1:


Possible solutions :

  1. Refer Express Prom Bundle library to measure metrics automatically across different paths. https://github.com/jochen-schweizer/express-prom-bundle

  2. Use onFinished callback to call Histogram.Observe() after the request processing is done https://github.com/jshttp/on-finished



来源:https://stackoverflow.com/questions/64692856/how-to-call-prometheus-histogram-observe-on-serveral-functions-at-once-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!