Files
simbarag/metrics.py
T
ryan 0478ee7568 Add Prometheus metrics endpoint for chat response observability
Tracks end-to-end response time and request counts (by stream mode and
result) via prometheus_client. Exposes GET /metrics behind a bearer
token (METRICS_TOKEN env var) so Prometheus can scrape it without
exposing raw data publicly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-15 22:57:12 -04:00

56 lines
1.3 KiB
Python

import time
from contextlib import contextmanager
from prometheus_client import (
CollectorRegistry,
Counter,
Histogram,
generate_latest,
)
REGISTRY = CollectorRegistry(auto_describe=True)
CHAT_RESPONSE_SECONDS = Histogram(
"raggr_chat_response_seconds",
"End-to-end chat response time in seconds.",
labelnames=("stream",),
buckets=(0.5, 1, 2, 5, 10, 30, 60, 120, 300),
registry=REGISTRY,
)
CHAT_REQUESTS_TOTAL = Counter(
"raggr_chat_requests_total",
"Total chat requests by stream mode and result.",
labelnames=("stream", "result"),
registry=REGISTRY,
)
def _s(stream: bool) -> str:
return "true" if stream else "false"
@contextmanager
def time_response(stream: bool):
t0 = time.monotonic()
result = "success"
try:
yield
except Exception:
result = "error"
raise
finally:
CHAT_RESPONSE_SECONDS.labels(stream=_s(stream)).observe(time.monotonic() - t0)
CHAT_REQUESTS_TOTAL.labels(stream=_s(stream), result=result).inc()
def observe_response(stream: bool, seconds: float) -> None:
CHAT_RESPONSE_SECONDS.labels(stream=_s(stream)).observe(seconds)
def count_result(stream: bool, result: str) -> None:
CHAT_REQUESTS_TOTAL.labels(stream=_s(stream), result=result).inc()
def render_metrics() -> bytes:
return generate_latest(REGISTRY)