Skip to content

healthcheck

Add a health check.

EndpointFilter

Bases: Filter

Filter pings to /health.

https://stackoverflow.com/a/70810102

Source code in src/red_utils/ext/fastapi_utils/healthcheck.py
class EndpointFilter(logging.Filter):
    """Filter pings to /health.

    https://stackoverflow.com/a/70810102
    """

    def filter(self, record: logging.LogRecord) -> bool:
        return record.args and len(record.args) >= 3 and record.args[2] != "/health"

healthy() async

Respond to healthchecks.

Endpoint does not take any parameters or request bodies.

Response: a 200 'OK', with a response body containing 'healthy'.

Source code in src/red_utils/ext/fastapi_utils/healthcheck.py
@router.get("/health", summary="Healthcheck")
async def healthy() -> JSONResponse:
    """Respond to healthchecks.

    Endpoint does not take any parameters or request bodies.

    Response: a 200 'OK', with a response body containing 'healthy'.
    """
    health: dict = jsonable_encoder({"healthy": True})
    response = JSONResponse(
        status_code=status.HTTP_200_OK, headers={"X-HEALTHY": "true"}, content=health
    )
    return response