Skip to main content

Python Healthcheck Lib

This is an opinionated library to help with structuring healthchecks in your python application.

It will not help you with the healthcheck logic itself, but it will help you build the response in a standard format.

We aim at supporting almost exactly the standard described in https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check. Although it's just a expired draft, it seems to be the better standard on healthchecks out there as of this writing.

This standard is not fully supported yet, see the roadmap for more information.

Installation

# With pip
pip install python-healthchecklib

# With poetry
poetry add python-healthchecklib

Concepts

The main concepts to keep in mind when using this library are the service and components concepts.

We consider a service to be the application as a whole. It's the thing that is running and that you want to check the health of.

We consider components to be the different parts of the application that you want to check the health of, being them internal or external. For example, if you have a web application, you might want to check the health of the database, the cache, internal components, etc.

You'll be responsible for implementing the healthcheck logic for each component, but this library will help you build the response in a standard format and define the health of the service based on the health of its components.

Usage

The lib exposes asynchronous methods, which makes it easy to use with either sync or async codebases.

from healthcheck import Healthcheck, HealthcheckDatastoreComponent, HealthcheckCallbackResponse

# Create a healthcheck instance
healthcheck = Healthcheck(
    name="My Service"
)

# Create a component
db_component = HealthcheckDatastoreComponent(
    name="MySQL",
)

# Define the healthcheck logic for the component.
# They should be async functions that return a HealthcheckCallbackResponse
async def db_healthcheck():
    # Implement some logic to check the health of the database
    return HealthcheckCallbackResponse(
        status="pass",
        output="Database is healthy",
        affects_service_health=True
    )

# Add the healthcheck logic to the component
db_component.add_healthcheck(db_healthcheck)

# You can add more than one healthcheck to a component, which means that this is a component made of multiple instances.
async def db_healthcheck_2():
    # Implement some logic to check the health of the database
    return HealthcheckCallbackResponse(
        status="warn",
        output="Responsive but high latency",
        affects_service_health=False
    )

db_component.add_healthcheck(db_healthcheck_2)

# Add the component to the healthcheck
healthcheck.add_component(db_component)

# Get the health status of the service
status = await healthcheck.run()

# In case you're in a sync context, you can use `asyncio.run` to run the async code:
status = asyncio.run(healthcheck.run())

# Print the status
print(status)

# {
#   "status": "pass",
#   "description": "Health status of My Service",
#   "checks": {
#     "MySQL": [
#       {
#         "status": "pass",
#         "output": "Database is healthy",
#         "componentType": "datastore",
#         "componentName": "MySQL
#       },
#       {
#         "status": "warn",
#         "output": "Responsive but high latency",
#         "componentType": "datastore",
#         "componentName": "MySQL
#       }
#     ]
#   }
# }

Roadmap

We welcome contributions to help us achieve these goals. See below how to contribute.

Contributing

See the contributing guide for more information.

License

By contributing to python-healthchecklib, you agree that your contributions will be licensed under the MIT License.

Metadata

Release files for python-healthchecklib 0.1.1

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

Source distribution (sdist)

Source distribution for python-healthchecklib 0.1.1
File Size Uploaded
python_healthchecklib-0.1.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-healthchecklib 0.1.1
File Interpreter ABI Platform
python_healthchecklib-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / python_healthchecklib-0.1.1.tar.gz

Download URL python_healthchecklib-0.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bac6cdd9ef5825f6deb0cbe5f6d97260f3f402e111fc7fe2146444bdb77fd892
BLAKE2b-256 checksum
How to use checksums
bcceb7e2c780b8f7b87a3632c572a0cc4a67c8a0ac0a6ccc57198ef8fe87b6d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.5 Darwin/23.1.0

Release files / python_healthchecklib-0.1.1-py3-none-any.whl

Download URL python_healthchecklib-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51ad9e7e782145977bf322cbe2095198a8b61473b09d43e79018e47483840d15
BLAKE2b-256 checksum
How to use checksums
a37dbde432ce2bfd4559ed46963eeb8331125ef4c2ea3f3a1dbf0691cdf029da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.5 Darwin/23.1.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

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