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.
pip install py-healthcheckVerified import paths — ran on the pinned version, not inferred.
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.
Initialize `HealthCheck(success_ttl=None, failed_ttl=None)` or tune `success_ttl` and `failed_ttl` parameters as needed.
Ensure all functions passed to `health.add_check()` adhere to the `(bool, str)` return signature. For example: `return True, "Service is up"` or `return False, "Service is down: error details"`.
Review documentation for `HealthCheck` or `add_check` for specific timeout parameters. Alternatively, implement timeouts using Python's `concurrent.futures` or `asyncio.wait_for` within your custom checker functions. The release notes suggest this is an added capability.
Run `pip install py-healthcheck` to install the library.
For Flask, ensure `HealthCheck(app, '/healthcheck')` and `EnvironmentDump(app, '/environment')` are correctly instantiated with your `app` object and desired paths. For Tornado, ensure `TornadoHandler` is correctly added to `app.add_handlers` or `tornado.web.Application` routes.
Modify your checker function to return a tuple, e.g., `return True, "All good"`.