feat(observability): wire request metrics into PryHttpMiddleware
This commit is contained in:
parent
ec9795caa6
commit
733e21cbcc
1 changed files with 10 additions and 2 deletions
12
api.py
12
api.py
|
|
@ -47,7 +47,7 @@ from extractor import SchemaExtractor
|
|||
from mconfig import PryConfig
|
||||
from mcp_production import make_fallback_server, register_all
|
||||
from mcp_sse import mcp_post_message, mcp_sse_endpoint
|
||||
from observability import get_metrics_output, setup_tracing
|
||||
from observability import REQUEST_COUNT, REQUEST_LATENCY, get_metrics_output, setup_tracing
|
||||
from pipeline import HOOK_POINTS, get_pipeline, run_pipeline
|
||||
from pryextras import BatchProcessor, TransformEngine, recorder, streams
|
||||
from routers.auth import router as auth_router
|
||||
|
|
@ -254,8 +254,10 @@ class PryHttpMiddleware:
|
|||
start = time.time()
|
||||
request_id = self._get_header(scope, b"x-request-id") or uuid.uuid4().hex[:12]
|
||||
path = scope.get("path", "")
|
||||
method = scope.get("method", "GET")
|
||||
client = scope.get("client")
|
||||
ip = client[0] if client else "unknown"
|
||||
response_status = 200
|
||||
|
||||
# Authentication check
|
||||
api_key = settings.api_key
|
||||
|
|
@ -329,7 +331,9 @@ class PryHttpMiddleware:
|
|||
request_id_bytes = request_id.encode()
|
||||
|
||||
async def wrapped_send(message: dict[str, Any]) -> None:
|
||||
nonlocal response_status
|
||||
if message["type"] == "http.response.start":
|
||||
response_status = message.get("status", 200)
|
||||
headers = list(message.get("headers", []))
|
||||
headers.append((b"x-ratelimit-limit", ratelimit_limit))
|
||||
headers.append((b"x-ratelimit-remaining", ratelimit_remaining))
|
||||
|
|
@ -341,12 +345,16 @@ class PryHttpMiddleware:
|
|||
await self.app(scope, receive, wrapped_send)
|
||||
|
||||
elapsed = time.time() - start
|
||||
if REQUEST_COUNT and REQUEST_LATENCY:
|
||||
REQUEST_COUNT.labels(method=method, endpoint=path, status=str(response_status)).inc()
|
||||
REQUEST_LATENCY.labels(endpoint=path).observe(elapsed)
|
||||
logger.info(
|
||||
"request_end",
|
||||
extra={
|
||||
"request_id": request_id,
|
||||
"method": scope.get("method", "GET"),
|
||||
"method": method,
|
||||
"path": path,
|
||||
"status": response_status,
|
||||
"duration": f"{elapsed:.3f}s",
|
||||
},
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue