|
1 | 1 | import os |
2 | 2 | import time |
| 3 | +from typing import Any, cast |
3 | 4 | from unittest.mock import MagicMock, patch |
4 | 5 |
|
5 | 6 | import httpx |
@@ -276,3 +277,147 @@ def test_backend_span_exporter_close(mock_client): |
276 | 277 |
|
277 | 278 | # Ensure underlying http client is closed |
278 | 279 | mock_client.return_value.close.assert_called_once() |
| 280 | + |
| 281 | + |
| 282 | +@patch("httpx.Client") |
| 283 | +def test_backend_span_exporter_sanitizes_generation_usage_for_openai_tracing(mock_client): |
| 284 | + """Unsupported usage keys should be stripped before POSTing to OpenAI tracing.""" |
| 285 | + |
| 286 | + class DummyItem: |
| 287 | + tracing_api_key = None |
| 288 | + |
| 289 | + def __init__(self): |
| 290 | + self.exported_payload: dict[str, Any] = { |
| 291 | + "object": "trace.span", |
| 292 | + "span_data": { |
| 293 | + "type": "generation", |
| 294 | + "usage": { |
| 295 | + "requests": 1, |
| 296 | + "input_tokens": 10, |
| 297 | + "output_tokens": 5, |
| 298 | + "total_tokens": 15, |
| 299 | + "input_tokens_details": {"cached_tokens": 1}, |
| 300 | + "output_tokens_details": {"reasoning_tokens": 2}, |
| 301 | + }, |
| 302 | + }, |
| 303 | + } |
| 304 | + |
| 305 | + def export(self): |
| 306 | + return self.exported_payload |
| 307 | + |
| 308 | + mock_response = MagicMock() |
| 309 | + mock_response.status_code = 200 |
| 310 | + mock_client.return_value.post.return_value = mock_response |
| 311 | + |
| 312 | + exporter = BackendSpanExporter(api_key="test_key") |
| 313 | + item = DummyItem() |
| 314 | + exporter.export([cast(Any, item)]) |
| 315 | + |
| 316 | + sent_payload = mock_client.return_value.post.call_args.kwargs["json"]["data"][0] |
| 317 | + sent_usage = sent_payload["span_data"]["usage"] |
| 318 | + assert "requests" not in sent_usage |
| 319 | + assert sent_usage["input_tokens"] == 10 |
| 320 | + assert sent_usage["output_tokens"] == 5 |
| 321 | + assert sent_usage["total_tokens"] == 15 |
| 322 | + assert sent_usage["input_tokens_details"] == {"cached_tokens": 1} |
| 323 | + assert sent_usage["output_tokens_details"] == {"reasoning_tokens": 2} |
| 324 | + |
| 325 | + # Ensure the original exported object has not been mutated. |
| 326 | + assert "requests" in item.exported_payload["span_data"]["usage"] |
| 327 | + exporter.close() |
| 328 | + |
| 329 | + |
| 330 | +@patch("httpx.Client") |
| 331 | +def test_backend_span_exporter_keeps_generation_usage_for_custom_endpoint(mock_client): |
| 332 | + class DummyItem: |
| 333 | + tracing_api_key = None |
| 334 | + |
| 335 | + def __init__(self): |
| 336 | + self.exported_payload = { |
| 337 | + "object": "trace.span", |
| 338 | + "span_data": { |
| 339 | + "type": "generation", |
| 340 | + "usage": { |
| 341 | + "requests": 1, |
| 342 | + "input_tokens": 10, |
| 343 | + "output_tokens": 5, |
| 344 | + }, |
| 345 | + }, |
| 346 | + } |
| 347 | + |
| 348 | + def export(self): |
| 349 | + return self.exported_payload |
| 350 | + |
| 351 | + mock_response = MagicMock() |
| 352 | + mock_response.status_code = 200 |
| 353 | + mock_client.return_value.post.return_value = mock_response |
| 354 | + |
| 355 | + exporter = BackendSpanExporter( |
| 356 | + api_key="test_key", |
| 357 | + endpoint="https://example.com/v1/traces/ingest", |
| 358 | + ) |
| 359 | + exporter.export([cast(Any, DummyItem())]) |
| 360 | + |
| 361 | + sent_payload = mock_client.return_value.post.call_args.kwargs["json"]["data"][0] |
| 362 | + assert sent_payload["span_data"]["usage"]["requests"] == 1 |
| 363 | + assert sent_payload["span_data"]["usage"]["input_tokens"] == 10 |
| 364 | + assert sent_payload["span_data"]["usage"]["output_tokens"] == 5 |
| 365 | + exporter.close() |
| 366 | + |
| 367 | + |
| 368 | +@patch("httpx.Client") |
| 369 | +def test_backend_span_exporter_does_not_modify_non_generation_usage(mock_client): |
| 370 | + class DummyItem: |
| 371 | + tracing_api_key = None |
| 372 | + |
| 373 | + def export(self): |
| 374 | + return { |
| 375 | + "object": "trace.span", |
| 376 | + "span_data": { |
| 377 | + "type": "function", |
| 378 | + "usage": {"requests": 1}, |
| 379 | + }, |
| 380 | + } |
| 381 | + |
| 382 | + mock_response = MagicMock() |
| 383 | + mock_response.status_code = 200 |
| 384 | + mock_client.return_value.post.return_value = mock_response |
| 385 | + |
| 386 | + exporter = BackendSpanExporter(api_key="test_key") |
| 387 | + exporter.export([cast(Any, DummyItem())]) |
| 388 | + |
| 389 | + sent_payload = mock_client.return_value.post.call_args.kwargs["json"]["data"][0] |
| 390 | + assert sent_payload["span_data"]["usage"] == {"requests": 1} |
| 391 | + exporter.close() |
| 392 | + |
| 393 | + |
| 394 | +def test_sanitize_for_openai_tracing_api_keeps_allowed_generation_usage(): |
| 395 | + exporter = BackendSpanExporter(api_key="test_key") |
| 396 | + payload = { |
| 397 | + "object": "trace.span", |
| 398 | + "span_data": { |
| 399 | + "type": "generation", |
| 400 | + "usage": { |
| 401 | + "input_tokens": 1, |
| 402 | + "output_tokens": 2, |
| 403 | + "total_tokens": 3, |
| 404 | + "input_tokens_details": {"cached_tokens": 0}, |
| 405 | + "output_tokens_details": {"reasoning_tokens": 0}, |
| 406 | + }, |
| 407 | + }, |
| 408 | + } |
| 409 | + assert exporter._sanitize_for_openai_tracing_api(payload) is payload |
| 410 | + exporter.close() |
| 411 | + |
| 412 | + |
| 413 | +def test_sanitize_for_openai_tracing_api_skips_non_dict_generation_usage(): |
| 414 | + exporter = BackendSpanExporter(api_key="test_key") |
| 415 | + payload = { |
| 416 | + "object": "trace.span", |
| 417 | + "span_data": { |
| 418 | + "type": "generation", |
| 419 | + "usage": None, |
| 420 | + }, |
| 421 | + } |
| 422 | + assert exporter._sanitize_for_openai_tracing_api(payload) is payload |
| 423 | + exporter.close() |
0 commit comments