|
| 1 | +"""Tests for ``agentex.lib.core.observability.tracing_metrics_recording``.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import agentex.lib.core.observability.tracing_metrics_recording as recording |
| 8 | + |
| 9 | + |
| 10 | +class _Item: |
| 11 | + def __init__(self, enqueued_at: float | None) -> None: |
| 12 | + self.enqueued_at = enqueued_at |
| 13 | + |
| 14 | + |
| 15 | +class TestIsMetricsEnabled: |
| 16 | + def setup_method(self) -> None: |
| 17 | + recording._metrics_enabled = None |
| 18 | + recording._tracing = None |
| 19 | + |
| 20 | + def test_enabled_by_default(self, monkeypatch): |
| 21 | + monkeypatch.delenv("AGENTEX_TRACING_METRICS", raising=False) |
| 22 | + assert recording.is_metrics_enabled() is True |
| 23 | + |
| 24 | + def test_disabled_by_zero(self, monkeypatch): |
| 25 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "0") |
| 26 | + recording._metrics_enabled = None |
| 27 | + assert recording.is_metrics_enabled() is False |
| 28 | + |
| 29 | + |
| 30 | +class TestRecordingHelpers: |
| 31 | + def setup_method(self) -> None: |
| 32 | + recording._metrics_enabled = None |
| 33 | + recording._tracing = None |
| 34 | + |
| 35 | + def test_record_span_enqueued_when_disabled_does_not_load_metrics(self, monkeypatch): |
| 36 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "0") |
| 37 | + recording._metrics_enabled = None |
| 38 | + with patch( |
| 39 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics" |
| 40 | + ) as mock_get: |
| 41 | + recording.record_span_enqueued("start") |
| 42 | + mock_get.assert_not_called() |
| 43 | + |
| 44 | + def test_record_span_enqueued_when_enabled(self, monkeypatch): |
| 45 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "1") |
| 46 | + recording._metrics_enabled = None |
| 47 | + mock_metrics = MagicMock() |
| 48 | + with patch( |
| 49 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics", |
| 50 | + return_value=mock_metrics, |
| 51 | + ): |
| 52 | + recording.record_span_enqueued("end") |
| 53 | + mock_metrics.span_events_enqueued.add.assert_called_once_with(1, {"event_type": "end"}) |
| 54 | + |
| 55 | + def test_monotonic_if_enabled_respects_kill_switch(self, monkeypatch): |
| 56 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "0") |
| 57 | + recording._metrics_enabled = None |
| 58 | + assert recording.monotonic_if_enabled() is None |
| 59 | + |
| 60 | + def test_record_batch_coalesced_records_lag(self, monkeypatch): |
| 61 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "1") |
| 62 | + recording._metrics_enabled = None |
| 63 | + mock_metrics = MagicMock() |
| 64 | + with patch( |
| 65 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics", |
| 66 | + return_value=mock_metrics, |
| 67 | + ), patch("agentex.lib.core.observability.tracing_metrics_recording.time.monotonic", return_value=10.0): |
| 68 | + recording.record_batch_coalesced( |
| 69 | + queue_depth=3, |
| 70 | + batch_items=[_Item(9.5), _Item(9.0)], |
| 71 | + ) |
| 72 | + mock_metrics.queue_depth.record.assert_called_once_with(3) |
| 73 | + mock_metrics.batch_items.record.assert_called_once_with(2) |
| 74 | + mock_metrics.queue_lag.record.assert_called_once_with(1000.0) |
| 75 | + |
| 76 | + def test_record_export_failure(self, monkeypatch): |
| 77 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "1") |
| 78 | + recording._metrics_enabled = None |
| 79 | + mock_metrics = MagicMock() |
| 80 | + |
| 81 | + class AuthenticationError(Exception): |
| 82 | + pass |
| 83 | + |
| 84 | + exc = AuthenticationError("Error code: 401 - denied") |
| 85 | + processor = type("SGPAsyncTracingProcessor", (), {})() |
| 86 | + |
| 87 | + with patch( |
| 88 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics", |
| 89 | + return_value=mock_metrics, |
| 90 | + ): |
| 91 | + recording.record_export_failure( |
| 92 | + processor=processor, |
| 93 | + event_type="start", |
| 94 | + span_count=5, |
| 95 | + exc=exc, |
| 96 | + ) |
| 97 | + |
| 98 | + mock_metrics.export_batch_failures.add.assert_called_once() |
| 99 | + mock_metrics.export_span_failures.add.assert_called_once_with( |
| 100 | + 5, |
| 101 | + { |
| 102 | + "processor": "sgp", |
| 103 | + "event_type": "start", |
| 104 | + "http_code": "401", |
| 105 | + "error_class": "authentication", |
| 106 | + }, |
| 107 | + ) |
| 108 | + |
| 109 | + def test_record_export_success(self, monkeypatch): |
| 110 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "1") |
| 111 | + recording._metrics_enabled = None |
| 112 | + mock_metrics = MagicMock() |
| 113 | + with patch( |
| 114 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics", |
| 115 | + return_value=mock_metrics, |
| 116 | + ): |
| 117 | + recording.record_export_success(event_type="end", span_count=12, processor="sgp") |
| 118 | + |
| 119 | + mock_metrics.export_batches.add.assert_called_once_with( |
| 120 | + 1, |
| 121 | + {"processor": "sgp", "event_type": "end"}, |
| 122 | + ) |
| 123 | + mock_metrics.export_spans.add.assert_called_once_with( |
| 124 | + 12, |
| 125 | + {"processor": "sgp", "event_type": "end"}, |
| 126 | + ) |
| 127 | + |
| 128 | + def test_record_export_success_accepts_processor_label(self, monkeypatch): |
| 129 | + monkeypatch.setenv("AGENTEX_TRACING_METRICS", "1") |
| 130 | + recording._metrics_enabled = None |
| 131 | + mock_metrics = MagicMock() |
| 132 | + with patch( |
| 133 | + "agentex.lib.core.observability.tracing_metrics.get_tracing_metrics", |
| 134 | + return_value=mock_metrics, |
| 135 | + ): |
| 136 | + recording.record_export_success( |
| 137 | + event_type="start", span_count=3, processor="other" |
| 138 | + ) |
| 139 | + |
| 140 | + mock_metrics.export_batches.add.assert_called_once_with( |
| 141 | + 1, |
| 142 | + {"processor": "other", "event_type": "start"}, |
| 143 | + ) |
0 commit comments