node.jsaxiosmetricsprometheus

Monitoring node.js axios requests with prometheus


I have node.js microservice with axios requests to external api, and I need monitor them with prometheus. As I see, prometheus is made to monitor 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();
});

But I found no way to use it with axios (for example):

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

Hope someone could help with this.


Solution

  • From prom-client documentation - you can start a timer and call the return method on finish:

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

    Use it with gauge, histogram, summary or any other object...