0

我正在使用 Prometheus 监控 NodeJS 应用程序。

创建这样的直方图

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() 来监视对路径的请求'/'

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 应用程序中有许多其他请求路径,所以为了监视每个请求路径,我是否应该手动编辑每个服务请求的函数。

或者

有没有其他方法可以让我们从外部对每个函数调用 histogram.observe() 而无需手动编辑?

4

1 回答 1

0

可能的解决方案 :

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

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

于 2020-11-26T06:29:06.527 回答