sending different path to prometheus

This commit is contained in:
Miodec 2022-07-02 17:39:48 +02:00
parent 9acd68e99f
commit d14a582cb8
3 changed files with 21 additions and 8 deletions

View file

@ -21,6 +21,10 @@ describe("middlewares/auth", () => {
beforeEach(async () => {
mockRequest = {
baseUrl: "/api/v1",
route: {
path: "/",
},
headers: {
authorization: "Bearer 123456789",
},

View file

@ -71,18 +71,17 @@ function authenticateRequest(authOptions = DEFAULT_OPTIONS): Handler {
recordAuthTime(
authType,
"failure",
req.originalUrl,
Math.round(performance.now() - startTime)
Math.round(performance.now() - startTime),
req
);
return next(error);
}
recordAuthTime(
token.type,
"success",
req.originalUrl,
Math.round(performance.now() - startTime)
Math.round(performance.now() - startTime),
req
);
next();

View file

@ -191,9 +191,19 @@ const authTime = new Histogram({
export function recordAuthTime(
type: string,
status: "success" | "failure",
path: string,
time: number
time: number,
req: MonkeyTypes.Request
): void {
const pathNoGet = path?.replace(/\?.*/, "");
const reqPath = req.baseUrl + req.route.path;
let normalizedPath = "/";
if (reqPath !== "/") {
normalizedPath = reqPath.endsWith("/") ? reqPath.slice(0, -1) : reqPath;
}
const pathNoGet = normalizedPath.replace(/\?.*/, "");
console.log(pathNoGet);
authTime.observe({ type, status, path: pathNoGet }, time);
}