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.