【问题标题】:Monitoring node.js axios requests with prometheus使用 prometheus 监控 node.js axios 请求
【发布时间】:2019-03-12 07:40:14
【问题描述】:

我有 node.js 微服务,带有对外部 api 的 axios 请求,我需要用 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]
    }
}

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

【问题讨论】:

  • 试试这个。 blog.risingstack.com/…
  • 我已经用过,但例子中只有express。我已经在问题的第一部分描述了它,这不是我需要的东西

标签: node.js axios metrics prometheus


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 2022-11-04
    • 2018-05-22
    • 1970-01-01
    • 2013-01-11
    • 2021-10-30
    • 2019-10-23
    • 1970-01-01
    相关资源
    最近更新 更多