📦 EqualifyEverything / equalify-reflow

📄 metrics.py · 164 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164"""Metrics middleware for FastAPI with OpenTelemetry and Prometheus.

This module provides automatic instrumentation for HTTP requests and
custom business metrics using OpenTelemetry and Prometheus.

Metrics exported on port 8001 (configurable via METRICS_PORT env var):
- HTTP request duration (histogram)
- HTTP request count by endpoint (counter)
- HTTP error rate by status code (counter)
- Active requests (gauge)
"""

import logging
from collections.abc import Awaitable, Callable

from fastapi import FastAPI, Request, Response
from prometheus_client import (
    REGISTRY,
    Counter,
    Gauge,
    Histogram,
    generate_latest,
)
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.routing import Match

from ..config import settings

logger = logging.getLogger(__name__)

# Prometheus Metrics
http_requests_total = Counter(
    "http_requests_total",
    "Total HTTP requests",
    ["method", "endpoint", "status"],
)

http_request_duration_seconds = Histogram(
    "http_request_duration_seconds",
    "HTTP request duration in seconds",
    ["method", "endpoint"],
    buckets=[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0],
)

http_requests_in_progress = Gauge(
    "http_requests_in_progress",
    "Number of HTTP requests currently being processed",
    ["method", "endpoint"],
)


class MetricsMiddleware(BaseHTTPMiddleware):
    """Middleware to collect HTTP metrics."""

    async def dispatch(
        self, request: Request, call_next: Callable[[Request], Awaitable[Response]]
    ) -> Response:
        """Process request and collect metrics.

        Args:
            request: FastAPI request object
            call_next: Next middleware/handler in chain

        Returns:
            Response from downstream handler
        """
        # Skip metrics endpoint itself
        if request.url.path == "/metrics":
            return await call_next(request)

        method = request.method

        # Get endpoint pattern (e.g., /api/documents/{job_id}/status)
        endpoint = self._get_endpoint_pattern(request)

        # Track in-progress requests
        http_requests_in_progress.labels(method=method, endpoint=endpoint).inc()

        try:
            # Time the request
            with http_request_duration_seconds.labels(
                method=method, endpoint=endpoint
            ).time():
                response = await call_next(request)

            # Record request
            http_requests_total.labels(
                method=method,
                endpoint=endpoint,
                status=response.status_code,
            ).inc()

            return response

        except Exception:
            # Record error
            http_requests_total.labels(
                method=method,
                endpoint=endpoint,
                status=500,
            ).inc()
            raise

        finally:
            # Decrement in-progress counter
            http_requests_in_progress.labels(method=method, endpoint=endpoint).dec()

    def _get_endpoint_pattern(self, request: Request) -> str:
        """Get endpoint pattern from request (e.g., /api/documents/{job_id}).

        Args:
            request: FastAPI request object

        Returns:
            Endpoint pattern string (or path if no pattern found)
        """
        try:
            for route in request.app.routes:
                match, _ = route.matches(request.scope)
                if match == Match.FULL:
                    return route.path  # type: ignore[no-any-return]

            # Fallback to raw path if no route matches
            return request.url.path

        except Exception as e:
            logger.warning(f"Failed to get endpoint pattern: {e}")
            return request.url.path


def setup_metrics(app: FastAPI) -> None:
    """Set up metrics collection for FastAPI application.

    This function:
    1. Registers the metrics middleware
    2. Adds a /metrics endpoint for Prometheus scraping

    Args:
        app: FastAPI application instance
    """
    if not settings.enable_metrics:
        logger.info("Metrics collection disabled")
        return

    logger.info(f"Setting up metrics on port {settings.metrics_port}")

    # Add metrics middleware
    app.add_middleware(MetricsMiddleware)

    # Add metrics endpoint
    @app.get("/metrics", include_in_schema=False)
    async def metrics() -> Response:
        """Prometheus metrics endpoint.

        Returns:
            Response with Prometheus format metrics
        """
        return Response(
            content=generate_latest(REGISTRY),
            media_type="text/plain; version=0.0.4",
        )

    logger.info("Metrics collection enabled at /metrics")