问题
I am monitoring a NodeJS app using Prometheus.
Creating a Histogram like this
const histogram = new client.Histogram({
name: 'node_request_duration_seconds',
help: 'Histogram for the duration in seconds.',
buckets: [1, 2, 5, 6, 10]
});
Now I am calling histogram.observe() like this to monitor a request to the path '/'
const app = express();
app.get('/', (req, res) => {
//Simulate a sleep
var start = new Date()
var simulateTime = 1000
setTimeout(function(argument) {
// execution time simulated with setTimeout function
var end = new Date() - start
histogram.observe(end / 1000); //convert to seconds
}, simulateTime)
counter.inc();
res.send('Hello world\n');
});
Now the problem is I have many other requests paths in the NodesJS app, So in order to monitor on the every request path, Should I manually edit every function that serves a request.
OR
Is there any other way so we can call histogram.observe() on every function from outside without editing manually?
回答1:
Possible solutions :
Refer Express Prom Bundle library to measure metrics automatically across different paths. https://github.com/jochen-schweizer/express-prom-bundle
Use onFinished callback to call Histogram.Observe() after the request processing is done https://github.com/jshttp/on-finished
来源:https://stackoverflow.com/questions/64692856/how-to-call-prometheus-histogram-observe-on-serveral-functions-at-once-in-node