Skip to content

Commit

Permalink
Improve health check functionality (#338)
Browse files Browse the repository at this point in the history
Enhanced the /health endpoint to perform an HTTP HEAD request to Google, returning the status code and response time. This change provides a more accurate health status and logs errors if the check fails.
  • Loading branch information
mhdzumair authored Nov 1, 2024
1 parent 48b3f5e commit b19ac44
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,18 @@ async def get_home(request: Request):
@app.get("/health", tags=["health"])
@wrappers.exclude_rate_limit
async def health():
return {"status": "healthy"}
start_time = asyncio.get_event_loop().time()
async with aiohttp.ClientSession() as session:
try:
async with session.head("https://www.google.com", timeout=10) as response:
return {
"status": "healthy",
"status_code": response.status,
"time": asyncio.get_event_loop().time() - start_time,
}
except Exception as e:
logging.error("Health check failed: %s", e)
raise HTTPException(status_code=503, detail="Health check failed.")


@app.get("/favicon.ico")
Expand Down

0 comments on commit b19ac44

Please sign in to comment.