Skip to content

Commit

Permalink
fix: update healthchecker
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed May 3, 2024
1 parent 17e1736 commit 00dc6ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 471 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@gearbox-protocol/sdk": "^3.0.0-next.177",
"@redstone-finance/evm-connector": "^0.3.6",
"@types/commander": "^2.12.2",
"@types/express": "^4.17.20",
"@uniswap/sdk-core": "^4.2.0",
"@uniswap/v3-sdk": "^3.11.1",
"amqplib": "^0.10.3",
Expand All @@ -39,12 +38,10 @@
"date-fns": "^3.6.0",
"dotenv": "^16.4.5",
"ethers": "^5.7.1",
"express": "^4.19.2",
"forever": "^4.0.3",
"lodash": "^4.17.21",
"p-retry": "4.6.2",
"pino": "^8.19.0",
"prom-client": "^15.1.2",
"redstone-protocol": "^1.0.5",
"reflect-metadata": "^0.2.2",
"tslib": "^2.6.2",
Expand Down
94 changes: 54 additions & 40 deletions src/services/healthChecker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { formatDuration, intervalToDuration } from "date-fns";
import express from "express";
import { Gauge, register } from "prom-client";
import { createServer } from "node:http";

import { Inject, Service } from "typedi";

import config from "../config";
Expand All @@ -15,55 +14,70 @@ export class HealthChecker {
@Inject()
scanServiceV3: ScanServiceV3;

#start = Math.round(new Date().valueOf() / 1000);

/**
* Launches health checker - simple express server
*/
public launch(): void {
if (config.optimisticLiquidations) {
return;
}
const start = new Date();
const app = express();

const latestBlockGauge = new Gauge({
name: "eth_block_number",
help: "Latest processed block",
});
const startTimeGauge = new Gauge({
name: "start_time",
help: "Start time, in unixtime",
});
startTimeGauge.set(Math.round(start.valueOf() / 1000));
// pseudo-metric that provides metadata about the running binary
const buildInfo = new Gauge({
name: "liquidator_ts_build_info",
help: "Build info",
labelNames: ["version"],
});
buildInfo.set({ version: config.version }, 1);

app.get("/", (_, res) => {
res.send({
latestBlock: this.scanServiceV3.lastUpdated,
uptime: formatDuration(intervalToDuration({ start, end: new Date() })),
});
});
app.get("/metrics", async (_, res) => {
try {
latestBlockGauge.set(
isFinite(this.scanServiceV3.lastUpdated)
? this.scanServiceV3.lastUpdated
: 0,
const server = createServer(async (req, res) => {
// Routing
if (req.url === "/") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
start_time: this.#start,
block_number: this.scanServiceV3.lastUpdated,
version: config.version,
}),
);
res.set("Content-Type", register.contentType);
res.end(await register.metrics());
} catch (ex) {
res.status(500).end(ex);
} else if (req.url === "/metrics") {
try {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(this.#metrics());
} catch (ex) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("error");
}
} else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("not found");
}
});

app.listen(config.port, () => {
this.log.info(`started on port ${config.port}`);
server.listen(config.port, () => {
this.log.debug("listening");
});

process.on("SIGTERM", () => {
this.log.info("terminating");
server.close();
});

this.log.info("launched");
}

/**
* Returns metrics in prometheus format
* https://prometheus.io/docs/concepts/data_model/
*/
#metrics(): string {
return `# HELP start_time Start time, in unixtime
# TYPE start_time gauge
start_time ${this.#start}
# HELP build_info Build info
# TYPE build_info gauge
build_info{version="${config.version}"} 1
# HELP block_number Latest processed block
# TYPE block_number gauge
block_number ${this.scanServiceV3.lastUpdated}
`;
}
}
Loading

0 comments on commit 00dc6ff

Please sign in to comment.