使用 prometheus 监控 node.js axios 请求

Monitoring node.js axios requests with prometheus

我有 node.js 微服务,带有对外部 api 的 axios 请求,我需要用 prometheus 监控它们。如我所见,prometheus是用来监听express requests的:

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]
    }
}

希望有人能帮忙。

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

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

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