Skip to content

Commit

Permalink
Add option to serve Prometheus metrics #671
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent FAYOLLE committed Oct 12, 2023
1 parent 6dfe634 commit 06559da
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ PORT | port number to listen on for Grist server
REDIS_URL | optional redis server for browser sessions and db query caching
GRIST_SNAPSHOT_TIME_CAP | optional. Define the caps for tracking buckets. Usage: {"hour": 25, "day": 32, "isoWeek": 12, "month": 96, "year": 1000}
GRIST_SNAPSHOT_KEEP | optional. Number of recent snapshots to retain unconditionally for a document, regardless of when they were made
GRIST_PROMCLIENT_PORT | optional. If set, serve the Prometheus metrics on the specified port number. ⚠️ Be sure to use a port which is not publicly exposed ⚠️.

#### AI Formula Assistant related variables (all optional):

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
"plotly.js-basic-dist": "2.13.2",
"popper-max-size-modifier": "0.2.0",
"popweasel": "0.1.18",
"prom-client": "14.2.0",
"qrcode": "1.5.0",
"randomcolor": "0.5.3",
"redis": "3.1.1",
Expand Down
6 changes: 5 additions & 1 deletion sandbox/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ if [[ "$GRIST_SANDBOX_FLAVOR" = "gvisor" ]]; then
source ./sandbox/gvisor/get_checkpoint_path.sh
fi

NODE_PATH=_build:_build/stubs:_build/ext node _build/stubs/app/server/server.js
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
8 changes: 7 additions & 1 deletion sandbox/watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ 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 _build/stubs/app/server/server.js &
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 &

wait
24 changes: 24 additions & 0 deletions stubs/app/server/prometheus-exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { collectDefaultMetrics, register } from 'prom-client';
import http from 'http';

collectDefaultMetrics();

const server = http.createServer((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');

console.log("---------------------------------------------");
console.log(`Prometheus exporter listening on port ${port}`);
console.log("---------------------------------------------");
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,11 @@ binary@~0.3.0:
buffers "~0.1.1"
chainsaw "~0.1.0"

[email protected]:
version "1.0.2"
resolved "https://registry.yarnpkg.com/bintrees/-/bintrees-1.0.2.tgz#49f896d6e858a4a499df85c38fb399b9aff840f8"
integrity sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==

bl@^4.0.3:
version "4.1.0"
resolved "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz"
Expand Down Expand Up @@ -6762,6 +6767,13 @@ process@~0.11.0:
resolved "https://registry.npmjs.org/process/-/process-0.11.10.tgz"
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=

[email protected]:
version "14.2.0"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.2.0.tgz#ca94504e64156f6506574c25fb1c34df7812cf11"
integrity sha512-sF308EhTenb/pDRPakm+WgiN+VdM/T1RaHj1x+MvAuT8UiQP8JmOEbxVqtkbfR4LrvOg5n7ic01kRBDGXjYikA==
dependencies:
tdigest "^0.1.1"

promise-inflight@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3"
Expand Down Expand Up @@ -7960,6 +7972,13 @@ tcp-port-used@^1.0.1:
debug "4.3.1"
is2 "^2.0.6"

tdigest@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/tdigest/-/tdigest-0.1.2.tgz#96c64bac4ff10746b910b0e23b515794e12faced"
integrity sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==
dependencies:
bintrees "1.0.2"

term-size@^2.1.0:
version "2.2.0"
resolved "https://registry.npmjs.org/term-size/-/term-size-2.2.0.tgz"
Expand Down

0 comments on commit 06559da

Please sign in to comment.