Added a custom auth counter to prometheus metrics (#2690)

* added basic auth and prom client dependencies

* incrementing auth counter

* added custom prometheus metric

* turns out there is no need for basic auth

* updated name

* exact

* exact
This commit is contained in:
Jack 2022-03-12 15:20:18 +01:00 committed by GitHub
parent 2ba52eb3be
commit 4c396717ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import { verifyIdToken } from "../utils/auth";
import { base64UrlDecode } from "../utils/misc";
import { NextFunction, Response, Handler } from "express";
import statuses from "../constants/monkey-status-codes";
import { incrementAuth } from "../utils/prometheus";
interface RequestAuthenticationOptions {
isPublic?: boolean;
@ -38,7 +39,11 @@ function authenticateRequest(authOptions = DEFAULT_OPTIONS): Handler {
options
);
} else if (options.isPublic) {
return next();
token = {
type: "None",
uid: "",
email: "",
};
} else if (process.env.MODE === "dev") {
token = authenticateWithBody(req.body);
} else {
@ -49,6 +54,8 @@ function authenticateRequest(authOptions = DEFAULT_OPTIONS): Handler {
);
}
incrementAuth(token.type);
req.ctx = {
...req.ctx,
decodedToken: token,

View file

@ -0,0 +1,12 @@
import "dotenv/config";
import { Counter } from "prom-client";
const auth = new Counter({
name: "api_request_auth_total",
help: "Counts authentication events",
labelNames: ["type"],
});
export function incrementAuth(type: "Bearer" | "ApeKey" | "None"): void {
auth.inc({ type });
}