Skip to main content

A module to have a FastAPI HealthCheck for database, RabbitMQ server, Redis or external URI to validate their healths.

Project description

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

Adding Health Checks

Here is what you need to get started.

from fastapi import FastAPI
from fastapi_healthz import (
    HealthCheckRegistry,
    HealthCheckRabbitMQ,
    HealthCheckRedis,
    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"))
# 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_healthz-0.2.5.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

fastapi_healthz-0.2.5-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_healthz-0.2.5.tar.gz.

File metadata

  • Download URL: fastapi_healthz-0.2.5.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-88-generic

File hashes

Hashes for fastapi_healthz-0.2.5.tar.gz
Algorithm Hash digest
SHA256 460764cd699f75c4fdb3d21f64d4a2a8ad77af06492f62384e8cc8a5f4ca68fa
MD5 ead5e5a760a2aca374259b2de6bf817a
BLAKE2b-256 16128a04b4306ff2acb01b02e728d703b4a9deaf28c8264d57b9928738252a34

See more details on using hashes here.

File details

Details for the file fastapi_healthz-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: fastapi_healthz-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-88-generic

File hashes

Hashes for fastapi_healthz-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4253bc3a2800237cb848a2f03d29612b40d09baa6a8e276298a3f30c2eb06272
MD5 973e3a6a1f3238e51d2c0c9fb2e84a9d
BLAKE2b-256 a8c754668b7b1a186b3f871da45571490dcbb8cff833fda18b44d6c96cb59e3b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page