|
1 | 1 | """Hook-based instrumentation for OpenAI Agents using the SDK's native callback system.""" |
2 | 2 |
|
3 | | -from typing import Dict, Any |
| 3 | +from typing import Dict, Any, Final |
4 | 4 | import json |
5 | 5 | import time |
6 | 6 | from collections import OrderedDict |
|
11 | 11 | gen_ai_attributes as GenAIAttributes |
12 | 12 | ) |
13 | 13 | from agents.tracing.processors import TracingProcessor |
14 | | -from .utils import dont_throw |
| 14 | +from .utils import dont_throw, should_send_prompts |
15 | 15 |
|
16 | 16 | from traceloop.sdk.tracing import set_agent_name |
17 | 17 |
|
@@ -233,6 +233,7 @@ def on_span_end(self, span): |
233 | 233 | if span in self._otel_spans: |
234 | 234 | otel_span = self._otel_spans[span] |
235 | 235 | span_data = getattr(span, 'span_data', None) |
| 236 | + should_trace_content: Final[bool] = should_send_prompts() |
236 | 237 | if span_data and ( |
237 | 238 | type(span_data).__name__ == 'ResponseSpanData' or isinstance( |
238 | 239 | span_data, |
@@ -298,7 +299,7 @@ def on_span_end(self, span): |
298 | 299 | otel_span.set_attribute(f"{prefix}.role", role) |
299 | 300 |
|
300 | 301 | # Set content attribute |
301 | | - if content is not None: |
| 302 | + if should_trace_content and content is not None: |
302 | 303 | if not isinstance(content, str): |
303 | 304 | content = json.dumps(content) |
304 | 305 | otel_span.set_attribute(f"{prefix}.content", content) |
@@ -399,7 +400,7 @@ def on_span_end(self, span): |
399 | 400 | if hasattr(response, 'output') and response.output: |
400 | 401 | for i, output in enumerate(response.output): |
401 | 402 | # Handle different output types |
402 | | - if hasattr(output, 'content') and output.content: |
| 403 | + if should_trace_content and hasattr(output, 'content') and output.content: |
403 | 404 | # Text message with content array (ResponseOutputMessage) |
404 | 405 | content_text = "" |
405 | 406 | for content_item in output.content: |
@@ -430,7 +431,7 @@ def on_span_end(self, span): |
430 | 431 | otel_span.set_attribute( |
431 | 432 | f"{GenAIAttributes.GEN_AI_COMPLETION}.{i}.tool_calls.0.id", tool_call_id) |
432 | 433 |
|
433 | | - elif hasattr(output, 'text'): |
| 434 | + elif should_trace_content and hasattr(output, 'text'): |
434 | 435 | # Direct text content |
435 | 436 | otel_span.set_attribute(f"{GenAIAttributes.GEN_AI_COMPLETION}.{i}.content", output.text) |
436 | 437 | otel_span.set_attribute( |
@@ -466,7 +467,7 @@ def on_span_end(self, span): |
466 | 467 | elif span_data: |
467 | 468 | # Extract prompt data from input and add to response span (legacy support) |
468 | 469 | input_data = getattr(span_data, 'input', []) |
469 | | - if input_data: |
| 470 | + if should_trace_content and input_data: |
470 | 471 | for i, message in enumerate(input_data): |
471 | 472 | if hasattr(message, 'role') and hasattr(message, 'content'): |
472 | 473 | otel_span.set_attribute(f"gen_ai.prompt.{i}.role", message.role) |
@@ -508,7 +509,7 @@ def on_span_end(self, span): |
508 | 509 | if hasattr(response, 'output') and response.output: |
509 | 510 | for i, output in enumerate(response.output): |
510 | 511 | # Handle different output types |
511 | | - if hasattr(output, 'content') and output.content: |
| 512 | + if should_trace_content and hasattr(output, 'content') and output.content: |
512 | 513 | # Text message with content array (ResponseOutputMessage) |
513 | 514 | content_text = "" |
514 | 515 | for content_item in output.content: |
@@ -539,7 +540,7 @@ def on_span_end(self, span): |
539 | 540 | otel_span.set_attribute( |
540 | 541 | f"{GenAIAttributes.GEN_AI_COMPLETION}.{i}.tool_calls.0.id", tool_call_id) |
541 | 542 |
|
542 | | - elif hasattr(output, 'text'): |
| 543 | + elif should_trace_content and hasattr(output, 'text'): |
543 | 544 | # Direct text content |
544 | 545 | otel_span.set_attribute(f"{GenAIAttributes.GEN_AI_COMPLETION}.{i}.content", output.text) |
545 | 546 | otel_span.set_attribute( |
|
0 commit comments