From e6352f11cd9e33f82b5ebf53deeed7bf0ec0852c Mon Sep 17 00:00:00 2001 From: marudor Date: Sun, 15 Dec 2024 21:54:02 +0100 Subject: [PATCH] fix: error metrics --- src/server/admin/index.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/server/admin/index.ts b/src/server/admin/index.ts index b1199f520..448f21295 100644 --- a/src/server/admin/index.ts +++ b/src/server/admin/index.ts @@ -1,8 +1,9 @@ import type { Server } from 'node:http'; -import type { - AxiosInstance, - AxiosResponse, - InternalAxiosRequestConfig, +import { + type AxiosInstance, + type AxiosResponse, + type InternalAxiosRequestConfig, + isAxiosError, } from 'axios'; import Koa from 'koa'; import PromClient, { Counter, Histogram } from 'prom-client'; @@ -44,6 +45,13 @@ function upstreamResponseApiCountInterceptor( return res; } +function upstreamErrorResponseApiCountInterceptor(apiName: string, error: any) { + if (isAxiosError(error) && error.status) { + UpstreamApiResponseMetric.inc({ api: apiName, code: error.status }); + } + return error; +} + export function axiosUpstreamInterceptor( axios: AxiosInstance, apiName: string, @@ -53,6 +61,7 @@ export function axiosUpstreamInterceptor( ); axios.interceptors.response.use( upstreamResponseApiCountInterceptor.bind(undefined, apiName), + upstreamErrorResponseApiCountInterceptor.bind(undefined, apiName), ); }