added country log test

This commit is contained in:
Miodec 2022-09-23 20:02:29 +02:00
parent 326adb11a0
commit 0fff40f912
2 changed files with 31 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import swaggerStats from "swagger-stats";
import swaggerUi from "swagger-ui-express";
import publicSwaggerSpec from "../../documentation/public-swagger.json";
import internalSwaggerSpec from "../../documentation/internal-swagger.json";
import { recordRequestCountry } from "../../utils/prometheus";
const SWAGGER_UI_OPTIONS = {
customCss: ".swagger-ui .topbar { display: none } .try-out { display: none }",
@ -32,6 +33,14 @@ function addSwaggerMiddlewares(app: Application): void {
swaggerUi.serve,
swaggerUi.setup(publicSwaggerSpec, SWAGGER_UI_OPTIONS)
);
app.use((req, res, next) => {
const country = req.headers["cf-ipcountry"] as string;
if (country) {
recordRequestCountry(country, req as MonkeyTypes.Request);
}
next();
});
}
export default addSwaggerMiddlewares;

View file

@ -205,3 +205,25 @@ export function recordAuthTime(
authTime.observe({ type, status, path: pathNoGet }, time);
}
const requestCountry = new Counter({
name: "api_request_country",
help: "Country of request",
labelNames: ["path", "country"],
});
export function recordRequestCountry(
country: string,
req: MonkeyTypes.Request
): void {
const reqPath = req.baseUrl + req.route.path;
let normalizedPath = "/";
if (reqPath !== "/") {
normalizedPath = reqPath.endsWith("/") ? reqPath.slice(0, -1) : reqPath;
}
const pathNoGet = normalizedPath.replace(/\?.*/, "");
requestCountry.inc({ path: pathNoGet, country });
}