use prom-client

This commit is contained in:
Hendrik Schutter 2025-01-11 22:59:07 +01:00
parent 574a63b2a3
commit 9b00853d5a
3 changed files with 65 additions and 5 deletions

View File

@ -14,6 +14,7 @@
"express": "^4.21.2",
"http-status-codes": "^2.3.0",
"mariadb": "^3.4.0",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.2.2",
"sequelize": "^6.37.5",
"swagger-jsdoc": "^6.2.8",
@ -120,6 +121,15 @@
"integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==",
"license": "MIT"
},
"node_modules/@opentelemetry/api": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",
"integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",
"license": "Apache-2.0",
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@scarf/scarf": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/@scarf/scarf/-/scarf-1.4.0.tgz",
@ -386,6 +396,12 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/bintrees": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz",
"integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==",
"license": "MIT"
},
"node_modules/body-parser": {
"version": "1.20.3",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz",
@ -1434,6 +1450,19 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/prom-client": {
"version": "15.1.3",
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-15.1.3.tgz",
"integrity": "sha512-6ZiOBfCywsD4k1BN9IX0uZhF+tJkV8q8llP64G5Hajs4JOeVLPCwpPVcpXy3BwYiUGgyJzsJJQeOIv7+hDSq8g==",
"license": "Apache-2.0",
"dependencies": {
"@opentelemetry/api": "^1.4.0",
"tdigest": "^0.1.1"
},
"engines": {
"node": "^16 || ^18 || >=20"
}
},
"node_modules/proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
@ -1873,6 +1902,15 @@
"express": ">=4.0.0 || >=5.0.0-beta"
}
},
"node_modules/tdigest": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz",
"integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==",
"license": "MIT",
"dependencies": {
"bintrees": "1.0.2"
}
},
"node_modules/to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",

View File

@ -24,6 +24,7 @@
"express": "^4.21.2",
"http-status-codes": "^2.3.0",
"mariadb": "^3.4.0",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.2.2",
"sequelize": "^6.37.5",
"swagger-jsdoc": "^6.2.8",

View File

@ -1,15 +1,36 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
//import { LocationService } from "../services/locationService";
import { Counter, collectDefaultMetrics, register } from "prom-client";
//const locationService = container.resolve(LocationService);
const router = express.Router();
// Collect default system metrics (e.g., CPU, memory usage)
const prefix = 'locationhub_';
collectDefaultMetrics({ prefix });
// Define a custom Counter metric
const requestCounter = new Counter({
name: "http_requests_total",
help: "Total number of HTTP requests",
labelNames: ["method", "route", "status"], // Labels for filtering in Prometheus
});
// Define the metrics endpoint
router.get("/", async (req: Request, res: Response) => {
try {
console.log("Metric Endpoint triggered")
res.status(200).send();
console.log("Metric Endpoint triggered");
// Increment the counter with labels
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
// Expose metrics in Prometheus format
res.set("Content-Type", register.contentType);
res.send(await register.metrics());
} catch (error) {
// Increment the counter for errors
requestCounter.inc({ method: req.method, route: req.route.path, status: 500 });
console.error("Error running metrics endpoint:", error);
res.status(500).json({ error: "Error running metrics endpoint" });
}
});