cogitoergosum
cogitoergosum

Reputation: 2471

Prometheus metrics not generated

I am working with this example to make my NodeJs application expose Prometheus metrics. When I access the /metrics endpoint, I don't see output. I am new to Prometheus or for that matter custom metrics in general.

Here is my attempt.

const express = require('express')
const promClient = require('prom-client')
const Registry = promClient.Registry;
const register = new Registry();

const app = express()
const port = process.env.PORT || 3000

const histogram = new promClient.Histogram({
  name: 'hello_lb:http_request_duration',
  help: 'Duration of HTTP requests in ms',
  labelNames: ['method', 'status_code'],
  buckets: [0.1, 5, 15, 50, 100, 500]
});
register.registerMetric(histogram);

app.get('/hello', (req, res) => {
  const end = histogram.startTimer();
  res.send('Hello World!')
  end({ method: req.method, 'status_code': 200 });
})

// expose our metrics at the default URL for Prometheus
app.get('/metrics', (request, response) => {
  response.set('Content-Type', promClient.register.contentType);
  response.send(promClient.register.metrics());
});

app.listen(port, () => {
  console.log(`App is up at ${port}`)
})
  1. Why aren't many metrics seen at the /metrics end-point?
  2. This example refers to a histogram of duration of HTTP requests in ms. How do I create a metric for total HTTP requests per second? This blog post seems to use total HTTP requests per second; but, it does not explain how the metric changed from http_requests_total to http_requests_per_second. (Ignore the Kubernetes, EKS, FluxCD, auto-scaling and Go part of the blog.)

Upvotes: 1

Views: 2850

Answers (1)

cogitoergosum
cogitoergosum

Reputation: 2471

Why aren't many metrics seen at /metrics end-point?

Because, the register.metrics returns a Promise that should be awaited. From another blog post.

This worked.

// expose our metrics at the default URL for Prometheus
app.get('/metrics', (async (request, response) => {
  response.set('Content-Type', promClient.register.contentType);
  response.send(await promClient.register.metrics());
}));

How the metric changed from http_requests_total to http_requests_per_second ?

Here is a hint from this tutorial.

Install the Prometheus Adapter and configure it to turn the metric from Prometheus into a per-second request rate (using PromQL) and expose that metric as myapp_requests_per_second through the Custom Metrics API

Upvotes: 4

Related Questions