If a condition function throws an exception, the health endpoint fails with a 500. I don't think this is the right thing to do for a function supposed to check for something that might be in failing state.
As a consequence, the way I'm using fastapi-health, in an app using Postgres, RabbitMQ, MinIO, looks like:
health_endpoint = health([database_check, rabbitmq_check, storage_check])
app.add_api_route("/health", health_endpoint, include_in_schema=False)
async def database_check() -> bool:
try:
async with db.pool.connection(timeout=5) as conn:
await conn.execute("select 1")
return True
except Exception as e:
logger.error("error checking database: %s - %s", type(e).__name__, e)
return False
async def rabbitmq_check() -> bool:
try:
await mq.mq.check_health()
return True
except Exception as e:
logger.error("error checking rabbitmq: %s - %s", type(e).__name__, e)
return False
async def storage_check() -> bool:
try:
await FileUploader.check_health(minio_url)
return True
except Exception as e:
logger.error("error checking storage: %s - %s", type(e).__name__, e)
return False
I think that the health endpoint should be just tolerant of failure and consider an error thrown by a condition function as a failure. I should be able to write the above as:
async def database_check() -> bool:
async with db.pool.connection(timeout=5) as conn:
await conn.execute("select 1")
return True
async def rabbitmq_check() -> bool:
await mq.mq.check_health()
return True
async def storage_check() -> bool:
await FileUploader.check_health(minio_url)
return True
For me the return True is redundant too, but that's probably a different matter.
Thoughts?
If a condition function throws an exception, the health endpoint fails with a 500. I don't think this is the right thing to do for a function supposed to check for something that might be in failing state.
As a consequence, the way I'm using fastapi-health, in an app using Postgres, RabbitMQ, MinIO, looks like:
I think that the health endpoint should be just tolerant of failure and consider an error thrown by a condition function as a failure. I should be able to write the above as:
For me the
return Trueis redundant too, but that's probably a different matter.Thoughts?