mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-31 07:04:22 +00:00
Monitoring: tag HTTP response rate histogram by route rather than request url
This commit is contained in:
@@ -16,9 +16,10 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import * as client from "prom-client";
|
||||
import { Router } from "express";
|
||||
import http, { IncomingMessage, ServerResponse } from "node:http";
|
||||
import * as client from "prom-client";
|
||||
import { Application, Router } from "express";
|
||||
import { sleep } from "@spacebar/util";
|
||||
|
||||
export class Monitoring {
|
||||
static isInitialised = false;
|
||||
@@ -29,8 +30,38 @@ export class Monitoring {
|
||||
Monitoring.isInitialised = true;
|
||||
}
|
||||
|
||||
public static attach(router: Router) {
|
||||
router.get("/metrics", async (req, res) => {
|
||||
public static attach(app: Application) {
|
||||
const a = app;
|
||||
const http_request_total = new client.Counter({
|
||||
name: "node_http_request_total",
|
||||
help: "The total number of HTTP requests received",
|
||||
labelNames: ["path", "method", "status_code"],
|
||||
});
|
||||
client.register.registerMetric(http_request_total);
|
||||
|
||||
const http_response_rate_histogram = new client.Histogram({
|
||||
name: "node_http_duration",
|
||||
labelNames: ["path", "method", "status_code"],
|
||||
help: "The duration of HTTP requests in seconds",
|
||||
buckets: [0.0, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 10],
|
||||
});
|
||||
client.register.registerMetric(http_response_rate_histogram);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const endTimer = http_response_rate_histogram.startTimer();
|
||||
res.on("finish", () => {
|
||||
const r = req;
|
||||
const path = (res.locals.lambertRouteBase ?? req.baseUrl ?? "") + req.route?.path;
|
||||
if (!req.route?.path) {
|
||||
console.log(req);
|
||||
}
|
||||
endTimer({ method: req.method, path, status_code: res.statusCode });
|
||||
http_request_total.inc({ method: req.method, path, status_code: res.statusCode });
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
app.get("/metrics", async (req, res) => {
|
||||
res.setHeader("Content-Type", client.register.contentType);
|
||||
const metrics = await client.register.metrics();
|
||||
res.send(metrics);
|
||||
|
||||
@@ -54,7 +54,15 @@ export class Server {
|
||||
if (router.default) router = router.default;
|
||||
if (!router || router?.prototype?.constructor?.name !== "router") throw `File doesn't export any default router`;
|
||||
|
||||
this.app.use(path, <Router>router);
|
||||
this.app.use(
|
||||
path,
|
||||
// TODO: I wish this middleware wasn't nessecary to preserve base path param names for monitoring...
|
||||
(_, res, next) => {
|
||||
res.locals.lambertRouteBase = path;
|
||||
next();
|
||||
},
|
||||
<Router>router,
|
||||
);
|
||||
|
||||
if (this.options.serverInitLogging && process.env.LOG_ROUTES !== "false") console.log(`[Server] Route ${path} registered`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user