|
1 | 1 | from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
2 | 5 | from prometheus_client import Counter, Histogram |
3 | 6 |
|
| 7 | +_otel_meter: Any | None = None |
| 8 | +_http_request_counter: Any | None = None |
| 9 | +_http_request_duration_histogram: Any | None = None |
| 10 | +_web_vitals_lcp_histogram: Any | None = None |
| 11 | + |
| 12 | +try: # pragma: no cover - optional dependency guard |
| 13 | + from opentelemetry import metrics as otel_metrics |
| 14 | +except ImportError: # pragma: no cover - optional dependency guard |
| 15 | + otel_metrics = None # type: ignore[assignment] |
| 16 | +else: |
| 17 | + _otel_meter = otel_metrics.get_meter(__name__) |
| 18 | + _http_request_counter = _otel_meter.create_counter( |
| 19 | + "http.server.request.count", |
| 20 | + description="HTTP requests processed by the backend", |
| 21 | + unit="1", |
| 22 | + ) |
| 23 | + _http_request_duration_histogram = _otel_meter.create_histogram( |
| 24 | + "http.server.duration", |
| 25 | + description="Duration of HTTP requests handled by the backend", |
| 26 | + unit="s", |
| 27 | + ) |
| 28 | + _web_vitals_lcp_histogram = _otel_meter.create_histogram( |
| 29 | + "frontend.web_vitals.lcp", |
| 30 | + description="Largest Contentful Paint reported from the frontend", |
| 31 | + unit="s", |
| 32 | + ) |
| 33 | + |
4 | 34 | REQUEST_LATENCY_BUCKETS = ( |
5 | 35 | 0.005, |
6 | 36 | 0.01, |
@@ -86,21 +116,41 @@ def observe_http_request( |
86 | 116 | service: str, |
87 | 117 | route: str, |
88 | 118 | method: str, |
89 | | - status: str, |
| 119 | + status_code: int | str, |
90 | 120 | duration_seconds: float, |
91 | 121 | ) -> None: |
92 | 122 | """Record a single HTTP request observation.""" |
93 | 123 |
|
| 124 | + status_label = str(status_code) |
94 | 125 | http_requests_total.labels( |
95 | | - service=service, route=route, method=method, status=status |
| 126 | + service=service, route=route, method=method, status=status_label |
96 | 127 | ).inc() |
97 | 128 | http_request_duration_seconds.labels( |
98 | | - service=service, route=route, method=method, status=status |
| 129 | + service=service, route=route, method=method, status=status_label |
99 | 130 | ).observe(duration_seconds) |
100 | 131 |
|
| 132 | + if _http_request_counter is not None or _http_request_duration_histogram is not None: |
| 133 | + attributes: dict[str, str | int] = { |
| 134 | + "service.name": service, |
| 135 | + "http.route": route, |
| 136 | + "http.method": method, |
| 137 | + } |
| 138 | + try: |
| 139 | + attributes["http.status_code"] = int(status_code) |
| 140 | + except (TypeError, ValueError): |
| 141 | + # Skip the status code attribute if it cannot be coerced to an int. |
| 142 | + pass |
| 143 | + |
| 144 | + if _http_request_counter is not None: |
| 145 | + _http_request_counter.add(1, attributes) |
| 146 | + if _http_request_duration_histogram is not None: |
| 147 | + _http_request_duration_histogram.record(duration_seconds, attributes) |
| 148 | + |
101 | 149 |
|
102 | 150 | def observe_lcp(*, app: str, seconds: float) -> None: |
103 | 151 | """Record a Largest Contentful Paint measurement in seconds.""" |
104 | 152 |
|
105 | 153 | web_vitals_lcp.labels(app=app).observe(seconds) |
| 154 | + if _web_vitals_lcp_histogram is not None: |
| 155 | + _web_vitals_lcp_histogram.record(seconds, {"app": app}) |
106 | 156 |
|
0 commit comments