Skip to content

Commit

Permalink
Run prometheus-exporter as a module #671
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent FAYOLLE committed Oct 24, 2023
1 parent 06559da commit a7a14e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
4 changes: 0 additions & 4 deletions sandbox/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ if [[ "$GRIST_SANDBOX_FLAVOR" = "gvisor" ]]; then
source ./sandbox/gvisor/get_checkpoint_path.sh
fi

if [[ -n "$GRIST_PROMCLIENT_PORT" ]]; then
require_promclient="--require ./_build/stubs/app/server/prometheus-exporter.js"
fi

NODE_PATH=_build:_build/stubs:_build/ext node ${require_promclient} _build/stubs/app/server/server.js
6 changes: 0 additions & 6 deletions sandbox/watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ if [ ! -e _build ]; then
buildtools/build.sh
fi

if [ -n "$GRIST_PROMCLIENT_PORT" ]; then
require_promclient="--require ./_build/stubs/app/server/prometheus-exporter.js"
fi

ls _build/stubs/app/server

tsc --build -w --preserveWatchOutput $PROJECT &
catw app/client/*.css app/client/*/*.css -o static/bundle.css -v & webpack --config $WEBPACK_CONFIG --mode development --watch &
NODE_PATH=_build:_build/stubs:_build/ext nodemon ${NODE_INSPECT:+--inspect} --delay 1 -w _build/app/server -w _build/app/common ${require_promclient} _build/stubs/app/server/server.js &
Expand Down
25 changes: 13 additions & 12 deletions stubs/app/server/prometheus-exporter.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { collectDefaultMetrics, register } from 'prom-client';
import http from 'http';

collectDefaultMetrics();

const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
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);
});
});
};

const port = parseInt(process.env.GRIST_PROMCLIENT_PORT!, 10);
if (isNaN(port)) {
throw new Error(`Invalid port: ${process.env.GRIST_PROMCLIENT_PORT}`);
}
server.listen(port, '0.0.0.0');
export function runPrometheusExporter(port: number) {
collectDefaultMetrics();

console.log("---------------------------------------------");
console.log(`Prometheus exporter listening on port ${port}`);
console.log("---------------------------------------------");
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;
}
5 changes: 5 additions & 0 deletions stubs/app/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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', '[email protected]');
// Set directory for uploaded documents.
Expand Down

0 comments on commit a7a14e3

Please sign in to comment.