# fastapi_healthz
Streamline your health checks with FastAPI using this user-friendly health check module. It serves as the foundational
component for integrating and enhancing health checks within FastAPI.
This core module doesn't include any service checkers by default, but it offers a simple method for adding them. The
reason for housing various modules separately is to accommodate the distinct dependencies required for each, ensuring
that you only import the specific packages you need, thereby preventing any unnecessary package bloat.
**This package is Pydantic v2 compliant**.
## Install
`pip install fastapi-healthz` or `poetry add fastapi-healthz`.
Eventually, you can partially install the package by running
`pip install fastapi-healthz[redis]`
`pip install fastapi-healthz[rabbitmq]`
`pip install fastapi-healthz[database]`
`pip install fastapi-healthz[uri]`
`pip install fastapi-healthz[mongodb]`
to install only the package you really need, with the required dependencies.
## Adding Health Checks
Here is what you need to get started.
```python
from fastapi import FastAPI
from fastapi_healthz import (
HealthCheckRegistry,
HealthCheckRabbitMQ,
HealthCheckRedis,
HealthCheckUri,
HealthCheckDatabase,
health_check_route,
)
app = FastAPI()
# Add Health Checks
_healthChecks = HealthCheckRegistry()
# SQLAlchemy
_healthChecks.add(HealthCheckDatabase(uri="dialect+driver://username:password@host:port/database"))
# RabbitMQ
_healthChecks.add(HealthCheckRabbitMQ(host="localhost", port=5672, vhost="", username="username", password="pwd", ssl=True))
# Redis
_healthChecks.add(HealthCheckRedis(uri="redis://[password:]host:port/database"))
# This will check external URI and validate the response that is returned.
_healthChecks.add(HealthCheckUri(uri="https://www.reddit.com/r/aww.json"))
app.add_api_route('/health', endpoint=health_check_route(registry=_healthChecks))
```
## Returned Data
When you initiate a health check request, it will examine all the submitted entries and execute a fundamental query on
each of them. If the results conform to expectations, a status code of 200 is returned. However, if an error is
encountered, a 500 error response will be provided.
```json
{
"status":"Healthy",
"totalTimeTaken":"0:00:00.671642",
"entities":[
{
"alias":"db",
"status":"Healthy",
"timeTaken":"0:00:00.009619",
"tags":["db"]
},
{
"alias":"reddit",
"status":"Unhealthy",
"timeTaken":"0:00:00.661716",
"tags":["uri"]
}
]
}
```
## Writing a custom checker
You can effortlessly extend the functionality of this foundational module by incorporating additional health checks for
various services. Simply create a new service that imports the HealthCheckAbstract. Then, build the corresponding class
around this abstract.
Once your service is prepared, integrate it into the HealthCheckRegistry, and you're all set to commence testing.
Raw data
{
"_id": null,
"home_page": "https://github.com/matteocacciola/fastapi_healthz",
"name": "fastapi-healthz",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Matteo Cacciola",
"author_email": "matteo.cacciola@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9b/9e/638c43bb44aaae64ec5a19e7bdce3295f81501700932e9bfbf779fb3d21b/fastapi_healthz-1.0.0.tar.gz",
"platform": null,
"description": "# fastapi_healthz\n\nStreamline your health checks with FastAPI using this user-friendly health check module. It serves as the foundational\ncomponent for integrating and enhancing health checks within FastAPI.\n\nThis core module doesn't include any service checkers by default, but it offers a simple method for adding them. The\nreason for housing various modules separately is to accommodate the distinct dependencies required for each, ensuring\nthat you only import the specific packages you need, thereby preventing any unnecessary package bloat.\n\n**This package is Pydantic v2 compliant**.\n\n## Install\n\n`pip install fastapi-healthz` or `poetry add fastapi-healthz`.\n\nEventually, you can partially install the package by running\n\n`pip install fastapi-healthz[redis]`\n\n`pip install fastapi-healthz[rabbitmq]`\n\n`pip install fastapi-healthz[database]`\n\n`pip install fastapi-healthz[uri]`\n\n`pip install fastapi-healthz[mongodb]`\n\nto install only the package you really need, with the required dependencies.\n\n## Adding Health Checks\n\nHere is what you need to get started.\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_healthz import (\n HealthCheckRegistry,\n HealthCheckRabbitMQ,\n HealthCheckRedis,\n HealthCheckUri,\n HealthCheckDatabase,\n health_check_route,\n)\n\napp = FastAPI()\n\n# Add Health Checks\n_healthChecks = HealthCheckRegistry()\n\n# SQLAlchemy\n_healthChecks.add(HealthCheckDatabase(uri=\"dialect+driver://username:password@host:port/database\"))\n# RabbitMQ\n_healthChecks.add(HealthCheckRabbitMQ(host=\"localhost\", port=5672, vhost=\"\", username=\"username\", password=\"pwd\", ssl=True))\n# Redis\n_healthChecks.add(HealthCheckRedis(uri=\"redis://[password:]host:port/database\"))\n# This will check external URI and validate the response that is returned.\n_healthChecks.add(HealthCheckUri(uri=\"https://www.reddit.com/r/aww.json\"))\n\napp.add_api_route('/health', endpoint=health_check_route(registry=_healthChecks))\n\n```\n\n## Returned Data\n\nWhen you initiate a health check request, it will examine all the submitted entries and execute a fundamental query on\neach of them. If the results conform to expectations, a status code of 200 is returned. However, if an error is\nencountered, a 500 error response will be provided.\n\n```json\n{\n \"status\":\"Healthy\",\n \"totalTimeTaken\":\"0:00:00.671642\",\n \"entities\":[\n {\n \"alias\":\"db\",\n \"status\":\"Healthy\",\n \"timeTaken\":\"0:00:00.009619\",\n \"tags\":[\"db\"]\n },\n {\n \"alias\":\"reddit\",\n \"status\":\"Unhealthy\",\n \"timeTaken\":\"0:00:00.661716\",\n \"tags\":[\"uri\"]\n }\n ]\n}\n```\n\n## Writing a custom checker\nYou can effortlessly extend the functionality of this foundational module by incorporating additional health checks for\nvarious services. Simply create a new service that imports the HealthCheckAbstract. Then, build the corresponding class\naround this abstract.\n\nOnce your service is prepared, integrate it into the HealthCheckRegistry, and you're all set to commence testing.",
"bugtrack_url": null,
"license": "MIT",
"summary": "A module to have a FastAPI HealthCheck for database, RabbitMQ server, Redis, MongoDB or external URI to validate their healths.",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/matteocacciola/fastapi_healthz",
"Repository": "https://github.com/matteocacciola/fastapi_healthz"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "82032f4683051325463e450ff017a41c55bcc51fb4a7412ed75b1ffec8d4fced",
"md5": "ba0674faa975f2986a3bf24ea8ffaf29",
"sha256": "ed3a9e5c64743ae1a89329872ad0548e67f225e23f7a35612592e161ba05ed03"
},
"downloads": -1,
"filename": "fastapi_healthz-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ba0674faa975f2986a3bf24ea8ffaf29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 10230,
"upload_time": "2024-09-21T00:00:04",
"upload_time_iso_8601": "2024-09-21T00:00:04.217145Z",
"url": "https://files.pythonhosted.org/packages/82/03/2f4683051325463e450ff017a41c55bcc51fb4a7412ed75b1ffec8d4fced/fastapi_healthz-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9b9e638c43bb44aaae64ec5a19e7bdce3295f81501700932e9bfbf779fb3d21b",
"md5": "302d6498105ee6e119e58fadf1ae48f4",
"sha256": "61531cbea5c9ce9f2bd5cb4e2389de399c9727eab47b7583cbaba26719e4df70"
},
"downloads": -1,
"filename": "fastapi_healthz-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "302d6498105ee6e119e58fadf1ae48f4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 7554,
"upload_time": "2024-09-21T00:00:05",
"upload_time_iso_8601": "2024-09-21T00:00:05.924342Z",
"url": "https://files.pythonhosted.org/packages/9b/9e/638c43bb44aaae64ec5a19e7bdce3295f81501700932e9bfbf779fb3d21b/fastapi_healthz-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-21 00:00:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "matteocacciola",
"github_project": "fastapi_healthz",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastapi-healthz"
}