Skip to content

Commit

Permalink
feat: include support for metrics (#131)
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Mastrangelo <[email protected]>
  • Loading branch information
acuarica committed Feb 16, 2024
1 parent 91eb632 commit 57946ac
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 8 deletions.
120 changes: 113 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@
"express": "^4.17.1",
"express-fileupload": "^1.4.0",
"express-openapi-validator": "^5.0.4",
"express-prom-bundle": "^7.0.0",
"express-session": "^1.17.1",
"http-status-codes": "^2.1.4",
"ipfs-http-client": "^56.0.3",
"ipfs-http-gateway": "0.9.3",
"json-refs": "^3.0.15",
"memorystore": "^1.6.7",
"node-fetch": "2.6.6",
"prom-client": "^15.1.0",
"puppeteer": "^20.7.4",
"serve-index": "^1.9.1",
"solc": "^0.8.17",
Expand Down Expand Up @@ -131,4 +133,4 @@
"text-summary"
]
}
}
}
35 changes: 35 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import { initDeprecatedRoutes } from "./deprecated.routes";
import { getAddress } from "ethers";
import { logger } from "../common/loggerLoki";
import { setLibSourcifyLogger } from "@ethereum-sourcify/lib-sourcify";

import promBundle from "express-prom-bundle";
import { SourcifyEventManager } from "../common/SourcifyEventManager/SourcifyEventManager";

import client from "prom-client";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const fileUpload = require("express-fileupload");

Expand Down Expand Up @@ -175,6 +181,35 @@ export class Server {
next();
});

// Add the options to the prometheus middleware most option are for http_request_duration_seconds histogram metric
const metricsMiddleware = promBundle({
includeMethod: true,
includePath: true,
includeStatusCode: true,
includeUp: true,
httpDurationMetricName: "sourcify_http_request_duration_seconds",
promClient: {
collectDefaultMetrics: {
prefix: "sourcify_",
}
}
});
// add the prometheus middleware to all routes
this.app.use(metricsMiddleware);

const counter = new client.Counter({
name: "sourcify_event_count",
help: "events that happened during verification labeled with: event and chainId",
labelNames: ["event", "chainId"],
});

SourcifyEventManager.on("*", [
(event: string, argument: any) => {
const chainId = argument.chainId;
counter.labels({ event, chainId }).inc(1);
},
]);

// Session API endpoints require non "*" origins because of the session cookies
const sessionPaths = [
"/session", // all paths /session/verify /session/input-files etc.
Expand Down

0 comments on commit 57946ac

Please sign in to comment.