Skip to main content

fastapi_healthz

Streamline your health checks with FastAPI using this user-friendly health check module. It serves as the foundational component for integrating and enhancing health checks within FastAPI.

This core module doesn't include any service checkers by default, but it offers a simple method for adding them. The reason for housing various modules separately is to accommodate the distinct dependencies required for each, ensuring that you only import the specific packages you need, thereby preventing any unnecessary package bloat.

This package is Pydantic v2 compliant.

Install

pip install fastapi-healthz or poetry add fastapi-healthz.

Eventually, you can partially install the package by running

pip install fastapi-healthz[redis]

pip install fastapi-healthz[rabbitmq]

pip install fastapi-healthz[database]

pip install fastapi-healthz[uri]

pip install fastapi-healthz[mongodb]

pip install fastapi-healthz[s3]

to install only the package you really need, with the required dependencies.

Adding Health Checks

Here is what you need to get started.

from fastapi import FastAPI
from fastapi_healthz import (
    HealthCheckRegistry,
    HealthCheckRabbitMQ,
    HealthCheckRedis,
    HealthCheckS3,
    HealthCheckUri,
    HealthCheckDatabase,
    health_check_route,
)

app = FastAPI()

# Add Health Checks
_healthChecks = HealthCheckRegistry()

# SQLAlchemy
_healthChecks.add(HealthCheckDatabase(uri="dialect+driver://username:password@host:port/database"))
# RabbitMQ
_healthChecks.add(HealthCheckRabbitMQ(host="localhost", port=5672, vhost="", username="username", password="pwd", ssl=True))
# Redis
_healthChecks.add(HealthCheckRedis(uri="redis://[password:]host:port/database"))
# S3
_healthChecks.add(HealthCheckS3(endpoint="http://localhost:9000", access_key="access_key", secret_key="secret_key"))
# This will check external URI and validate the response that is returned.
_healthChecks.add(HealthCheckUri(uri="https://www.reddit.com/r/aww.json"))

app.add_api_route('/health', endpoint=health_check_route(registry=_healthChecks))

Returned Data

When you initiate a health check request, it will examine all the submitted entries and execute a fundamental query on each of them. If the results conform to expectations, a status code of 200 is returned. However, if an error is encountered, a 500 error response will be provided.

{
    "status":"Healthy",
    "totalTimeTaken":"0:00:00.671642",
    "entities":[
        {
            "alias":"db",
            "status":"Healthy",
            "timeTaken":"0:00:00.009619",
            "tags":["db"]
        },
        {
            "alias":"reddit",
            "status":"Unhealthy",
            "timeTaken":"0:00:00.661716",
            "tags":["uri"]
        }
    ]
}

Writing a custom checker

You can effortlessly extend the functionality of this foundational module by incorporating additional health checks for various services. Simply create a new service that imports the HealthCheckAbstract. Then, build the corresponding class around this abstract.

Once your service is prepared, integrate it into the HealthCheckRegistry, and you're all set to commence testing.

Release files for fastapi-healthz 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-healthz 1.3.3
File Size Uploaded
fastapi_healthz-1.3.3.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-healthz 1.3.3
File Interpreter ABI Platform
fastapi_healthz-1.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.6 kB

Release files / fastapi_healthz-1.3.3.tar.gz

Download URL fastapi_healthz-1.3.3.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4590de0ee627424009d209b9190928f8f1af3bf3dd2b82372c2dfbf569d2ae64
BLAKE2b-256 checksum
How to use checksums
4bced624155fdca5a95a5c8c2dde9f779b1fe3696b531822441b1faada3cf315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.10 Linux/6.8.0-90-generic

Release files / fastapi_healthz-1.3.3-py3-none-any.whl

Download URL fastapi_healthz-1.3.3-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d37b6dbff1c10cc2360a11ab815f7fd8aaf7a84c2075732bdc3e798f62634fc
BLAKE2b-256 checksum
How to use checksums
ffaa96f86148fd30fc914694a5cae530af7bfea6c2332b35433ff2923f4290fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.10 Linux/6.8.0-90-generic

Release history Release notifications | RSS feed

This release

1.3.3 This release

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page