|
15 | 15 | import pytest |
16 | 16 |
|
17 | 17 | 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 |
19 | 19 | from agents.tracing.processor_interface import TracingExporter, TracingProcessor |
20 | 20 | from agents.tracing.processors import BackendSpanExporter, BatchTraceProcessor, ConsoleSpanExporter |
21 | 21 | from agents.tracing.provider import DefaultTraceProvider, TraceProvider |
@@ -271,40 +271,111 @@ def export(self, items: list[Trace | Span[Any]]) -> None: |
271 | 271 | assert not processor._worker_thread.is_alive() |
272 | 272 |
|
273 | 273 |
|
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]] = [] |
276 | 276 |
|
277 | 277 | class DeadlineExporter(TracingExporter): |
278 | 278 | def export(self, items: list[Trace | Span[Any]]) -> None: |
279 | | - raise AssertionError("shutdown should use the deadline-aware exporter path") |
| 279 | + exported.extend(items) |
280 | 280 |
|
281 | 281 | def _export_with_deadline( |
282 | 282 | self, items: list[Trace | Span[Any]], deadline: float | None |
283 | 283 | ) -> None: |
284 | | - seen_deadlines.append(deadline) |
| 284 | + raise AssertionError("private deadline helpers are not an exporter extension API") |
285 | 285 |
|
286 | 286 | processor = BatchTraceProcessor(exporter=DeadlineExporter()) |
287 | | - processor._queue.put_nowait(get_span(processor)) |
| 287 | + span = get_span(processor) |
| 288 | + processor._queue.put_nowait(span) |
288 | 289 |
|
289 | 290 | processor.shutdown(timeout=1.0) |
290 | 291 |
|
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() |
293 | 353 |
|
294 | 354 |
|
| 355 | +@pytest.mark.parametrize("use_subclass", [False, True], ids=["default", "inherited-export"]) |
295 | 356 | @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 | + |
297 | 363 | transient = MagicMock(status_code=503, headers={}) |
298 | 364 | success = MagicMock(status_code=200, headers={}) |
299 | 365 | mock_client.return_value.post.side_effect = [transient, success] |
300 | 366 |
|
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) |
302 | 369 | processor = BatchTraceProcessor(exporter=exporter) |
303 | 370 | processor._queue.put_nowait(get_span(processor)) |
304 | 371 |
|
305 | 372 | processor.shutdown(timeout=1.0) |
306 | 373 |
|
307 | 374 | 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 |
308 | 379 | exporter.close() |
309 | 380 |
|
310 | 381 |
|
|
0 commit comments