fastapi-health-monitor


Namefastapi-health-monitor JSON
Version 1.0.1 PyPI version JSON
download
home_page
SummaryA health monitor for FastAPI applications.
upload_time2023-08-26 08:14:53
maintainer
docs_urlNone
authorAdam Kirchberger
requires_python>=3.10,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastAPI Health Monitor

[![codecov](https://codecov.io/gh/adamkirchberger/fastapi-health-monitor/graph/badge.svg?token=8IdTKDXy89)](https://codecov.io/gh/adamkirchberger/fastapi-health-monitor)

A health monitor for FastAPI applications which runs user provided background checks to determine the current overall health status of an API system.

## Features

* Microservices architecture health endpoint.
* Returns HTTP status codes. (200 OK / 503 unavailable)
* Can be used for Kubernetes liveness / readiness checks.
* Supports custom dependency and subcomponent checks.
  * Simple ok/error checks or optional support for more detailed metrics.

## Installation

Install using the pip package manager.

```
pip install fastapi-health-monitor
```

## Usage

This example shows a FastAPI application which returns a dad joke from an external API.

The checks are run every 10 seconds by default. This can be changed using an environment variable `HEALTH_CHECK_DELAY_SECONDS`.

```python
from fastapi import FastAPI
import requests

# Import health monitor
from fastapi_health_monitor import HealthMonitor, ComponentHealth, HealthStatus


app = FastAPI()

# Create health monitor instance
monitor = HealthMonitor(
    root_app=app,
    service_id="dadjokes",
    version="1",
    release_id="1.0.0",
    extra_notes=["environment=test"],
    health_endpoint="/health",
)

# Create a component check function
def check_external_api(component: ComponentHealth):
    """
    Check that external API returns 200 OK
    """
    res = requests.get(
        "https://icanhazdadjoke.com/", headers={"Accept": "application/json"}
    )

    # Update status based on status code
    if res.status_code == 200:
        component.status = HealthStatus.OK
    else:
        component.status = HealthStatus.ERROR

    return component


# Add a component to the health monitor
monitor.add_component(
    component_name="icanhazdadjoke.com",
    measurement_name="reachability",
    check_function=check_external_api,
)


@app.get("/")
async def root():
    """
    Return a dad joke from https://icanhazdadjoke.com/
    """
    return (
        requests.get(
            "https://icanhazdadjoke.com/", headers={"Accept": "application/json"}
        )
        .json()
        .get("joke")
    )

```

## Example response

```json
{
    "service_id": "dadjokes",
    "status": "ok",
    "version": "1",
    "release_id": "1.0.0",
    "notes": [
        "startup_timestamp=2023-08-22T18:02:55.133246",
        "last_checked_timestamp=2023-08-22T18:03:06.617896",
        "environment=test"
    ],
    "checks": {
        "icanhazdadjoke.com:reachability": {
            "component_name": "icanhazdadjoke.com",
            "measurement_name": "reachability",
            "status": "ok",
            "time": "2023-08-22T18:03:06.617719"
        },
        "uptime": {
            "measurement_name": "uptime",
            "component_type": "system",
            "observed_value": 11.484558,
            "observed_unit": "s",
            "status": "ok",
            "time": "2023-08-22T18:03:06.617811"
        }
    }
}
```

## Authors

FastAPI Health Monitor was created by Adam Kirchberger in 2023.

## License

MIT

See [LICENSE](https://github.com/adamkirchberger/fastapi-health-monitor/blob/main/LICENSE).

## Change Log

See [CHANGELOG](https://github.com/adamkirchberger/fastapi-health-monitor/blob/main/CHANGELOG.md)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "fastapi-health-monitor",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Adam Kirchberger",
    "author_email": "adamkirchberger@users.noreply.github.com",
    "download_url": "",
    "platform": null,
    "description": "# FastAPI Health Monitor\n\n[![codecov](https://codecov.io/gh/adamkirchberger/fastapi-health-monitor/graph/badge.svg?token=8IdTKDXy89)](https://codecov.io/gh/adamkirchberger/fastapi-health-monitor)\n\nA health monitor for FastAPI applications which runs user provided background checks to determine the current overall health status of an API system.\n\n## Features\n\n* Microservices architecture health endpoint.\n* Returns HTTP status codes. (200 OK / 503 unavailable)\n* Can be used for Kubernetes liveness / readiness checks.\n* Supports custom dependency and subcomponent checks.\n  * Simple ok/error checks or optional support for more detailed metrics.\n\n## Installation\n\nInstall using the pip package manager.\n\n```\npip install fastapi-health-monitor\n```\n\n## Usage\n\nThis example shows a FastAPI application which returns a dad joke from an external API.\n\nThe checks are run every 10 seconds by default. This can be changed using an environment variable `HEALTH_CHECK_DELAY_SECONDS`.\n\n```python\nfrom fastapi import FastAPI\nimport requests\n\n#\u00a0Import health monitor\nfrom fastapi_health_monitor import HealthMonitor, ComponentHealth, HealthStatus\n\n\napp = FastAPI()\n\n# Create health monitor instance\nmonitor = HealthMonitor(\n    root_app=app,\n    service_id=\"dadjokes\",\n    version=\"1\",\n    release_id=\"1.0.0\",\n    extra_notes=[\"environment=test\"],\n    health_endpoint=\"/health\",\n)\n\n# Create a component check function\ndef check_external_api(component: ComponentHealth):\n    \"\"\"\n    Check that external API returns 200 OK\n    \"\"\"\n    res = requests.get(\n        \"https://icanhazdadjoke.com/\", headers={\"Accept\": \"application/json\"}\n    )\n\n    # Update status based on status code\n    if res.status_code == 200:\n        component.status = HealthStatus.OK\n    else:\n        component.status = HealthStatus.ERROR\n\n    return component\n\n\n# Add a component to the health monitor\nmonitor.add_component(\n    component_name=\"icanhazdadjoke.com\",\n    measurement_name=\"reachability\",\n    check_function=check_external_api,\n)\n\n\n@app.get(\"/\")\nasync def root():\n    \"\"\"\n    Return a dad joke from https://icanhazdadjoke.com/\n    \"\"\"\n    return (\n        requests.get(\n            \"https://icanhazdadjoke.com/\", headers={\"Accept\": \"application/json\"}\n        )\n        .json()\n        .get(\"joke\")\n    )\n\n```\n\n## Example response\n\n```json\n{\n    \"service_id\": \"dadjokes\",\n    \"status\": \"ok\",\n    \"version\": \"1\",\n    \"release_id\": \"1.0.0\",\n    \"notes\": [\n        \"startup_timestamp=2023-08-22T18:02:55.133246\",\n        \"last_checked_timestamp=2023-08-22T18:03:06.617896\",\n        \"environment=test\"\n    ],\n    \"checks\": {\n        \"icanhazdadjoke.com:reachability\": {\n            \"component_name\": \"icanhazdadjoke.com\",\n            \"measurement_name\": \"reachability\",\n            \"status\": \"ok\",\n            \"time\": \"2023-08-22T18:03:06.617719\"\n        },\n        \"uptime\": {\n            \"measurement_name\": \"uptime\",\n            \"component_type\": \"system\",\n            \"observed_value\": 11.484558,\n            \"observed_unit\": \"s\",\n            \"status\": \"ok\",\n            \"time\": \"2023-08-22T18:03:06.617811\"\n        }\n    }\n}\n```\n\n## Authors\n\nFastAPI Health Monitor was created by Adam Kirchberger in 2023.\n\n## License\n\nMIT\n\nSee [LICENSE](https://github.com/adamkirchberger/fastapi-health-monitor/blob/main/LICENSE).\n\n## Change Log\n\nSee [CHANGELOG](https://github.com/adamkirchberger/fastapi-health-monitor/blob/main/CHANGELOG.md)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A health monitor for FastAPI applications.",
    "version": "1.0.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c855524b0634159a45bd74abd2b2be7322cacc390ce32196442caa0435e745da",
                "md5": "63e8f59a7a970ecb01c01d71914b5723",
                "sha256": "24cc8693fa3641b8bc76620d7c8b83f095df2c4be5ab92431d68fc7436a40f72"
            },
            "downloads": -1,
            "filename": "fastapi_health_monitor-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "63e8f59a7a970ecb01c01d71914b5723",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 9749,
            "upload_time": "2023-08-26T08:14:53",
            "upload_time_iso_8601": "2023-08-26T08:14:53.154069Z",
            "url": "https://files.pythonhosted.org/packages/c8/55/524b0634159a45bd74abd2b2be7322cacc390ce32196442caa0435e745da/fastapi_health_monitor-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-26 08:14:53",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "fastapi-health-monitor"
}
        
Elapsed time: 1.67059s