Skip to main content

Base package to handle health checks with FastAPI.

Project description

fastapi_healthcheck

Easy to use health check for your FastAPI. This is the root module that will let you add implement and expand your usage of health checks, with FastAPI.

This module does not contain any service checkers, but you can easily add them. The other modules are not in this root module due to different dependencies required for each one. This is made so you only bring in the packages that you need to not add extra packages.

Install

pip install fastapi-healthcheck or poetry add fastapi-healthcheck

Adding Health Checks

Here is what you need to get started.

from fastapi import FastAPI
from fastapi_healthcheck import HealthCheckFactory, healthCheckRoute
from fastapi_healthcheck_sqlalchemy import HealthCheckSQLAlchemy

app = FastAPI()

# Add Health Checks
_healthChecks = HealthCheckFactory()

# SQLAlchemy comes from fastapi-healthcheck-sqlalchemy
_healthChecks.add(HealthCheckSQLAlchemy(alias='postgres db', connectionUri=cs.value, table=SmtpContactsSqlModel, tags=('postgres', 'db', 'sql01')))

# This will check external URI and validate the response that is returned.
# fastapi-healthcheck-uri
_healthChecks.add(HealthCheckUri(alias='reddit', connectionUri="https://www.reddit.com/r/aww.json", tags=('external', 'reddit', 'aww')))
app.add_api_route('/health', endpoint=healthCheckRoute(factory=_healthChecks))

Returned Data

When you request your health check, it will go and check all the entries that have been submitted and run a basic query against them. If they come back as expected, then a status code is 200. But if it runs into an error, it will return a 500 error.

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

Available Modules

If you have made a public service module and want to see it on this list, please open a new issue so we can add it to the list.

Writing a custom module

You can easily expand on this core module to add other health checks for other services. Generate a new service that pulls in HealthCheckInterface and HealthCheckBase. With those, you can build the respective class around the interface.

Once you have your service ready to go, add it to the HealthCheckFactory, and let the testing start.

If you would like to see an example of a custom service see fastapi_healthcheck_sqlalchemy. This will give you a better example of what you need to do to create your own module to interface with healthcheck.

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_healthcheck-0.2.13.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

fastapi_healthcheck-0.2.13-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_healthcheck-0.2.13.tar.gz.

File metadata

  • Download URL: fastapi_healthcheck-0.2.13.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1022-azure

File hashes

Hashes for fastapi_healthcheck-0.2.13.tar.gz
Algorithm Hash digest
SHA256 139080f2b4ad5d3400054b12e53afb43e97db34bc1d4676db956db2302f0bdde
MD5 18e0fbf26d12e0f4c3ffe9bea015fd64
BLAKE2b-256 1d9bab3448739829a5e1f6b1a71f35969f7182d9068ff36b345d413769df26a5

See more details on using hashes here.

File details

Details for the file fastapi_healthcheck-0.2.13-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_healthcheck-0.2.13-py3-none-any.whl
Algorithm Hash digest
SHA256 a58760d346a1be7990a6a3f1de9cf8eac07e8cff94b29a0de7d6da944e5bab64
MD5 1739be60c04f2701239f45ef4217b7e8
BLAKE2b-256 4c14eff8b7a35db9d798b9565543d65a7d5f231a2776d9d53a5155ab5b2ce9f4

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