WebhockTransfer/app/http_client.py
auto-bot 6f4793a330 feat: add comprehensive health checks and Prometheus metrics
- Add app/health.py with HealthChecker and MetricsCollector classes
- Implement composite /health endpoint checking DB and HTTP client
- Add /metrics endpoint with Prometheus exposition format
- Add webhook request metrics (counters, histograms, gauges)
- Create app/http_client.py for shared AsyncClient management
- Update app/main.py lifespan to init/close HTTP client
- Add comprehensive tests in tests/test_health.py

This enables proper observability with health checks and metrics
for monitoring system status and webhook processing performance.
2025-12-24 11:10:54 +08:00

32 lines
813 B
Python

"""
HTTP client management for shared AsyncClient instance.
"""
import httpx
from typing import Optional
# Global HTTP client instance
_http_client: Optional[httpx.AsyncClient] = None
async def init_http_client():
"""Initialize the shared HTTP client."""
global _http_client
if _http_client is None:
_http_client = httpx.AsyncClient(
timeout=httpx.Timeout(10.0, connect=5.0),
limits=httpx.Limits(max_keepalive_connections=20, max_connections=100)
)
async def close_http_client():
"""Close the shared HTTP client."""
global _http_client
if _http_client:
await _http_client.aclose()
_http_client = None
def get_http_client() -> Optional[httpx.AsyncClient]:
"""Get the shared HTTP client instance."""
return _http_client