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 configure in prtg
use the sensor "HTTP data advanced" (https://www.paessler.com/manuals/prtg/http_data_advanced_sensor) and configure to your prtg health endpoint (like in the example above ([URL_TO_YOUR_SERVICE]/health/health-prtg)
How to start developing
With vscode
Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.
With pycharm
- Install latest pycharm
- Install pycharm plugin BlackConnect
- Install pycharm plugin Mypy
- Configure the python interpreter/venv
- pip install requirements-dev.txt
- pip install black[d]
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
- Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
- Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
- Ctl+Alt+S => Click Tools => Actions on save => Reformat code
- Restart pycharm
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
Hashes for sag_py_fastapi_health-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 637481a4e85acde3b1db941371aa571d5ef721fd5c1176941cb0d7b12e45c860 |
|
MD5 | 8277316f61a7dc31a5ad3787cc306f2b |
|
BLAKE2b-256 | 9335d0ef79332130defd394c12bebe59bd79e831f7b186ecfa7ea3ecc85aea75 |
Hashes for sag_py_fastapi_health-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a12eccf78a7ca369053d61e1e6a6223075072101cb355a63364d2c1809b1118 |
|
MD5 | 890204703f38bbffd32a0ec56309d072 |
|
BLAKE2b-256 | 146e99baa20823338bf80a4d89fc4684b66d43c17ef920b918e3a96314aced0a |