首页 > 解决方案 > 使用 prometheus 监控 node.js axios 请求

问题描述

我有带有对外部 api 的 axios 请求的 node.js 微服务,我需要用 prometheus 监控它们。如我所见,prometheus 用于监控快速请求:

app.use((req, res, next) => {
  const responseTimeInMs = Date.now() - res.locals.startEpoch;

  httpRequestDurationMs
      .labels(req.method, req.route.path, res.statusCode)
      .observe(responseTimeInMs);

  next();
});

但是我发现无法将它与 axios 一起使用(例如):

function getData() {
  return axios.get(url)
    .then (res) => {
      [should put metrics somewhere here]
    }
}

希望有人可以帮助解决这个问题。

标签: node.jsaxiosmetricsprometheus

解决方案


prom-client文档 - 您可以启动计时器并在完成时调用 return 方法:

const end = gauge.startTimer();
xhrRequest(function(err, res) {
  end(); // Sets value to xhrRequests duration in seconds
});

与仪表、直方图、摘要或任何其他对象一起使用...


推荐阅读