2222from collections .abc import Mapping
2323from typing import Any , Protocol
2424
25+ from src .writers .writer import HealthCheckError
26+
2527logger = logging .getLogger (__name__ )
2628
2729
2830class HealthCheckable (Protocol ):
2931 """Protocol for dependencies that support health checks."""
3032
31- def check_health (self ) -> tuple [ bool , str ] :
33+ def check_health (self ) -> str | None :
3234 """Check dependency health.
3335 Returns:
34- Tuple of (is_healthy, message).
36+ `None` when healthy, a descriptive string when intentionally disabled.
37+ Raises:
38+ HealthCheckError: If the dependency is unhealthy.
3539 """
3640
3741
@@ -51,11 +55,15 @@ def get_health(self) -> dict[str, Any]:
5155 logger .debug ("Handling GET Health." )
5256
5357 failures : dict [str , str ] = {}
58+ statuses : dict [str , str ] = {}
5459
5560 for name , dependency in self .dependencies .items ():
56- healthy , msg = dependency .check_health ()
57- if not healthy :
58- failures [name ] = msg
61+ try :
62+ result = dependency .check_health ()
63+ statuses [name ] = result if result else "ok"
64+ except HealthCheckError as exc :
65+ failures [name ] = str (exc )
66+ statuses [name ] = str (exc )
5967
6068 uptime_seconds = int ((datetime .now (timezone .utc ) - self .start_time ).total_seconds ())
6169
@@ -64,12 +72,12 @@ def get_health(self) -> dict[str, Any]:
6472 return {
6573 "statusCode" : 200 ,
6674 "headers" : {"Content-Type" : "application/json" },
67- "body" : json .dumps ({"status" : "ok" , "uptime_seconds" : uptime_seconds }),
75+ "body" : json .dumps ({"status" : "ok" , "uptime_seconds" : uptime_seconds , "dependencies" : statuses }),
6876 }
6977
7078 logger .debug ("Health check degraded: %s." , failures )
7179 return {
7280 "statusCode" : 503 ,
7381 "headers" : {"Content-Type" : "application/json" },
74- "body" : json .dumps ({"status" : "degraded" , "failures" : failures }),
82+ "body" : json .dumps ({"status" : "degraded" , "failures" : failures , "dependencies" : statuses }),
7583 }
0 commit comments