Prometheus metrics dont work on self defined node.js application

1.6k views Asked by At

im follwing some tuorials, they are all doing it the same way with including the 'prom-client' and creating a new /metrics url. then i access my http://localhost:4000/metrics im getting follwing error instad my metrics:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Promise
    at new NodeError (node:internal/errors:372:5)
    at write_ (node:_http_outgoing:742:11)
    at ServerResponse.end (node:_http_outgoing:855:5)
    at /server.js:79:9
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at next (/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at /node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/node_modules/express/lib/router/index.js:341:12)

server.js:


//coniguration for server
const express= require('express');
const app= express();
const https= require('https');
const http = require('http');
const fs = require('fs');
const PORT=process.env.PORT || 4000


const client = require('prom-client')
const register = new client.Registry()

register.setDefaultLabels({
    app: 'chat-api'
})
client.collectDefaultMetrics({register})

const httpsRequestDuractionMicroseconds = new client.Histogram({
    name: 'httpsreq',
    help: 'httpsreqhelp',
    labelNames: ['method','route','code'],
    buckets: [0.1,0.3,0.5,1,3,4,5,7]


})

register.registerMetric(httpsRequestDuractionMicroseconds)

//server inizialisation
//var server= https.createServer(options,app)
var server=http.createServer(app)

const {Server} = require('socket.io');
const { response } = require('./routes/posts');

const io= new Server(server,{maxHttpBufferSize:10e7})





server.listen(PORT, ()=>{
    console.log('server is listening on port '+PORT)
})



//array for displaying users
var arrayUsers=[]


//app.get('/test', (req,res)=>{
  //  res.sendStatus(200);
//})

//app pages
app.use(express.static(__dirname + '/public'))

app.use(require('./routes/posts'));

app.get('/metrics',(req,res)=>{
    res.setHeader('Content-Type',register.contentType)
    res.end(register.metrics())
})

app.get('/',(req,res)=>{
    res.sendFile(__dirname + '/login_register/login.html')
})


app.get('/register',(req,res)=>{
    res.sendFile(__dirname + '/login_register/register.html')
})

app.get('/chatroom',(req,res)=>{
    res.sendFile(__dirname + '/index.html')
})



//on new socket connection
//socket event simple message and file message

io.on('connection',(socket)=>{
    console.log('new connection',socket.id)
    socket.on('message', (msg)=>{
        socket.broadcast.emit('message',msg)
    })
    socket.on('file-message', (msg)=>{
        socket.broadcast.emit('file-message',msg)
    })


    socket.on('user-connected', (userName_)=>{
        console.log(userName_ + " joined " +socket.id)

        //push username and socket.id into array to display users in chat client
        arrayUsers.push({
            socket_id: socket.id,
            userName_socket: userName_
        })

        console.log(arrayUsers)
        //client message for users that a new user joined
        socket.broadcast.emit('user-connected',userName_)
        
        //update online-users list
        socket.emit('online-users',arrayUsers)
        socket.broadcast.emit('online-users',arrayUsers)

    })

    

    socket.on('disconnect',()=>{
        
        //find socket.id of disconnected user to delete him from array
        let arrayMatch=arrayUsers.find(arrayMatch=>arrayMatch.socket_id===socket.id)
           
            
            let index = arrayUsers.findIndex(arrayMatch => {
                return arrayMatch.socket_id === socket.id;
              });

           console.log(index)


            //chat message to other clients that user disconnected
            
            try{

            
            socket.broadcast.emit('user_disconnected',arrayMatch.userName_socket)
        
            //splice disconnected user from array
            arrayUsers.splice(index,1)

            console.log(arrayUsers)

            //update online-userlist
            socket.broadcast.emit('online-users',arrayUsers)

          
            }catch{}

    })

})

in my prometheus dashboard it also shows me that my server.js application isnt connected too, i dont know how to fix it

1

There are 1 answers

0
user17437274 On

Same issue on my side with prom-client module. register.metrics() returns a Promise. I'm not an expert of Express, I don't know if it's the right way to use a Promise in res.end() method. Anyway, I just resolved the Promise like this and get the Prometheus metrics returned as plain text :

app.get('/metrics', (req,res) => {
    res.setHeader('Content-Type',register.contentType)
    register.metrics().then(data => res.send(data));
})