本文介绍了如何在NodeJS中一次调用服务器功能上的Prometheus Histogram.observe()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Prometheus监视NodeJS应用.

I am monitoring a NodeJS app using Prometheus.

创建这样的直方图

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

现在,我像这样调用histogram.observe()来监视对路径'/'

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

现在的问题是,我在NodesJS应用程序中还有许多其他请求路径,因此,为了监视每个请求路径,我是否应该手动编辑为请求服务的每个函数.

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

还有其他方法可以让我们从外部在每个函数上调用histogram.observe()而不用手动编辑吗?

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

推荐答案

可能的解决方案:

  1. 请参阅Express Prom Bundle库以跨不同路径自动衡量指标. https://github.com/jochen-schweizer/express-prom-bundle

完成请求处理后,使用onFinished回调调用Histogram.Observe() https://github.com/jshttp/on-finished

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

这篇关于如何在NodeJS中一次调用服务器功能上的Prometheus Histogram.observe()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 03:59