Skip to content

Commit

Permalink
fix: error metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
marudor committed Dec 15, 2024
1 parent cb9e1c2 commit e6352f1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/server/admin/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -53,6 +61,7 @@ export function axiosUpstreamInterceptor(
);
axios.interceptors.response.use(
upstreamResponseApiCountInterceptor.bind(undefined, apiName),
upstreamErrorResponseApiCountInterceptor.bind(undefined, apiName),
);
}

Expand Down

0 comments on commit e6352f1

Please sign in to comment.