Add option to serve Prometheus metrics #671 (#693)

Co-authored-by: Florent FAYOLLE <florent.fayolle@beta.gouv.fr>
This commit is contained in:
Florent
2023-10-24 23:36:53 +02:00
committed by GitHub
parent e51c1b6b92
commit 38c8476aff
5 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
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;
}

View File

@@ -36,6 +36,7 @@ setDefaultEnv('GRIST_WIDGET_LIST_URL', commonUrls.gristLabsWidgetRepository);
import {updateDb} from 'app/server/lib/dbUtils';
import {main as mergedServerMain, parseServerTypes} from 'app/server/mergedServerMain';
import * as fse from 'fs-extra';
import {runPrometheusExporter} from './prometheus-exporter';
const G = {
port: parseInt(process.env.PORT!, 10) || 8484,
@@ -102,6 +103,10 @@ export async function main() {
console.log('For full logs, re-run with DEBUG=1');
}
if (process.env.GRIST_PROMCLIENT_PORT) {
runPrometheusExporter(parseInt(process.env.GRIST_PROMCLIENT_PORT, 10));
}
// If SAML is not configured, there's no login system, so provide a default email address.
setDefaultEnv('GRIST_DEFAULT_EMAIL', 'you@example.com');
// Set directory for uploaded documents.