# sag_py_fastapi_health
[![Maintainability][codeclimate-image]][codeclimate-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
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
```python
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
```python
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
[codeclimate-image]:https://api.codeclimate.com/v1/badges/518206f10db22dbeb984/maintainability
[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_fastapi_health/maintainability
[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_fastapi_health/badge.svg?branch=master
[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_fastapi_health?branch=master
[snyk-image]:https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_health/badge.svg
[snyk-url]:https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_health
Raw data
{
"_id": null,
"home_page": "https://github.com/SamhammerAG/sag_py_fastapi_health",
"name": "sag-py-fastapi-health",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "fastapi, health",
"author": "Samhammer AG",
"author_email": "support@samhammer.de",
"download_url": "https://files.pythonhosted.org/packages/93/35/d0ef79332130defd394c12bebe59bd79e831f7b186ecfa7ea3ecc85aea75/sag_py_fastapi_health-0.1.4.tar.gz",
"platform": null,
"description": "# sag_py_fastapi_health\n[![Maintainability][codeclimate-image]][codeclimate-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n\nAdd health check endpoints to fastapi (similar to the ones dotnet core has)\n\n## What it does\n* Adds one or multiple health endpoint\n* Configurable output format (json or prtg)\n* Possibility to add checks (own and pre shipped)\n* Pre-Shipped tests for http get requests including basic auth and directory existence/readability/writability\n\n### Installation\npip install sag-py-fastapi-health\n\n## How to use\n\n### Sample usage with existing checks\n```python\nfrom sag_py_fastapi_health.checks.http import HttpCheck\nfrom sag_py_fastapi_health.checks.storage import StorageExistsCheck, StorageReadableCheck\nfrom sag_py_fastapi_health.formatter import DefaultResponseFormatter, PrtgResponseFormatter\nfrom sag_py_fastapi_health.models import Probe\nfrom sag_py_fastapi_health.router import HealthcheckRouter\n\nfrom config import config\n\nrouter = HealthcheckRouter(\n Probe(\n name=\"health\",\n response_formatter=DefaultResponseFormatter(),\n checks=[\n StorageExistsCheck(\"/opt/app/data\", name=\"my_dir_exists\"),\n StorageReadableCheck(\"/opt/app/data\", name=\"my_dir_is_readable\"),\n HttpCheck(\"https://localhost/auth\", name=\"auth_available\", timeout=5),\n ],\n ),\n Probe(\n name=\"health-prtg\",\n response_formatter=PrtgResponseFormatter(),\n checks=[\n StorageExistsCheck(\"/opt/app/data\", name=\"my_dir_exists\"),\n StorageReadableCheck(\"/opt/app/data\", name=\"my_dir_is_readable\"),\n HttpCheck(\"https://localhost/auth\", name=\"auth_available\", timeout=5),\n ],\n ),\n)\n\n```\n### Write your own check\n```python\nfrom sag_py_fastapi_health.models import CheckResult\n\nclass TestCheck(Check):\n def __init__(self, name: str = \"check\") -> None:\n self._name: str = name\n\n async def __call__(self) -> CheckResult:\n is_healthy: bool = a_custom_check()\n description: str = \"A description of the status or a error message\"\n\n return CheckResult(\n name=self._name,\n status=\"Healthy\" if is_healthy else \"Unhealthy\",\n description=description,\n )\n```\nThe description contains something like \"Directory ... was accessable\" or \"Service is running\" if everything is ok.\nIf there was an error, you can add the error message/exception message there.\n\n## How to configure in prtg\n\nuse 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)\n\n\n## How to start developing\n\n### With vscode\n\nJust install vscode with dev containers extension. All required extensions and configurations are prepared automatically.\n\n### With pycharm\n\n* Install latest pycharm\n* Install pycharm plugin BlackConnect\n* Install pycharm plugin Mypy\n* Configure the python interpreter/venv\n* pip install requirements-dev.txt\n* pip install black[d]\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat\n* Ctl+Alt+S => Click Tools => BlackConnect => \"Load from pyproject.yaml\" (ensure line length is 120)\n* Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the \"local instance\" config (e.g. C:\\Python310\\Scripts\\blackd.exe)\n* Ctl+Alt+S => Click Tools => Actions on save => Reformat code\n* Restart pycharm\n\n## How to publish\n* Update the version in setup.py and commit your change\n* Create a tag with the same version number\n* Let github do the rest\n\n[codeclimate-image]:https://api.codeclimate.com/v1/badges/518206f10db22dbeb984/maintainability\n[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_fastapi_health/maintainability\n[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_fastapi_health/badge.svg?branch=master\n[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_fastapi_health?branch=master\n[snyk-image]:https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_health/badge.svg\n[snyk-url]:https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_health\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A library for fastapi health checks",
"version": "0.1.4",
"project_urls": {
"Bug Reports": "https://github.com/SamhammerAG/sag_py_fastapi_health/issues",
"Documentation": "https://github.com/SamhammerAG/sag_py_fastapi_health",
"Homepage": "https://github.com/SamhammerAG/sag_py_fastapi_health",
"Source": "https://github.com/SamhammerAG/sag_py_fastapi_health"
},
"split_keywords": [
"fastapi",
" health"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "146e99baa20823338bf80a4d89fc4684b66d43c17ef920b918e3a96314aced0a",
"md5": "890204703f38bbffd32a0ec56309d072",
"sha256": "3a12eccf78a7ca369053d61e1e6a6223075072101cb355a63364d2c1809b1118"
},
"downloads": -1,
"filename": "sag_py_fastapi_health-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "890204703f38bbffd32a0ec56309d072",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8758,
"upload_time": "2024-05-07T09:26:58",
"upload_time_iso_8601": "2024-05-07T09:26:58.026273Z",
"url": "https://files.pythonhosted.org/packages/14/6e/99baa20823338bf80a4d89fc4684b66d43c17ef920b918e3a96314aced0a/sag_py_fastapi_health-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9335d0ef79332130defd394c12bebe59bd79e831f7b186ecfa7ea3ecc85aea75",
"md5": "8277316f61a7dc31a5ad3787cc306f2b",
"sha256": "637481a4e85acde3b1db941371aa571d5ef721fd5c1176941cb0d7b12e45c860"
},
"downloads": -1,
"filename": "sag_py_fastapi_health-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "8277316f61a7dc31a5ad3787cc306f2b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 11081,
"upload_time": "2024-05-07T09:27:00",
"upload_time_iso_8601": "2024-05-07T09:27:00.103290Z",
"url": "https://files.pythonhosted.org/packages/93/35/d0ef79332130defd394c12bebe59bd79e831f7b186ecfa7ea3ecc85aea75/sag_py_fastapi_health-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-07 09:27:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "SamhammerAG",
"github_project": "sag_py_fastapi_health",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "fastapi",
"specs": [
[
">=",
"0.109.1"
]
]
},
{
"name": "pydantic",
"specs": [
[
">=",
"1.9"
]
]
},
{
"name": "aiohttp",
"specs": [
[
">=",
"3"
],
[
"<",
"4"
]
]
},
{
"name": "typing-extensions",
"specs": [
[
">=",
"4.0.0"
]
]
}
],
"lcname": "sag-py-fastapi-health"
}