siimon/prom-client

how to expose the metrics end-point ?

arthurvaverko opened this issue · 2 comments

I think I'm missing something here ..
i have set up this ..

import * as prom from 'prom-client'

prom.collectDefaultMetrics({register: prom.register})

now I want to expose a /metrics endpoint on port 9090 ..
how to i hook it up ?

the only running server i have is a yoga graph ql server on port 8080 ..
should i use express ? or is there any other way ? i could not find any example

this is what i used so far it works but there is no example documented and i think there should be ..

import * as prom from 'prom-client'
import {createServer, IncomingMessage, ServerResponse} from 'http'

prom.collectDefaultMetrics({register: prom.register})
const metricsSerer = createServer(async (request: IncomingMessage, response: ServerResponse) => {
  response.end(await prom.register.metrics());
});

metricsSerer.listen(METRICS_SERVER_PORT, () => {
    console.info(`GQL Server is running on http://localhost:${METRICS_SERVER_PORT}/metrics`);
});

I'm using NestJs web framework and outputting the metrics information that is the style of

@Get("metrics")
async metrics(@Res() res: Response) {
    let _metrics = await PromClient.register.metrics();
    res.set({ 'Content-Type': PromClient.register.contentType }).send(_metrics);
}

NestJS: https://nestjs.com/

Check out the examples directory, e.g.:

// Setup server to Prometheus scrapes:
server.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (ex) {
res.status(500).end(ex);
}
});