Skip to main content

Health Check for HTTP APIs

pypi version Build status Coverage Code style: black Number of tests Number of downloads

Create an health check endpoint on your REST API following Health Check RFC draft version 4.

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:

import healthpy.httpx

status, checks = healthpy.httpx.check("petstore", "https://petstore3.swagger.io/api/v3/openapi.json")

Note: httpx module must be installed to perform HTTP health checks.

Alternatively, you can use requests to perform the exact same check:

import healthpy.requests

status, 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 module must be installed to perform Redis health checks.

import healthpy.redis

status, 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.

import healthpy

status1 = healthpy.pass_status 
status2 = healthpy.warn_status
statusN = healthpy.fail_status

status = 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:

import healthpy

healthpy.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.

import healthpy

status = healthpy.pass_status  # replace with the aggregated status
checks = {}  # replace with the computed checks

body = healthpy.response_body(status, checks=checks)

Checks results are not mandatory in the response.

import healthpy

status = 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.

import healthpy

status = 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 health checks.

import healthpy

status = 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 endpoint for Consul health check.

from starlette.applications import Starlette
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.starlette import add_consul_health_endpoint


app = 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 module must be installed.

Flask-RestX

An helper function is available to create a Flask-RestX endpoint for health check.

import flask
import flask_restx
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.flask_restx import add_health_endpoint


app = 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 module must be installed.

Consul Service Health check

An helper function is available to create a Flask-RestX endpoint for Consul health check.

import flask
import flask_restx
import healthpy
import healthpy.httpx
import healthpy.redis
from healthpy.flask_restx import add_consul_health_endpoint


app = 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 module must be installed.

Testing

A pytest fixture can be used to mock the datetime returned in http health check.

from healthpy.testing import mock_http_health_datetime

def 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+ must be installed
  2. Use pip to install module:
python -m pip install healthpy

Release files for healthpy 1.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for healthpy 1.14.0
File Size Uploaded
healthpy-1.14.0.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for healthpy 1.14.0
File Interpreter ABI Platform
healthpy-1.14.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / healthpy-1.14.0.tar.gz

Download URL healthpy-1.14.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
27df31dea95fb3d1a10c46f047ed8989bb6cdf86cde0124ead13bac12624597b
BLAKE2b-256 checksum
How to use checksums
36262a0b04bbc2859f4845010ba2c10653af5c33c05bada537e6f813d9dbd033
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

Release files / healthpy-1.14.0-py3-none-any.whl

Download URL healthpy-1.14.0-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6bbac0e06e5a65bc424030ca9f4c02a746ad67b688e632c8f088f2836991bb15
BLAKE2b-256 checksum
How to use checksums
5cd2c07f06811759e08f8cf62be1f295c293f60cd18c763ec243d2bf71a63063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

1.14.0 This release

2 release files

1.13.0

2 release files

1.12.0

2 release files

1.11.1

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page