mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-18 03:26:07 +08:00
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:
parent
2ba52eb3be
commit
4c396717ef
2 changed files with 20 additions and 1 deletions
|
@ -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,
|
||||
|
|
12
backend/utils/prometheus.ts
Normal file
12
backend/utils/prometheus.ts
Normal 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 });
|
||||
}
|
Loading…
Add table
Reference in a new issue