|
1 | 1 | """Unit tests for Prometheus metric recording helpers.""" |
2 | 2 |
|
| 3 | +from collections.abc import Callable |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
3 | 8 | from pytest_mock import MockerFixture |
4 | 9 |
|
5 | 10 | from metrics import recording |
6 | 11 |
|
7 | 12 |
|
| 13 | +@dataclass(frozen=True) |
| 14 | +class CounterRecorderCase: |
| 15 | + """Expected behavior for a counter-style metric recorder.""" |
| 16 | + |
| 17 | + metric_path: str |
| 18 | + recorder: Callable[..., None] |
| 19 | + args: tuple[object, ...] |
| 20 | + labels: tuple[object, ...] |
| 21 | + warning_message: str |
| 22 | + |
| 23 | + |
| 24 | +@dataclass(frozen=True) |
| 25 | +class HistogramRecorderCase: |
| 26 | + """Expected behavior for a histogram-style metric recorder.""" |
| 27 | + |
| 28 | + metric_path: str |
| 29 | + recorder: Callable[..., None] |
| 30 | + args: tuple[object, ...] |
| 31 | + labels: tuple[object, ...] |
| 32 | + duration: float |
| 33 | + warning_message: str |
| 34 | + |
| 35 | + |
8 | 36 | def test_measure_response_duration_records_timer(mocker: MockerFixture) -> None: |
9 | 37 | """Test that response duration measurement uses the path label timer.""" |
10 | 38 | mock_timer = mocker.MagicMock() |
@@ -159,3 +187,78 @@ def test_record_llm_token_usage_logs_metric_errors(mocker: MockerFixture) -> Non |
159 | 187 | mock_logger.warning.assert_called_once_with( |
160 | 188 | "Failed to update token metrics", exc_info=True |
161 | 189 | ) |
| 190 | + |
| 191 | + |
| 192 | +@pytest.fixture(name="recording_logger") |
| 193 | +def recording_logger_fixture(mocker: MockerFixture) -> Any: |
| 194 | + """Patch the metric recording logger for failure assertions.""" |
| 195 | + return mocker.patch("metrics.recording.logger") |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.parametrize( |
| 199 | + "case", |
| 200 | + [ |
| 201 | + CounterRecorderCase( |
| 202 | + metric_path="metrics.recording.metrics.authorization_checks_total", |
| 203 | + recorder=recording.record_authorization_check, |
| 204 | + args=("responses", "success"), |
| 205 | + labels=("responses", "success"), |
| 206 | + warning_message="Failed to update authorization metric", |
| 207 | + ), |
| 208 | + ], |
| 209 | +) |
| 210 | +def test_counter_recorders_update_metrics_and_log_errors( |
| 211 | + mocker: MockerFixture, |
| 212 | + recording_logger: Any, |
| 213 | + case: CounterRecorderCase, |
| 214 | +) -> None: |
| 215 | + """Test new single-counter helpers with shared success and failure coverage.""" |
| 216 | + mock_metric = mocker.patch(case.metric_path) |
| 217 | + |
| 218 | + case.recorder(*case.args) |
| 219 | + |
| 220 | + mock_metric.labels.assert_called_once_with(*case.labels) |
| 221 | + mock_metric.labels.return_value.inc.assert_called_once() |
| 222 | + |
| 223 | + mock_metric.reset_mock() |
| 224 | + mock_metric.labels.return_value.inc.side_effect = AttributeError("missing") |
| 225 | + case.recorder(*case.args) |
| 226 | + |
| 227 | + recording_logger.warning.assert_called_once_with( |
| 228 | + case.warning_message, exc_info=True |
| 229 | + ) |
| 230 | + |
| 231 | + |
| 232 | +@pytest.mark.parametrize( |
| 233 | + "case", |
| 234 | + [ |
| 235 | + HistogramRecorderCase( |
| 236 | + metric_path="metrics.recording.metrics.authorization_duration_seconds", |
| 237 | + recorder=recording.record_authorization_duration, |
| 238 | + args=("responses", "success", 0.25), |
| 239 | + labels=("responses", "success"), |
| 240 | + duration=0.25, |
| 241 | + warning_message="Failed to update authorization duration metric", |
| 242 | + ), |
| 243 | + ], |
| 244 | +) |
| 245 | +def test_histogram_recorders_observe_metrics_and_log_errors( |
| 246 | + mocker: MockerFixture, |
| 247 | + recording_logger: Any, |
| 248 | + case: HistogramRecorderCase, |
| 249 | +) -> None: |
| 250 | + """Test new histogram helpers with shared success and failure coverage.""" |
| 251 | + mock_metric = mocker.patch(case.metric_path) |
| 252 | + |
| 253 | + case.recorder(*case.args) |
| 254 | + |
| 255 | + mock_metric.labels.assert_called_once_with(*case.labels) |
| 256 | + mock_metric.labels.return_value.observe.assert_called_once_with(case.duration) |
| 257 | + |
| 258 | + mock_metric.reset_mock() |
| 259 | + mock_metric.labels.return_value.observe.side_effect = TypeError("bad") |
| 260 | + case.recorder(*case.args) |
| 261 | + |
| 262 | + recording_logger.warning.assert_called_once_with( |
| 263 | + case.warning_message, exc_info=True |
| 264 | + ) |
0 commit comments