Skip to main content

Strive Health Checks

A simple framework for running health checks. This library provides classes which serialize to Strive's preferred data contract, as well as a means of running checks asynchronously.

Output format

{
    "serviceName": "Your service",
    "checks": [
        {
            "checkName": "check one",
            "status": "ok"
        },
        {
            "checkName": "check two",
            "status": "unhealthy",
            "errorDetails": "Failed to connect."
        }
    ]
}

Creating health checks

A health check is configured by creating an instance of the HealthChecker class and providing a name for the check (string), a run function (see below), and if desired, a timeout in seconds (integer).

To invoke health checks, pass the name of the service and a list of health checkers to the run_checks function and await the result. The result will be a ServiceHealth instance, which contains the results from all checks along with a healthy flag (bool). If any check is unhealthy, the entire response will be considered unhealthy.

If a timeout is provided, the check will be considered unhealthy if it does not complete in less than the provided timeout value.

Run function

The run function is a function which takes the name of the check as string for the only argument, and returns a HealthCheckResult instance.

You can do anything you like inside the function prior to returning the result, eg: checking database connectivity, checking disk space, etc.

You do not need to worry about timeouts or exceptions; both can be handled by the run_checks function.

Example usage in a FastAPI service

from time import sleep

from fastapi import FastAPI, Response
from fastapi.params import Depends
from strivehealthchecks import HealthChecker, HealthCheckResult, create_health_check, create_health_check_with_timeout, run_checks

app = FastAPI()


def create_health_checks() -> list[HealthChecker]:
    return [
        check_one(),
        check_two(),
    ]


def check_one() -> HealthChecker:
    def run(name: str) -> HealthCheckResult:
        return HealthCheckResult.ok(name)

    return HealthChecker(name="one", run=run)


# this check will fail because it takes 3 seconds to return a successful result, but the timeout is 1 second
def check_two() -> HealthChecker:
    def slow_check(name: str) -> HealthCheckResult:
        sleep(3)
        return HealthCheckResult.ok(name)

    return HealthChecker(name="two", timeout_seconds=1, run=slow_check)


@app.get("/healthz")
async def health_handler(response: Response, checks: list[HealthChecker] = Depends(create_health_checks)):
    result = await run_checks("sample", checks)

    response.status_code = 200 if result.healthy else 500
    return result.to_dict()

Expected result from above example:

{
    "serviceName": "sample",
    "checks": [        
        {
            "checkName": "one",
            "status": "ok"
        },
        {
            "checkName": "two",
            "status": "unhealthy",
            "errorDetails": "did not respond after 1 seconds"
        }
    ]
}

Download files

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

Source Distribution

strivehealthchecks-1.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strivehealthchecks-1.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file strivehealthchecks-1.1.0.tar.gz.

File metadata

  • Download URL: strivehealthchecks-1.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for strivehealthchecks-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9d3ea01615a116e2fa26414464b58a153139ee294ac2c355e08615a7c414aa2d
MD5 e143f9e47f823805caeb0b540ec5cb21
BLAKE2b-256 eb127b793b5cca1d020084087127a246c1ac3f09fafc784706c0b4a798279445

See more details on using hashes here.

File details

Details for the file strivehealthchecks-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for strivehealthchecks-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32886c26776bf6081696f22926b14f58faaa48142e2408af203e940874503d14
MD5 53b381ea8ded96b47e5bae9d3a3a4ee8
BLAKE2b-256 1d22bcde1e913297cdad53a7b8a33c462e9a1503aed5c9c075f8890d13e5fed6

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page