|
24 | 24 | from agentops.logging import logger |
25 | 25 | from agentops.instrumentation.openai_agents.processor import OpenAIAgentsProcessor |
26 | 26 | from agentops.instrumentation.openai_agents.exporter import OpenAIAgentsExporter |
| 27 | +from agentops.instrumentation.openai_agents import LIBRARY_VERSION |
27 | 28 |
|
28 | 29 |
|
29 | 30 | class OpenAIAgentsInstrumentor(BaseInstrumentor): |
30 | 31 | """An instrumentor for OpenAI Agents SDK that primarily uses the built-in tracing API.""" |
31 | | - |
| 32 | + |
32 | 33 | _processor = None |
33 | 34 | _exporter = None |
34 | 35 | _default_processor = None |
35 | | - |
| 36 | + |
36 | 37 | def instrumentation_dependencies(self) -> Collection[str]: |
37 | 38 | """Return packages required for instrumentation.""" |
38 | 39 | return ["openai-agents >= 0.0.1"] |
39 | | - |
| 40 | + |
40 | 41 | def _instrument(self, **kwargs): |
41 | 42 | """Instrument the OpenAI Agents SDK.""" |
42 | 43 | tracer_provider = kwargs.get("tracer_provider") |
43 | | - |
| 44 | + |
44 | 45 | try: |
45 | 46 | # Check if Agents SDK is available |
46 | 47 | try: |
47 | 48 | import agents # type: ignore |
48 | | - logger.debug(f"Agents SDK detected, version: {getattr(agents, '__version__', 'unknown')}") |
| 49 | + |
| 50 | + logger.debug(f"OpenAI Agents SDK detected with version: {LIBRARY_VERSION}") |
49 | 51 | except ImportError as e: |
50 | | - logger.debug(f"Agents SDK import failed: {e}") |
| 52 | + logger.debug(f"OpenAI Agents SDK import failed: {e}") |
51 | 53 | return |
52 | | - |
| 54 | + |
53 | 55 | self._exporter = OpenAIAgentsExporter(tracer_provider=tracer_provider) |
54 | 56 | self._processor = OpenAIAgentsProcessor( |
55 | 57 | exporter=self._exporter, |
56 | 58 | ) |
57 | | - |
| 59 | + |
58 | 60 | # Replace the default processor with our processor |
59 | 61 | from agents import set_trace_processors # type: ignore |
60 | 62 | from agents.tracing.processors import default_processor # type: ignore |
| 63 | + |
61 | 64 | # Store reference to default processor for later restoration |
62 | 65 | self._default_processor = default_processor() |
63 | 66 | set_trace_processors([self._processor]) |
64 | 67 | logger.debug("Replaced default processor with OpenAIAgentsProcessor in OpenAI Agents SDK") |
65 | | - |
| 68 | + |
66 | 69 | except Exception as e: |
67 | 70 | logger.warning(f"Failed to instrument OpenAI Agents SDK: {e}") |
68 | | - |
| 71 | + |
69 | 72 | def _uninstrument(self, **kwargs): |
70 | 73 | """Remove instrumentation from OpenAI Agents SDK.""" |
71 | 74 | try: |
72 | 75 | # Clean up any active spans in the exporter |
73 | | - if hasattr(self, '_exporter') and self._exporter: |
| 76 | + if hasattr(self, "_exporter") and self._exporter: |
74 | 77 | # Call cleanup to properly handle any active spans |
75 | | - if hasattr(self._exporter, 'cleanup'): |
| 78 | + if hasattr(self._exporter, "cleanup"): |
76 | 79 | self._exporter.cleanup() |
77 | | - |
| 80 | + |
78 | 81 | # Put back the default processor |
79 | 82 | from agents import set_trace_processors |
80 | | - if hasattr(self, '_default_processor') and self._default_processor: |
| 83 | + |
| 84 | + if hasattr(self, "_default_processor") and self._default_processor: |
81 | 85 | set_trace_processors([self._default_processor]) |
82 | 86 | self._default_processor = None |
83 | 87 | self._processor = None |
84 | 88 | self._exporter = None |
85 | | - |
| 89 | + |
86 | 90 | logger.info("Successfully removed OpenAI Agents SDK instrumentation") |
87 | 91 | except Exception as e: |
88 | 92 | logger.warning(f"Failed to uninstrument OpenAI Agents SDK: {e}") |
0 commit comments