Skip to content

Commit

Permalink
feat: expose prometheus metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Nov 21, 2022
1 parent 691eb6d commit c37e059
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"forever": "^4.0.3",
"lodash": "^4.17.21",
"pino": "^8.7.0",
"prom-client": "^14.1.0",
"reflect-metadata": "^0.1.13",
"tslib": "^2.4.0",
"tslog": "^3.3.4",
Expand Down
25 changes: 25 additions & 0 deletions src/services/healthChecker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import formatDuration from "date-fns/formatDuration";
import intervalToDuration from "date-fns/intervalToDuration";
import express from "express";
import { Gauge, register } from "prom-client";
import { Inject, Service } from "typedi";

import config from "../config";
Expand All @@ -22,12 +23,36 @@ export class HealthChecker {
const start = new Date();
const app = express();

const latestBlockGauge = new Gauge({
name: "latest_block",
help: "Latest processed block",
});
const startTimeGauge = new Gauge({
name: "start_time",
help: "Start time, in unixtime",
});
startTimeGauge.set(start.valueOf());
const healthyGauge = new Gauge({
name: "healthy",
help: "Is service healthy",
});
healthyGauge.set(1);

app.get("/", (_, res) => {
res.send({
latestBlock: this.scanService.lastUpdated,
uptime: formatDuration(intervalToDuration({ start, end: new Date() })),
});
});
app.get("/metrics", async (_, res) => {
try {
latestBlockGauge.set(this.scanService.lastUpdated);
res.set("Content-Type", register.contentType);
res.end(await register.metrics());
} catch (ex) {
res.status(500).end(ex);
}
});

app.listen(config.port, () => {
this.log.info(`started on port ${config.port}`);
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,11 @@ bindings@^1.5.0:
dependencies:
file-uri-to-path "1.0.0"

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

bn.js@^4.11.9:
version "4.12.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
Expand Down Expand Up @@ -6050,6 +6055,13 @@ process@^0.11.10:
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==

prom-client@^14.1.0:
version "14.1.0"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.1.0.tgz#049609859483d900844924df740722c76ed1fdbb"
integrity sha512-iFWCchQmi4170omLpFXbzz62SQTmPhtBL35v0qGEVRHKcqIeiexaoYeP0vfZTujxEq3tA87iqOdRbC9svS1B9A==
dependencies:
tdigest "^0.1.1"

[email protected]:
version "0.2.14"
resolved "https://registry.yarnpkg.com/prompt/-/prompt-0.2.14.tgz#57754f64f543fd7b0845707c818ece618f05ffdc"
Expand Down Expand Up @@ -6921,6 +6933,13 @@ tapable@^2.2.0:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==

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"

text-extensions@^1.0.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/text-extensions/-/text-extensions-1.9.0.tgz#1853e45fee39c945ce6f6c36b2d659b5aabc2a26"
Expand Down

0 comments on commit c37e059

Please sign in to comment.