|
| 1 | +import agents |
| 2 | +from agents.tracing import add_trace_processor |
| 3 | + |
| 4 | +from ddtrace import config |
| 5 | +from ddtrace.contrib.internal.openai_agents.processor import LLMObsTraceProcessor |
| 6 | +from ddtrace.contrib.internal.openai_agents.processor import NoOpTraceProcessor |
| 7 | +from ddtrace.llmobs._integrations.openai_agents import OpenAIAgentsIntegration |
| 8 | +from ddtrace.trace import Pin |
| 9 | + |
| 10 | + |
| 11 | +config._add("openai_agents", {}) |
| 12 | + |
| 13 | +_span_processor = None |
| 14 | + |
| 15 | + |
| 16 | +def get_version() -> str: |
| 17 | + from agents import version |
| 18 | + |
| 19 | + return getattr(version, "__version__", "") |
| 20 | + |
| 21 | + |
| 22 | +def patch(): |
| 23 | + """ |
| 24 | + Patch the instrumented methods |
| 25 | + """ |
| 26 | + if getattr(agents, "_datadog_patch", False): |
| 27 | + return |
| 28 | + |
| 29 | + agents._datadog_patch = True |
| 30 | + |
| 31 | + global _span_processor |
| 32 | + |
| 33 | + Pin().onto(agents) |
| 34 | + |
| 35 | + _span_processor = LLMObsTraceProcessor( |
| 36 | + integration=OpenAIAgentsIntegration(integration_config=config.openai_agents), |
| 37 | + ) |
| 38 | + add_trace_processor(_span_processor) |
| 39 | + |
| 40 | + |
| 41 | +def unpatch(): |
| 42 | + """ |
| 43 | + Remove instrumentation from patched methods |
| 44 | + """ |
| 45 | + if not getattr(agents, "_datadog_patch", False): |
| 46 | + return |
| 47 | + |
| 48 | + # Since there's no public API to remove a trace processor, we set the instance |
| 49 | + # we added to a no-op instance |
| 50 | + global _span_processor |
| 51 | + if _span_processor is not None: |
| 52 | + _span_processor = NoOpTraceProcessor() |
| 53 | + |
| 54 | + agents._datadog_patch = False |
0 commit comments