Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prom-client returning empty object for default metrics in NodeJS application

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

enter image description here

I am newly using the prom-client from npm. How can I make this work?

like image 711
Keet Sugathadasa Avatar asked Dec 04 '25 13:12

Keet Sugathadasa


1 Answers

Found out the issue. What you need to realize is, register.metrics() returns a promise. Hence, using await register.metrics() will return the expected response for default metrics. The updated code snippet is given below.

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', async function (req, res) {
    // Return all metrics the Prometheus exposition format
    res.set('Content-Type', register.contentType);
    let metrics = await register.metrics();
    res.send(metrics);
})

let server = app.listen(8080, function () {
    let port = server.address().port
    console.log("Application running on port: %s", port)
})

Now, navigate to http://localhost:8080/metrics in your browser and see the default metrics.

like image 50
Keet Sugathadasa Avatar answered Dec 07 '25 13:12

Keet Sugathadasa