From 71c93ebfb5b54feee83c5ae221db5639978c039c Mon Sep 17 00:00:00 2001 From: Timo Notheisen <65653426+tnotheis@users.noreply.github.com> Date: Thu, 13 Feb 2025 14:34:15 +0100 Subject: [PATCH] Health check returns HTTP status 200 even when unhealthy (#371) * fix: return 500 if health check result is unhealthy * fix: allow status 500 for a successful health check result in sdk --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- packages/sdk/src/endpoints/Endpoint.ts | 4 ++-- packages/sdk/src/endpoints/MonitoringEndpoint.ts | 2 +- src/infrastructure/httpServer/HttpServer.ts | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/sdk/src/endpoints/Endpoint.ts b/packages/sdk/src/endpoints/Endpoint.ts index 0df0dbd1..a5ff0cd6 100644 --- a/packages/sdk/src/endpoints/Endpoint.ts +++ b/packages/sdk/src/endpoints/Endpoint.ts @@ -5,8 +5,8 @@ import { ConnectorHttpResponse } from "../types/ConnectorHttpResponse"; export abstract class Endpoint { public constructor(private readonly httpClient: AxiosInstance) {} - protected async getPlain(path: string): Promise { - const reponse = await this.httpClient.get(path, { validateStatus: (status) => status === 200 }); + protected async getPlain(path: string, validateStatus?: (status: number) => boolean): Promise { + const reponse = await this.httpClient.get(path, { validateStatus: validateStatus ?? ((status) => status === 200) }); return reponse.data; } diff --git a/packages/sdk/src/endpoints/MonitoringEndpoint.ts b/packages/sdk/src/endpoints/MonitoringEndpoint.ts index c1296248..c7afa828 100644 --- a/packages/sdk/src/endpoints/MonitoringEndpoint.ts +++ b/packages/sdk/src/endpoints/MonitoringEndpoint.ts @@ -3,7 +3,7 @@ import { Endpoint } from "./Endpoint"; export class MonitoringEndpoint extends Endpoint { public async getHealth(): Promise { - return await this.getPlain("/health"); + return await this.getPlain("/health", (status) => status === 200 || status === 500); } public async getVersion(): Promise { diff --git a/src/infrastructure/httpServer/HttpServer.ts b/src/infrastructure/httpServer/HttpServer.ts index 469ed938..1dd50e25 100644 --- a/src/infrastructure/httpServer/HttpServer.ts +++ b/src/infrastructure/httpServer/HttpServer.ts @@ -222,7 +222,8 @@ export class HttpServer extends ConnectorInfrastructure private useHealthEndpoint() { this.app.get("/health", async (_req: any, res: any) => { const health = await this.runtime.getHealth(); - res.status(200).json(health); + const httpStatus = health.isHealthy ? 200 : 500; + res.status(httpStatus).json(health); }); }