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"
}
]
}
Metadata
Release files for strivehealthchecks 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| strivehealthchecks-1.1.0.tar.gz | 7.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| strivehealthchecks-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.1 kB
Release files / strivehealthchecks-1.1.0.tar.gz
| Download URL | strivehealthchecks-1.1.0.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d3ea01615a116e2fa26414464b58a153139ee294ac2c355e08615a7c414aa2d
|
|
BLAKE2b-256 checksum How to use checksums |
eb127b793b5cca1d020084087127a246c1ac3f09fafc784706c0b4a798279445
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / strivehealthchecks-1.1.0-py3-none-any.whl
| Download URL | strivehealthchecks-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
32886c26776bf6081696f22926b14f58faaa48142e2408af203e940874503d14
|
|
BLAKE2b-256 checksum How to use checksums |
1d22bcde1e913297cdad53a7b8a33c462e9a1503aed5c9c075f8890d13e5fed6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for strivehealthchecks 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.