A health monitor for FastAPI applications.
Project description
FastAPI Health Monitor
A health monitor for FastAPI applications which runs user provided background checks to determine the current overall health status of an API system.
Features
- Microservices architecture health endpoint.
- Returns HTTP status codes. (200 OK / 503 unavailable)
- Can be used for Kubernetes liveness / readiness checks.
- Supports custom dependency and subcomponent checks.
- Simple ok/error checks or optional support for more detailed metrics.
Installation
Install using the pip package manager.
pip install fastapi-health-monitor
Usage
This example shows a FastAPI application which returns a dad joke from an external API.
The checks are run every 10 seconds by default. This can be changed using an environment variable HEALTH_CHECK_DELAY_SECONDS
.
from fastapi import FastAPI
import requests
# Import health monitor
from fastapi_health_monitor import HealthMonitor, ComponentHealth, HealthStatus
app = FastAPI()
# Create health monitor instance
monitor = HealthMonitor(
root_app=app,
service_id="dadjokes",
version="1",
release_id="1.0.0",
extra_notes=["environment=test"],
health_endpoint="/health",
)
# Create a component check function
def check_external_api(component: ComponentHealth):
"""
Check that external API returns 200 OK
"""
res = requests.get(
"https://icanhazdadjoke.com/", headers={"Accept": "application/json"}
)
# Update status based on status code
if res.status_code == 200:
component.status = HealthStatus.OK
else:
component.status = HealthStatus.ERROR
return component
# Add a component to the health monitor
monitor.add_component(
component_name="icanhazdadjoke.com",
measurement_name="reachability",
check_function=check_external_api,
)
@app.get("/")
async def root():
"""
Return a dad joke from https://icanhazdadjoke.com/
"""
return (
requests.get(
"https://icanhazdadjoke.com/", headers={"Accept": "application/json"}
)
.json()
.get("joke")
)
Example response
{
"service_id": "dadjokes",
"status": "ok",
"version": "1",
"release_id": "1.0.0",
"notes": [
"startup_timestamp=2023-08-22T18:02:55.133246",
"last_checked_timestamp=2023-08-22T18:03:06.617896",
"environment=test"
],
"checks": {
"icanhazdadjoke.com:reachability": {
"component_name": "icanhazdadjoke.com",
"measurement_name": "reachability",
"status": "ok",
"time": "2023-08-22T18:03:06.617719"
},
"uptime": {
"measurement_name": "uptime",
"component_type": "system",
"observed_value": 11.484558,
"observed_unit": "s",
"status": "ok",
"time": "2023-08-22T18:03:06.617811"
}
}
}
Authors
FastAPI Health Monitor was created by Adam Kirchberger in 2023.
License
MIT
See LICENSE.
Change Log
See CHANGELOG
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file fastapi_health_monitor-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_health_monitor-1.0.1-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24cc8693fa3641b8bc76620d7c8b83f095df2c4be5ab92431d68fc7436a40f72 |
|
MD5 | 63e8f59a7a970ecb01c01d71914b5723 |
|
BLAKE2b-256 | c855524b0634159a45bd74abd2b2be7322cacc390ce32196442caa0435e745da |