mirror of
https://github.com/Jerryplusy/AI-powered-switches.git
synced 2025-07-04 21:29:18 +00:00
31 lines
646 B
Python
31 lines
646 B
Python
from prometheus_client import Counter, Histogram
|
|
from fastapi import Request
|
|
|
|
REQUESTS = Counter(
|
|
'api_requests_total',
|
|
'Total API Requests',
|
|
['method', 'endpoint']
|
|
)
|
|
|
|
LATENCY = Histogram(
|
|
'api_request_latency_seconds',
|
|
'API Request Latency',
|
|
['endpoint']
|
|
)
|
|
|
|
|
|
async def monitor_requests(request: Request, call_next):
|
|
start_time = time.time()
|
|
response = await call_next(request)
|
|
latency = time.time() - start_time
|
|
|
|
REQUESTS.labels(
|
|
method=request.method,
|
|
endpoint=request.url.path
|
|
).inc()
|
|
|
|
LATENCY.labels(
|
|
endpoint=request.url.path
|
|
).observe(latency)
|
|
|
|
return response |