A library for fastapi health checks
Project description
sag_py_fastapi_health
Add health check endpoints to fastapi (similar to the ones dotnet core has)
What it does
- Adds one or multiple health endpoint
- Configurable output format (json or prtg)
- Possibility to add checks (own and pre shipped)
- Pre-Shipped tests for http get requests including basic auth and directory existence/readability/writability
Installation
pip install sag-py-fastapi-health
How to use
Sample usage with existing checks
from sag_py_fastapi_health.checks.http import HttpCheck
from sag_py_fastapi_health.checks.storage import StorageExistsCheck, StorageReadableCheck
from sag_py_fastapi_health.formatter import DefaultResponseFormatter, PrtgResponseFormatter
from sag_py_fastapi_health.models import Probe
from sag_py_fastapi_health.router import HealthcheckRouter
from config import config
router = HealthcheckRouter(
Probe(
name="health",
response_formatter=DefaultResponseFormatter(),
checks=[
StorageExistsCheck("/opt/app/data", name="my_dir_exists"),
StorageReadableCheck("/opt/app/data", name="my_dir_is_readable"),
HttpCheck("https://localhost/auth", name="auth_available", timeout=5),
],
),
Probe(
name="health-prtg",
response_formatter=PrtgResponseFormatter(),
checks=[
StorageExistsCheck("/opt/app/data", name="my_dir_exists"),
StorageReadableCheck("/opt/app/data", name="my_dir_is_readable"),
HttpCheck("https://localhost/auth", name="auth_available", timeout=5),
],
),
)
Write your own check
from sag_py_fastapi_health.models import CheckResult
class TestCheck(Check):
def __init__(self, name: str = "check") -> None:
self._name: str = name
async def __call__(self) -> CheckResult:
is_healthy: bool = a_custom_check()
description: str = "A description of the status or a error message"
return CheckResult(
name=self._name,
status="Healthy" if is_healthy else "Unhealthy",
description=description,
)
The description contains something like "Directory ... was accessable" or "Service is running" if everything is ok. If there was an error, you can add the error message/exception message there.
How to publish
- Update the version in setup.py and commit your change
- Create a tag with the same version number
- Let github do the rest
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
Built Distribution
Close
Hashes for sag-py-fastapi-health-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4023909f5b329caa763e826ab71e3941874fa2f076c3ee575078d995f1fb5edc |
|
MD5 | 71ee109f4d0389c1ffc511607b555752 |
|
BLAKE2b-256 | 1118546b398bf5b6980e9e4deeb8ba07ea886fd1198bda36a597ba1575698b7a |
Close
Hashes for sag_py_fastapi_health-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daa84e710e0fbac497dab5ad8fb501ab112fcefa1238c7cf47a54013f2891125 |
|
MD5 | 38cf023387d21f3564a8bc72b1288137 |
|
BLAKE2b-256 | 54462ea84a247b026e96fb7ec65ec38ce253897e3a0ccb7030c3303908cf630a |