py-healthcheck: Flask and Tornado Healthcheck Endpoints

1.10.1 · active · verified Thu Apr 16

py-healthcheck is a Python library that simplifies adding healthcheck and environment dump endpoints to Flask or Tornado web applications. It allows developers to define custom check functions to monitor application dependencies and internal state, exposing their status via a configurable HTTP route. The latest stable version is 1.10.1, though its release cadence appears irregular based on PyPI and GitHub history, with the most recent update in June 2022.

Common errors

Warnings

Install

Imports

Quickstart

This Flask quickstart demonstrates setting up `/healthcheck` and `/environment` endpoints. Custom functions (`redis_available`, `database_check`) are added to the health check. The `EnvironmentDump` also includes custom application data. Run with `python your_app.py` or `flask run` and access `/healthcheck` and `/environment` in your browser. Set `MOCK_REDIS_FAILURE=true` or `MOCK_DB_FAILURE=true` environment variables to see failure states.

from flask import Flask
from healthcheck import HealthCheck, EnvironmentDump
import os

app = Flask(__name__)

health = HealthCheck(app, '/healthcheck')
envdump = EnvironmentDump(app, '/environment')

def redis_available():
    # Simulate a Redis check
    # In a real app, you'd connect to Redis here
    if os.environ.get('MOCK_REDIS_FAILURE') == 'true':
        return False, "Redis connection failed (mocked)"
    return True, "redis ok"

def database_check():
    # Simulate a database check
    # In a real app, you'd check DB connection/query
    if os.environ.get('MOCK_DB_FAILURE') == 'true':
        return False, "Database connection failed (mocked)"
    return True, "database ok"

health.add_check(redis_available)
health.add_check(database_check)

def application_data():
    return {
        "maintainer": "Your Name",
        "git_repo": "https://github.com/your/repo",
        "version": "1.0.0"
    }

envdump.add_section("application", application_data)

# To run the Flask app (for demonstration, use 'flask run' in production)
if __name__ == '__main__':
    # Example usage: http://127.0.0.1:5000/healthcheck
    # Example usage: http://127.0.0.1:5000/environment
    app.run(debug=True)

view raw JSON →