Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quantara/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions quantara/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ faker = "^30.8.1"
notebook = "^7.2.2"
sentry-sdk = {extras = ["fastapi"], version = "^2.18.0"}
slowapi = "^0.1.9"
prometheus-client = "0.21.1"
structlog = ">=24.1.0"
[tool.poetry.group.dev.dependencies]
black = "24.8.0"
Expand Down
3 changes: 3 additions & 0 deletions quantara/web_app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from web_app.api.leaderboard import router as leaderboard_router
from web_app.api.referal import router as referal_router
from web_app.api.wallet_auth import router as auth_router
from web_app.api.metrics import router as metrics_router, PrometheusMiddleware
from web_app.config_validator import assert_valid_config
from web_app.api.middleware import MaxBodySizeMiddleware
from web_app.db.database import init_db
Expand Down Expand Up @@ -148,6 +149,7 @@ async def global_exception_handler(request: Request, exc: Exception):
# full middleware stack and can reject requests before they reach routers.
app.add_middleware(SlowAPIMiddleware)
app.add_middleware(MaxBodySizeMiddleware, max_body_size=1024*1024)
app.add_middleware(PrometheusMiddleware)


@app.middleware("http")
Expand Down Expand Up @@ -207,3 +209,4 @@ async def health_check(response: Response, db: Session = Depends(get_database)):
app.include_router(leaderboard_router)
app.include_router(referal_router)
app.include_router(auth_router)
app.include_router(metrics_router)
67 changes: 67 additions & 0 deletions quantara/web_app/api/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Prometheus metrics endpoint and middleware for the QUANTARA API.

Exposes /metrics with:
- http_requests_total: counter by method, endpoint, status
- http_request_duration_seconds: histogram of request latency
- http_requests_in_flight: gauge of concurrent requests
"""

import time

from fastapi import APIRouter
from prometheus_client import (
Counter,
Gauge,
Histogram,
generate_latest,
CONTENT_TYPE_LATEST,
)
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response

router = APIRouter()

REQUEST_COUNT = Counter(
"http_requests_total",
"Total HTTP request count",
["method", "endpoint", "status"],
)
REQUEST_LATENCY = Histogram(
"http_request_duration_seconds",
"HTTP request latency in seconds",
["method", "endpoint"],
)
REQUESTS_IN_FLIGHT = Gauge(
"http_requests_in_flight",
"Number of HTTP requests currently being processed",
)


class PrometheusMiddleware(BaseHTTPMiddleware):
"""Middleware that records request count, latency, and in-flight gauge."""

async def dispatch(self, request: Request, call_next) -> Response:
endpoint = request.url.path
method = request.method
REQUESTS_IN_FLIGHT.inc()
start = time.perf_counter()
try:
response = await call_next(request)
status = str(response.status_code)
except Exception:
status = "500"
raise
finally:
duration = time.perf_counter() - start
REQUEST_COUNT.labels(method=method, endpoint=endpoint, status=status).inc()
REQUEST_LATENCY.labels(method=method, endpoint=endpoint).observe(duration)
REQUESTS_IN_FLIGHT.dec()
return response


@router.get("/metrics", tags=["Observability"], summary="Prometheus metrics endpoint")
async def metrics() -> Response:
"""Expose Prometheus metrics in text format."""
return Response(content=generate_latest(), media_type=CONTENT_TYPE_LATEST)