使用prometheus监控node.js axios请求

问题描述 投票:0回答:2

我有node.js微服务,带有axios对外部api的请求,我需要用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.js axios metrics prometheus
2个回答
0
投票

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

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

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


0
投票

我参加聚会已经很晚了,但考虑到这篇文章在谷歌搜索结果中的排名仍然很高:

我为此目的创建了一个(因为这是使用 Axios 时的常见问题)

import { AxiosMetricsCollector } from "@sorooshme/axios-metrics-collector";
import axios from "axios";

const client = axios.create();

new AxiosMetricsCollector(client, (metrics) => {
  // you can use the provided `metrics` and expose it via Prometheus
  // for example:
  myCounter.increment(1, { status: metrics.status });
});

await client.get("https://example.org");
© www.soinside.com 2019 - 2024. All rights reserved.