I have a NodeJS application, and I want to expose the default metrics. I have the following implementation. I am using the prom-client package.
let express = require('express');
let app = express();
const client = require('prom-client');
// Create a Registry which registers the metrics
const register = new client.Registry()
// Add a default label which is added to all metrics
register.setDefaultLabels({
app: 'example-nodejs-app'
})
// Enable the collection of default metrics
client.collectDefaultMetrics({ register })
app.get('/metrics', function (req, res) {
// Return all metrics the Prometheus exposition format
res.set('Content-Type', register.contentType)
res.send(register.metrics())
})
let server = app.listen(8080, function () {
let port = server.address().port
console.log("Application running on port: %s", port)
})
When I navigate to http://localhost:8080/metrics, I get an empty object ({}
) as a response. When I check my Prometheus Targets, I see the following error.
"INVALID" is not a valid start token
I am newly using the prom-client from npm. How can I make this work?
Found out the issue. What you need to realize is,
register.metrics()
returns a promise. Hence, usingawait register.metrics()
will return the expected response for default metrics. The updated code snippet is given below.Now, navigate to http://localhost:8080/metrics in your browser and see the default metrics.