Health
grelmicro.health
Health Check Registry.
HealthCheckFunc
module-attribute
HealthCheckFunc: TypeAlias = (
SyncHealthCheckFunc | AsyncHealthCheckFunc
)
Any callable acceptable as a health check.
Returns:
- None: healthy, no details.
- HealthDetails: healthy, with a details dict.
Raises:
- HealthError: unhealthy. The message surfaces in the response.
- Any other exception: unhealthy with a generic message. The
traceback is logged server-side.
HealthDetails
module-attribute
HealthDetails: TypeAlias = dict[str, JSONEncodable]
Per-check details payload. JSON-serializable dict keyed by string.
CheckResult
Bases: TypedDict
Result of a single health check.
critical
instance-attribute
critical: bool
error
instance-attribute
error: str | None
HealthError
HealthError(
message: str, *, details: HealthDetails | None = None
)
Bases: GrelmicroError
Signal a check failure. The message is exposed in the response.
Pass details to include a diagnostic payload alongside the
error, visible under details on the check entry in
/healthz (subject to show_details).
Initialize with a message and optional details dict.
details
instance-attribute
details = details
HealthRegistry
HealthRegistry(
*,
timeout: PositiveFloat | None = None,
cache_ttl: NonNegativeFloat | None = None,
env_prefix: str | None = None,
read_env: bool = True,
)
Registry that manages health checks and runs them concurrently.
Checks are plain async functions. Register them with the
:meth:check decorator or the :meth:add method. All registered
checks are executed in parallel via an anyio task group. Each
check has its own timeout (falling back to the registry default)
and its own cached result. Concurrent requests for the same check
share a single execution via an anyio.Event.
Initialize the health registry.
| PARAMETER | DESCRIPTION |
|---|---|
timeout
|
Default per-check timeout in seconds. Checks that
exceed this duration are reported as Default: 5.0. When unset, resolves from the
environment variable
TYPE:
|
cache_ttl
|
Per-check cache TTL in seconds. Set to 0 to disable. Default: 1.0. When unset, resolves from the
environment variable
TYPE:
|
env_prefix
|
Override the auto-derived environment variable prefix. Default:
TYPE:
|
read_env
|
Whether to read environment variables. Default: True. Set to False when every field is already supplied via kwargs and the environment must not influence construction.
TYPE:
|
from_config
classmethod
from_config(config: HealthRegistryConfig) -> Self
Construct a HealthRegistry from a pre-built HealthRegistryConfig.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
The pre-built health registry configuration. Use this path when the configuration is assembled at
startup from a settings tree (for example YAML, Vault,
or a
TYPE:
|
add
add(
name: str,
func: HealthCheckFunc,
*,
critical: bool = True,
timeout: PositiveFloat | None = None,
) -> None
Register a health check function.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique name identifying this check.
TYPE:
|
func
|
Async function: returns
TYPE:
|
critical
|
Whether this check affects the aggregate status and HTTP response code. Critical failures flip the aggregate to
TYPE:
|
timeout
|
Per-check timeout override. Falls back to the registry default when omitted.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
check
check(
name: str,
*,
critical: bool = True,
timeout: PositiveFloat | None = None,
) -> Callable[[HealthCheckFunc], HealthCheckFunc]
Decorate an async function to register it as a health check.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique name identifying this check.
TYPE:
|
critical
|
Whether this check affects the aggregate status.
TYPE:
|
timeout
|
Per-check timeout override.
TYPE:
|
Example
@registry.check("database") ... async def check_db() -> dict | None: ... return None
run
async
run(
*,
critical_only: bool = False,
exclude: Iterable[str] | None = None,
) -> HealthReport
Run the selected checks concurrently and aggregate.
Each check runs with its own timeout. Results are cached per
check for cache_ttl seconds. Concurrent calls for the
same check coalesce via single-flight.
| PARAMETER | DESCRIPTION |
|---|---|
critical_only
|
If True, only run critical checks.
TYPE:
|
exclude
|
Check names to skip.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HealthReport
|
A HealthReport with the aggregate status and per-check |
HealthReport
|
results. |
HealthRegistryConfig
Bases: BaseModel
Health Registry Config.
timeout
class-attribute
instance-attribute
timeout: PositiveFloat = 5.0
Default per-check timeout in seconds. Checks that exceed this duration are reported as error. Can be overridden per check on registration.
cache_ttl
class-attribute
instance-attribute
cache_ttl: NonNegativeFloat = 1.0
Per-check cache TTL in seconds. Each check's last result is reused until it is older than cache_ttl. Concurrent calls coalesce via single-flight. Set to 0 to disable caching.
HealthReport
Bases: TypedDict
Aggregated health report across all registered checks.
HealthStatus
Bases: StrEnum
Binary health status for a component or aggregate report.
OK: the check passed. At the aggregate level: every critical check passed (non-critical failures do not flip the aggregate).ERROR: the check failed. At the aggregate level: at least one critical check failed.
OK
class-attribute
instance-attribute
OK = 'ok'
ERROR
class-attribute
instance-attribute
ERROR = 'error'
get_health_registry
get_health_registry(
name: str = DEFAULT_NAME,
) -> HealthRegistry
Resolve a health registry by name.
| RAISES | DESCRIPTION |
|---|---|
BackendNotLoadedError
|
If no registry resolves. |
grelmicro.health.fastapi
FastAPI Health Check Router.
health_router
health_router(
registry: HealthRegistry | None = None,
*,
prefix: str = "",
show_details: bool | Depends = False,
healthz_dependencies: list[Depends] | None = None,
) -> APIRouter
Create a FastAPI router with health check endpoints.
Provides three endpoints:
GET/HEAD {prefix}/livez: Liveness probe. Never runs checkers. Always returns200with an empty body.GET/HEAD {prefix}/readyz: Readiness probe. Runs critical checkers only. Returns200or503with an empty body.GET/HEAD {prefix}/healthz: Aggregate JSON report.
All responses set Cache-Control: no-store.
| PARAMETER | DESCRIPTION |
|---|---|
registry
|
Health registry whose checks the router runs. When omitted, the router resolves the global registry (the registry installed via
TYPE:
|
prefix
|
URL prefix for health endpoints (e.g. '/api/v1').
TYPE:
|
show_details
|
Whether
TYPE:
|
healthz_dependencies
|
FastAPI dependencies applied to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
DependencyNotFoundError
|
If |
TypeError
|
If |