From 2e8c58365fe64592eedc12f0ad9ae11b8e66ce36 Mon Sep 17 00:00:00 2001 From: Miodec Date: Tue, 25 Jul 2023 15:46:46 +0200 Subject: [PATCH] sending client version in headers logging which client versions are getting errors closes #4475 --- backend/src/middlewares/error.ts | 5 +++++ backend/src/utils/prometheus.ts | 10 ++++++++++ frontend/src/ts/ape/adapters/axios-adapter.ts | 2 ++ 3 files changed, 17 insertions(+) diff --git a/backend/src/middlewares/error.ts b/backend/src/middlewares/error.ts index 85cef8165..5b93d7c97 100644 --- a/backend/src/middlewares/error.ts +++ b/backend/src/middlewares/error.ts @@ -5,6 +5,7 @@ import MonkeyError from "../utils/error"; import { incrementBadAuth } from "./rate-limit"; import { NextFunction, Response } from "express"; import { MonkeyResponse, handleMonkeyResponse } from "../utils/monkey-response"; +import { recordClientErrorByVersion } from "../utils/prometheus"; async function errorHandlingMiddleware( error: Error, @@ -37,6 +38,10 @@ async function errorHandlingMiddleware( await incrementBadAuth(req, res, monkeyResponse.status); + if (monkeyResponse.status >= 400 && monkeyResponse.status < 500) { + recordClientErrorByVersion(req.headers["x-client-version"] as string); + } + if (process.env.MODE !== "dev" && monkeyResponse.status >= 500) { const { uid, errorId } = monkeyResponse.data; diff --git a/backend/src/utils/prometheus.ts b/backend/src/utils/prometheus.ts index a2f516855..afee67b2d 100644 --- a/backend/src/utils/prometheus.ts +++ b/backend/src/utils/prometheus.ts @@ -179,6 +179,16 @@ export function recordServerVersion(serverVersion: string): void { serverVersionCounter.inc({ version: serverVersion }); } +const clientErrorByVersion = new Counter({ + name: "api_client_error_by_version", + help: "Client versions which are experiencing 400 errors", + labelNames: ["version"], +}); + +export function recordClientErrorByVersion(version: string): void { + clientErrorByVersion.inc({ version }); +} + const authTime = new Histogram({ name: "api_request_auth_time", help: "Time spent authenticating", diff --git a/frontend/src/ts/ape/adapters/axios-adapter.ts b/frontend/src/ts/ape/adapters/axios-adapter.ts index c5837013b..8f2e7ed5d 100644 --- a/frontend/src/ts/ape/adapters/axios-adapter.ts +++ b/frontend/src/ts/ape/adapters/axios-adapter.ts @@ -1,6 +1,7 @@ import { Auth } from "../../firebase"; import { getIdToken } from "firebase/auth"; import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; +import { CLIENT_VERSION } from "../../version"; type AxiosClientMethod = ( endpoint: string, @@ -27,6 +28,7 @@ async function adaptRequestOptions( Accept: "application/json", "Content-Type": "application/json", ...(idToken && { Authorization: `Bearer ${idToken}` }), + "X-Client-Version": CLIENT_VERSION, }, }; }