Skip to content

Commit 4658a0e

Browse files
fix(tracing): preserve exporter overrides during shutdown (#5199)
* fix(tracing): preserve exporter overrides during shutdown * fix(tracing): retain deadlines for inherited backend export
1 parent e7d7097 commit 4658a0e

2 files changed

Lines changed: 92 additions & 19 deletions

File tree

‎src/agents/tracing/processors.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import random
88
import threading
99
import time
10-
from collections.abc import Callable, Mapping, Sequence
10+
from collections.abc import Mapping, Sequence
1111
from functools import cached_property
12-
from typing import Any, cast
12+
from typing import Any
1313

1414
import httpx2
1515

@@ -838,13 +838,15 @@ def _export_batches(self, deadline: float | None = None):
838838
# cannot kill the background worker thread and silently strand all
839839
# subsequent spans in the queue.
840840
try:
841-
export_with_deadline = getattr(self._exporter, "_export_with_deadline", None)
842-
if deadline is not None and callable(export_with_deadline):
843-
export_fn = cast(
844-
Callable[[list[Trace | Span[Any]], float | None], None],
845-
export_with_deadline,
846-
)
847-
export_fn(items_to_export, deadline)
841+
# Preserve deadlines for inherited backend export behavior, but never
842+
# bypass an application's public export override.
843+
if (
844+
deadline is not None
845+
and isinstance(self._exporter, BackendSpanExporter)
846+
and getattr(self._exporter.export, "__func__", None)
847+
is BackendSpanExporter.export
848+
):
849+
self._exporter._export_with_deadline(items_to_export, deadline)
848850
else:
849851
self._exporter.export(items_to_export)
850852
except Exception as exc:

‎tests/test_trace_processor.py‎

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616

1717
import agents._debug as _debug
18-
from agents.tracing import flush_traces, get_trace_provider
18+
from agents.tracing import flush_traces, get_trace_provider, setup as tracing_setup
1919
from agents.tracing.processor_interface import TracingExporter, TracingProcessor
2020
from agents.tracing.processors import BackendSpanExporter, BatchTraceProcessor, ConsoleSpanExporter
2121
from agents.tracing.provider import DefaultTraceProvider, TraceProvider
@@ -271,40 +271,111 @@ def export(self, items: list[Trace | Span[Any]]) -> None:
271271
assert not processor._worker_thread.is_alive()
272272

273273

274-
def test_batch_trace_processor_shutdown_passes_deadline_to_exporter() -> None:
275-
seen_deadlines: list[float | None] = []
274+
def test_batch_trace_processor_shutdown_uses_custom_exporter_public_method() -> None:
275+
exported: list[Trace | Span[Any]] = []
276276

277277
class DeadlineExporter(TracingExporter):
278278
def export(self, items: list[Trace | Span[Any]]) -> None:
279-
raise AssertionError("shutdown should use the deadline-aware exporter path")
279+
exported.extend(items)
280280

281281
def _export_with_deadline(
282282
self, items: list[Trace | Span[Any]], deadline: float | None
283283
) -> None:
284-
seen_deadlines.append(deadline)
284+
raise AssertionError("private deadline helpers are not an exporter extension API")
285285

286286
processor = BatchTraceProcessor(exporter=DeadlineExporter())
287-
processor._queue.put_nowait(get_span(processor))
287+
span = get_span(processor)
288+
processor._queue.put_nowait(span)
288289

289290
processor.shutdown(timeout=1.0)
290291

291-
assert len(seen_deadlines) == 1
292-
assert seen_deadlines[0] is not None
292+
assert exported == [span]
293+
294+
295+
@pytest.mark.parametrize("cleanup", ["flush", "shutdown", "timed_shutdown", "automatic"])
296+
@pytest.mark.parametrize("reject_batch", [False, True], ids=["filter", "raise"])
297+
def test_batch_trace_processor_cleanup_preserves_exporter_filter(
298+
monkeypatch: pytest.MonkeyPatch, cleanup: str, reject_batch: bool
299+
) -> None:
300+
received: list[dict[str, Any]] = []
301+
filtered_batches: list[list[Trace | Span[Any]]] = []
302+
303+
def handle_request(request: httpx2.Request) -> httpx2.Response:
304+
received.extend(json.loads(request.content)["data"])
305+
return httpx2.Response(200)
306+
307+
class FilteringExporter(BackendSpanExporter):
308+
def export(self, items: list[Trace | Span[Any]]) -> None:
309+
filtered_batches.append(items)
310+
if reject_batch:
311+
raise ValueError("batch rejected by application policy")
312+
super().export([item for item in items if isinstance(item, Trace)])
313+
314+
exporter = FilteringExporter(api_key="test_key")
315+
exporter._client.close()
316+
exporter._client = httpx2.Client(transport=httpx2.MockTransport(handle_request))
317+
processor = BatchTraceProcessor(exporter=exporter)
318+
# Keep the batch queued until the chosen cleanup entry point drains it.
319+
monkeypatch.setattr(processor, "_ensure_thread_started", lambda: None)
320+
allowed_trace = get_trace(processor)
321+
sensitive_span = SpanImpl(
322+
trace_id=allowed_trace.trace_id,
323+
span_id="test_sensitive_span",
324+
parent_id=None,
325+
processor=processor,
326+
span_data=FunctionSpanData(
327+
name="test_tool", input="SYNTHETIC_PRIVATE_INPUT", output="SYNTHETIC_PRIVATE_OUTPUT"
328+
),
329+
tracing_api_key=None,
330+
)
331+
processor.on_trace_start(allowed_trace)
332+
processor.on_span_end(sensitive_span)
333+
try:
334+
if cleanup == "automatic":
335+
provider = DefaultTraceProvider()
336+
provider.register_processor(processor)
337+
monkeypatch.setattr(tracing_setup, "GLOBAL_TRACE_PROVIDER", provider)
338+
tracing_setup._shutdown_global_trace_provider()
339+
elif cleanup == "timed_shutdown":
340+
processor.shutdown(timeout=1.0)
341+
elif cleanup == "shutdown":
342+
processor.shutdown()
343+
else:
344+
processor.force_flush()
345+
346+
assert received == ([] if reject_batch else [allowed_trace.export()])
347+
assert "SYNTHETIC_PRIVATE_INPUT" not in json.dumps(received)
348+
assert "SYNTHETIC_PRIVATE_OUTPUT" not in json.dumps(received)
349+
assert filtered_batches == [[allowed_trace, sensitive_span]]
350+
finally:
351+
processor.shutdown()
352+
exporter.close()
293353

294354

355+
@pytest.mark.parametrize("use_subclass", [False, True], ids=["default", "inherited-export"])
295356
@patch("httpx2.Client")
296-
def test_batch_trace_processor_timed_shutdown_retries_final_drain(mock_client) -> None:
357+
def test_batch_trace_processor_timed_shutdown_retries_final_drain(
358+
mock_client, use_subclass: bool
359+
) -> None:
360+
class InheritedExportBackend(BackendSpanExporter):
361+
pass
362+
297363
transient = MagicMock(status_code=503, headers={})
298364
success = MagicMock(status_code=200, headers={})
299365
mock_client.return_value.post.side_effect = [transient, success]
300366

301-
exporter = BackendSpanExporter(api_key="test_key", max_retries=2, base_delay=0.001)
367+
exporter_type = InheritedExportBackend if use_subclass else BackendSpanExporter
368+
exporter = exporter_type(api_key="test_key", max_retries=2, base_delay=0.001)
302369
processor = BatchTraceProcessor(exporter=exporter)
303370
processor._queue.put_nowait(get_span(processor))
304371

305372
processor.shutdown(timeout=1.0)
306373

307374
assert mock_client.return_value.post.call_count == 2
375+
for request in mock_client.return_value.post.call_args_list:
376+
timeout = request.kwargs["timeout"]
377+
assert isinstance(timeout, httpx2.Timeout)
378+
assert timeout.read is not None and 0 < timeout.read <= 1.0
308379
exporter.close()
309380

310381

0 commit comments

Comments
 (0)