sag-py-fastapi-health


Namesag-py-fastapi-health JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/SamhammerAG/sag_py_fastapi_health
SummaryA library for fastapi health checks
upload_time2023-05-23 17:25:42
maintainer
docs_urlNone
authorSamhammer AG
requires_python>=3.8
licenseMIT
keywords fastapi health
VCS
bugtrack_url
requirements fastapi pydantic aiohttp typing-extensions
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "fastapi,health",
    "author": "Samhammer AG",
    "author_email": "support@samhammer.de",
    "download_url": "https://files.pythonhosted.org/packages/54/60/fb26b8f30635796064755be8b382320dbdcc6a91429e522d5485919535ac/sag-py-fastapi-health-0.1.3.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.3",
    "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": "1661bccfb13ee673f81f8dbc90a284d1a1f9192501a51c5325050d23102b6bb2",
                "md5": "a1ead1ef8820820e2bf68ff27b7a5827",
                "sha256": "37ff207954053aed702b563a9c7c3b99cbf6aae498bffd07ef90daa8f5b0d88a"
            },
            "downloads": -1,
            "filename": "sag_py_fastapi_health-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a1ead1ef8820820e2bf68ff27b7a5827",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8589,
            "upload_time": "2023-05-23T17:25:41",
            "upload_time_iso_8601": "2023-05-23T17:25:41.031323Z",
            "url": "https://files.pythonhosted.org/packages/16/61/bccfb13ee673f81f8dbc90a284d1a1f9192501a51c5325050d23102b6bb2/sag_py_fastapi_health-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5460fb26b8f30635796064755be8b382320dbdcc6a91429e522d5485919535ac",
                "md5": "6a94f8cf65fd420ffe142811073c1819",
                "sha256": "4413b537861f75c45788bea5cd0144923aad9e31e0adaaa3a7da917a39a297cd"
            },
            "downloads": -1,
            "filename": "sag-py-fastapi-health-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6a94f8cf65fd420ffe142811073c1819",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10573,
            "upload_time": "2023-05-23T17:25:42",
            "upload_time_iso_8601": "2023-05-23T17:25:42.747900Z",
            "url": "https://files.pythonhosted.org/packages/54/60/fb26b8f30635796064755be8b382320dbdcc6a91429e522d5485919535ac/sag-py-fastapi-health-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-23 17:25:42",
    "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.70.1"
                ]
            ]
        },
        {
            "name": "pydantic",
            "specs": [
                [
                    ">=",
                    "1.9"
                ]
            ]
        },
        {
            "name": "aiohttp",
            "specs": [
                [
                    "<",
                    "4"
                ],
                [
                    ">=",
                    "3"
                ]
            ]
        },
        {
            "name": "typing-extensions",
            "specs": [
                [
                    ">=",
                    "4.0.0"
                ]
            ]
        }
    ],
    "lcname": "sag-py-fastapi-health"
}
        
Elapsed time: 0.06622s