|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import uuid |
| 4 | +import logging |
| 5 | +from typing import override |
| 6 | +from datetime import UTC, datetime |
| 7 | + |
| 8 | +from agentex.types.span import Span |
| 9 | +from agentex.lib.types.tracing import TracingProcessorConfig |
| 10 | +from agentex.lib.core.tracing.processors.tracing_processor_interface import ( |
| 11 | + AsyncTracingProcessor, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def _make_span(span_id: str | None = None) -> Span: |
| 16 | + return Span( |
| 17 | + id=span_id or str(uuid.uuid4()), |
| 18 | + name="test-span", |
| 19 | + start_time=datetime.now(UTC), |
| 20 | + trace_id="trace-1", |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +class _RecordingProcessor(AsyncTracingProcessor): |
| 25 | + """Test processor that records every on_span_* call and fails on demand.""" |
| 26 | + |
| 27 | + def __init__(self, fail_ids: set[str] | None = None) -> None: |
| 28 | + self.started_ids: list[str] = [] |
| 29 | + self.ended_ids: list[str] = [] |
| 30 | + self._fail_ids = fail_ids or set() |
| 31 | + |
| 32 | + @override |
| 33 | + async def on_span_start(self, span: Span) -> None: |
| 34 | + self.started_ids.append(span.id) |
| 35 | + if span.id in self._fail_ids: |
| 36 | + raise RuntimeError(f"boom-start-{span.id}") |
| 37 | + |
| 38 | + @override |
| 39 | + async def on_span_end(self, span: Span) -> None: |
| 40 | + self.ended_ids.append(span.id) |
| 41 | + if span.id in self._fail_ids: |
| 42 | + raise RuntimeError(f"boom-end-{span.id}") |
| 43 | + |
| 44 | + @override |
| 45 | + async def shutdown(self) -> None: |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +class TestDefaultBatchedFanout: |
| 50 | + """The default on_spans_start / on_spans_end in AsyncTracingProcessor must: |
| 51 | + - dispatch to the single-span method for every span |
| 52 | + - continue after individual failures (not short-circuit) |
| 53 | + - log each failure individually |
| 54 | + - not propagate exceptions to the caller |
| 55 | + """ |
| 56 | + |
| 57 | + async def test_on_spans_start_runs_every_span_despite_failures(self, caplog): |
| 58 | + proc = _RecordingProcessor(fail_ids={"span-1"}) |
| 59 | + spans = [_make_span(f"span-{i}") for i in range(3)] |
| 60 | + |
| 61 | + with caplog.at_level(logging.ERROR): |
| 62 | + # Must not raise, even though span-1 fails. |
| 63 | + await proc.on_spans_start(spans) |
| 64 | + |
| 65 | + # Every span's on_span_start was invoked |
| 66 | + assert proc.started_ids == ["span-0", "span-1", "span-2"] |
| 67 | + |
| 68 | + async def test_on_spans_start_logs_each_failure(self, caplog): |
| 69 | + proc = _RecordingProcessor(fail_ids={"span-0", "span-2"}) |
| 70 | + spans = [_make_span(f"span-{i}") for i in range(3)] |
| 71 | + |
| 72 | + with caplog.at_level(logging.ERROR): |
| 73 | + await proc.on_spans_start(spans) |
| 74 | + |
| 75 | + # Two distinct error log records, one per failing span |
| 76 | + error_records = [r for r in caplog.records if r.levelno == logging.ERROR] |
| 77 | + messages = " ".join(r.getMessage() for r in error_records) |
| 78 | + assert "span-0" in messages |
| 79 | + assert "span-2" in messages |
| 80 | + |
| 81 | + async def test_on_spans_end_runs_every_span_despite_failures(self, caplog): |
| 82 | + proc = _RecordingProcessor(fail_ids={"span-1"}) |
| 83 | + spans = [_make_span(f"span-{i}") for i in range(3)] |
| 84 | + |
| 85 | + with caplog.at_level(logging.ERROR): |
| 86 | + await proc.on_spans_end(spans) |
| 87 | + |
| 88 | + assert proc.ended_ids == ["span-0", "span-1", "span-2"] |
| 89 | + |
| 90 | + async def test_dummy_config_construction(self): |
| 91 | + """AsyncTracingProcessor's __init__ is abstract — verify concrete |
| 92 | + subclass above satisfies the interface.""" |
| 93 | + _ = TracingProcessorConfig |
| 94 | + proc = _RecordingProcessor() |
| 95 | + await proc.on_spans_start([]) |
| 96 | + await proc.on_spans_end([]) |
| 97 | + assert proc.started_ids == [] |
| 98 | + assert proc.ended_ids == [] |
0 commit comments