0

I have an express application running with pm2 in cluster mode.

We are monitoring with prometheus.

I've been able to monitor the standard metrics of the cluster running a second process working as an exporter for prometheus.

I need a metric with the number of http requests served by the cluster, for this i've created the custom metric of type counter with the official docs, but i've been unable to display it.

I didn't thought this was going to be that hard to do something so common.

I wouldn't like to put nginx as proxy just for counting the number of requests.

Hans Poo
  • 804
  • 1
  • 8
  • 17

1 Answers1

0

I've made a poc and it works, for example this express app declares a custom variable counter and call inc() in each request:

import * as express from 'express';
import tx2 = require('tx2');

const app = express();

const counter = tx2.counter({ name: 'num peticiones' });

app.get('/api', (req, res) => {
  counter.inc();
  res.send({ message: 'Welcome to api!' });
});

const port = process.env.port || 3333;
const server = app.listen(port, () => {
  console.log(`Listening at http://localhost:${port}/api`);
});
server.on('error', console.error);

Then we start it with pm2:

pm2 start --name prueba

Then i've used the pm2 prometheus exporter in: https://github.com/saikatharryc/pm2-prometheus-exporter

And if i start it, i can see the values for each of the instances in http://localhost:9209/metrics. To test it i've run apache bench, killing one of the workers in between and its'ok, that's why one of them has only 25 requests.

pm2_num_peticiones{id="0",name="prueba",version="0.0.0",instance="0",interpreter="node",node_version="16.15.0"} 50
pm2_num_peticiones{id="1",name="prueba",version="0.0.0",instance="1",interpreter="node",node_version="16.15.0"} 25
pm2_num_peticiones{id="2",name="prueba",version="0.0.0",instance="2",interpreter="node",node_version="16.15.0"} 50
pm2_num_peticiones{id="3",name="prueba",version="0.0.0",instance="3",interpreter="node",node_version="16.15.0"} 50

I expect this post can help others.

One gotcha is that the exporter should be run directly with node, using pm2 results in the metrics showing but not incrementing at all.

Hans Poo
  • 804
  • 1
  • 8
  • 17