Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Colin-b/healthpy
Health Check for HTTP APIs (RFC implementation)
https://github.com/Colin-b/healthpy
api check consul health http redis status
Last synced: 3 months ago
JSON representation
Health Check for HTTP APIs (RFC implementation)
- Host: GitHub
- URL: https://github.com/Colin-b/healthpy
- Owner: Colin-b
- License: mit
- Created: 2019-11-28T22:30:37.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2020-11-04T19:34:02.000Z (about 4 years ago)
- Last Synced: 2024-07-16T20:41:24.787Z (4 months ago)
- Topics: api, check, consul, health, http, redis, status
- Language: Python
- Homepage:
- Size: 104 KB
- Stars: 14
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - Colin-b/healthpy - Health Check for HTTP APIs (RFC implementation) (Python)
README
Health Check for HTTP APIs
Create an health check endpoint on your REST API following [Health Check RFC](https://inadarei.github.io/rfc-healthcheck/) draft version 4.
- [Perform checks](#perform-checks)
- [Of an external HTTP resource](#http)
- [Of a redis server](#redis)
- [Return health check result](#return-result)
- [Aggregate multiple statuses](#compute-status-from-multiple-statuses)
- [Use a custom status](#using-custom-status)
- [HTTP response body](#http-response-body)
- [HTTP response status code](#http-response-status-code)
- [Endpoint](#endpoint)
- [Starlette](#starlette)
- [Flask-RestX](#flask-restx)## Perform checks
In case you have external dependencies, you should check the health of those dependencies.
### HTTP
If you have an external HTTP resource, you can check its health, as in the following sample:
```python
import healthpy.httpxstatus, checks = healthpy.httpx.check("petstore", "https://petstore3.swagger.io/api/v3/openapi.json")
```Note: [httpx](https://pypi.python.org/pypi/httpx) module must be installed to perform HTTP health checks.
Alternatively, you can use [requests](https://pypi.python.org/pypi/requests) to perform the exact same check:
```python
import healthpy.requestsstatus, checks = healthpy.requests.check("petstore", "https://petstore3.swagger.io/api/v3/openapi.json")
```### Redis
If you rely on redis, you should check its health.
[redis](https://pypi.python.org/pypi/redis) module must be installed to perform Redis health checks.
```python
import healthpy.redisstatus, checks = healthpy.redis.check("redis://redis_url", "redis_key")
```## Return result
Once all checks have been performed you should return the result to your client.
### Compute status from multiple statuses
If you performed more than one check, you have to compute an aggregated status from all the checks.
```python
import healthpystatus1 = healthpy.pass_status
status2 = healthpy.warn_status
statusN = healthpy.fail_statusstatus = healthpy.status(status1, status2, statusN)
```### Using custom status
By default pass status is "pass", warn status is "warn" and fail status is "fail".
It can be tweaked by setting the value of healthpy.*_status as in the following sample:
```python
import healthpyhealthpy.pass_status = "ok"
healthpy.warn_status = "custom"
healthpy.fail_status = "error"
```### HTTP response body
HTTP response body can be retrieved as a dictionary to be returned as JSON.
```python
import healthpystatus = healthpy.pass_status # replace with the aggregated status
checks = {} # replace with the computed checksbody = healthpy.response_body(status, checks=checks)
```Checks results are not mandatory in the response.
```python
import healthpystatus = healthpy.pass_status # replace with the aggregated status
body = healthpy.response_body(status)
```### HTTP response status code
HTTP response status code can be retrieved as an integer.
```python
import healthpystatus = healthpy.pass_status # replace with the aggregated status
status_code = healthpy.response_status_code(status)
```#### Consul
HTTP response status code should be a bit different for [Consul](https://www.consul.io/docs/agent/checks.html) health checks.
```python
import healthpystatus = healthpy.pass_status # replace with the aggregated status
status_code = healthpy.consul_response_status_code(status)
```## Endpoint
### Starlette
An helper function is available to create a [starlette](https://www.starlette.io) endpoint for [Consul](https://www.consul.io/docs/agent/checks.html) health check.
```python
from starlette.applications import Starlette
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.starlette import add_consul_health_endpointapp = Starlette()
async def health_check():
# TODO Replace by your own checks.
status_1, checks_1 = healthpy.httpx.check("my external dependency", "http://url_to_check")
status_2, checks_2 = healthpy.redis.check("redis://redis_url", "key_to_check")
return healthpy.status(status_1, status_2), {**checks_1, **checks_2}# /health endpoint will call the health_check coroutine.
add_consul_health_endpoint(app, health_check)
```Note: [starlette](https://pypi.python.org/pypi/starlette) module must be installed.
### Flask-RestX
An helper function is available to create a [Flask-RestX](https://flask-restx.readthedocs.io/en/latest/) endpoint for health check.
```python
import flask
import flask_restx
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.flask_restx import add_health_endpointapp = flask.Flask(__name__)
api = flask_restx.Api(app)async def health_check():
# TODO Replace by your own checks.
status_1, checks_1 = healthpy.httpx.check("my external dependency", "http://url_to_check")
status_2, checks_2 = healthpy.redis.check("redis://redis_url", "key_to_check")
return healthpy.status(status_1, status_2), {**checks_1, **checks_2}# /health endpoint will call the health_check coroutine.
add_health_endpoint(api, health_check)
```Note: [flask-restx](https://pypi.python.org/pypi/flask-restx) module must be installed.
#### Consul Service Health check
An helper function is available to create a [Flask-RestX](https://flask-restx.readthedocs.io/en/latest/) endpoint for [Consul](https://www.consul.io/docs/agent/checks.html) health check.
```python
import flask
import flask_restx
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.flask_restx import add_consul_health_endpointapp = flask.Flask(__name__)
api = flask_restx.Api(app)async def health_check():
# TODO Replace by your own checks.
status_1, checks_1 = healthpy.httpx.check("my external dependency", "http://url_to_check")
status_2, checks_2 = healthpy.redis.check("redis://redis_url", "key_to_check")
return healthpy.status(status_1, status_2), {**checks_1, **checks_2}# /health endpoint will call the health_check coroutine.
add_consul_health_endpoint(api, health_check)
```Note: [flask-restx](https://pypi.python.org/pypi/flask-restx) module must be installed.
## Testing
A `pytest` fixture can be used to mock the datetime returned in http health check.
```python
from healthpy.testing import mock_http_health_datetimedef test_http(mock_http_health_datetime):
# Time will be returned as "2018-10-11T15:05:05.663979"
pass # Add your test calling healthpy.http.check
```## How to install
1. [python 3.7+](https://www.python.org/downloads/) must be installed
2. Use pip to install module:
```sh
python -m pip install healthpy
```