Fix: Address CodeRabbit suggestions for health endpoint

- Use context.startup_time for consistent uptime tracking
  - Prefer context.startup_time when available (matches MCP health_check tool)
  - Fallback to module-level _server_start_time during startup
- Add exc_info=True to exception logging for full stack traces
  - Follows coding guidelines for error message preservation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
leex279
2025-11-08 22:34:09 +01:00
parent c0190e90c9
commit 4620cfa8d6

View File

@@ -29,7 +29,6 @@ from pathlib import Path
from typing import Any from typing import Any
from dotenv import load_dotenv from dotenv import load_dotenv
from mcp.server.fastmcp import Context, FastMCP from mcp.server.fastmcp import Context, FastMCP
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
@@ -557,11 +556,10 @@ async def http_health_endpoint(request: Request):
"""HTTP health check endpoint for monitoring systems.""" """HTTP health check endpoint for monitoring systems."""
logger.info("Health endpoint called via HTTP") logger.info("Health endpoint called via HTTP")
try: try:
# Calculate uptime from module load time
uptime = time.time() - _server_start_time
# Try to get the shared context for detailed health info # Try to get the shared context for detailed health info
if _shared_context and hasattr(_shared_context, "health_status"): if _shared_context and hasattr(_shared_context, "health_status"):
# Use actual server startup time for consistency with MCP health_check tool
uptime = time.time() - _shared_context.startup_time
await perform_health_checks(_shared_context) await perform_health_checks(_shared_context)
return JSONResponse({ return JSONResponse({
@@ -571,7 +569,8 @@ async def http_health_endpoint(request: Request):
"timestamp": datetime.now().isoformat(), "timestamp": datetime.now().isoformat(),
}) })
else: else:
# Server starting up or no MCP connections yet - still return uptime # Server starting up or no MCP connections yet - use module load time as fallback
uptime = time.time() - _server_start_time
return JSONResponse({ return JSONResponse({
"success": True, "success": True,
"status": "ready", "status": "ready",
@@ -580,7 +579,7 @@ async def http_health_endpoint(request: Request):
"timestamp": datetime.now().isoformat(), "timestamp": datetime.now().isoformat(),
}) })
except Exception as e: except Exception as e:
logger.error(f"HTTP health check failed: {e}") logger.error(f"HTTP health check failed: {e}", exc_info=True)
return JSONResponse({ return JSONResponse({
"success": False, "success": False,
"error": f"Health check failed: {str(e)}", "error": f"Health check failed: {str(e)}",