Skip to content

Framework agnostic health checks with integrations for most popular ASGI frameworks: FastAPI, Faststream, Litestar

License

Notifications You must be signed in to change notification settings

shepilov-vladislav/fast-healthchecks

Repository files navigation

FastHealthcheck

Framework agnostic health checks with integrations for most popular ASGI frameworks: FastAPI / Faststream / Litestar to help you to implement the Health Check API pattern


Test Passing Coverage Package version Supported Python versions License


Installation

With pip:

pip install fast-healthcheck

With poetry:

poetry add fast-healthcheck

With uv:

uv add fast-healthcheck

Quick Start

Examples:

import asyncio
import os
import time

from fastapi import FastAPI

from fast_healthchecks.checks.function import FunctionHealthCheck
from fast_healthchecks.checks.kafka import KafkaHealthCheck
from fast_healthchecks.checks.mongo import MongoHealthCheck
from fast_healthchecks.checks.postgresql.asyncpg import PostgreSQLAsyncPGHealthCheck
from fast_healthchecks.checks.postgresql.psycopg import PostgreSQLPsycopgHealthCheck
from fast_healthchecks.checks.rabbitmq import RabbitMQHealthCheck
from fast_healthchecks.checks.redis import RedisHealthCheck
from fast_healthchecks.checks.url import UrlHealthCheck
from fast_healthchecks.integrations.fastapi import HealthcheckRouter, Probe


def sync_dummy_check() -> bool:
    time.sleep(0.1)
    return True


async def async_dummy_check() -> bool:
    await asyncio.sleep(0.1)
    return True


app = FastAPI()
app.include_router(
    HealthcheckRouter(
        Probe(
            name="liveness",
            checks=[
                FunctionHealthCheck(func=sync_dummy_check, name="Sync dummy"),
            ],
        ),
        Probe(
            name="readiness",
            checks=[
                KafkaHealthCheck(
                    bootstrap_servers=os.environ["KAFKA_BOOTSTRAP_SERVERS"],
                    name="Kafka",
                ),
                MongoHealthCheck.from_dsn(os.environ["MONGO_DSN"], name="Mongo"),
                PostgreSQLAsyncPGHealthCheck.from_dsn(os.environ["POSTGRES_DSN"], name="PostgreSQL asyncpg"),
                PostgreSQLPsycopgHealthCheck.from_dsn(os.environ["POSTGRES_DSN"], name="PostgreSQL psycopg"),
                RabbitMQHealthCheck.from_dsn(os.environ["RABBITMQ_DSN"], name="RabbitMQ"),
                RedisHealthCheck.from_dsn(os.environ["REDIS_DSN"], name="Redis"),
                UrlHealthCheck(url="https://httpbin.org/status/200", name="URL 200"),
            ],
        ),
        Probe(
            name="startup",
            checks=[
                FunctionHealthCheck(func=async_dummy_check, name="Async dummy"),
            ],
        ),
        debug=True,
        prefix="/health",
    ),
)

Development

Setup environment

git clone https://github.com/shepilov-vladislav/fast-healthchecks.git
cd fast-healthchecks
uv sync --group=dev --group=docs --all-extras

Run linters

make lint

Run all tests

make tests-all

Serve documentation

make serve-docs

Known alternatives

License

This project is licensed under the terms of the MIT license.