gristlabs_grist-core/stubs/app/server/prometheus-exporter.ts
Florent 38c8476aff
Add option to serve Prometheus metrics #671 (#693)
Co-authored-by: Florent FAYOLLE <florent.fayolle@beta.gouv.fr>
2023-10-24 17:36:53 -04:00

26 lines
725 B
TypeScript

import { collectDefaultMetrics, register } from 'prom-client';
import http from 'http';
const reqListener = (req: http.IncomingMessage, res: http.ServerResponse) => {
register.metrics().then((metrics) => {
res.writeHead(200, { 'Content-Type': register.contentType });
res.end(metrics);
}).catch((e) => {
res.writeHead(500);
res.end(e.message);
});
};
export function runPrometheusExporter(port: number) {
collectDefaultMetrics();
if (isNaN(port)) {
throw new Error(`Invalid port: ${process.env.GRIST_PROMCLIENT_PORT}`);
}
const server = http.createServer(reqListener);
server.listen(port, '0.0.0.0');
console.log(`Prometheus exporter listening on port ${port}.`);
return server;
}