Skip to content

Commit 3e1e8ce

Browse files
committed
feat(openai-agents): add clear flag to support two instrumentation modes
1 parent 19d204f commit 3e1e8ce

File tree

1 file changed

+28
-3
lines changed
  • packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents

1 file changed

+28
-3
lines changed

packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,32 @@
1515
class OpenAIAgentsInstrumentor(BaseInstrumentor):
1616
"""An instrumentor for OpenAI Agents SDK."""
1717

18+
def __init__(self, *, clear: bool = False) -> None:
19+
"""Initialize the instrumentor.
20+
21+
Args:
22+
clear: If True, existing trace processors are dropped
23+
and this instrumentor's processor is set as the only one.
24+
This is useful for replacing the default OpenAI instrumentation
25+
(enabled by default) with this one.
26+
"""
27+
# base class BaseInstrumentor is an ABC without __init__
28+
self._clear: bool = clear
29+
1830
def instrumentation_dependencies(self) -> Collection[str]:
1931
return _instruments
2032

21-
def _instrument(self, **kwargs):
33+
def _instrument(self, **kwargs) -> None:
34+
"""Instruments OpenAI Agents SDK.
35+
36+
Args:
37+
tracer_provider: An optional TracerProvider to use
38+
when creating a Tracer.
39+
meter_provider: An optional MeterProvider to use
40+
when creating a Meter.
41+
42+
Additional kwargs are ignored.
43+
"""
2244
tracer_provider = kwargs.get("tracer_provider")
2345
tracer = get_tracer(__name__, __version__, tracer_provider)
2446

@@ -30,12 +52,15 @@ def _instrument(self, **kwargs):
3052

3153
# Use hook-based approach with OpenAI Agents SDK callbacks
3254
try:
33-
from agents import add_trace_processor
55+
from agents import add_trace_processor, set_trace_processors
3456
from ._hooks import OpenTelemetryTracingProcessor
3557

3658
# Create and add our OpenTelemetry processor
3759
otel_processor = OpenTelemetryTracingProcessor(tracer)
38-
add_trace_processor(otel_processor)
60+
if self._clear:
61+
set_trace_processors([otel_processor])
62+
else:
63+
add_trace_processor(otel_processor)
3964

4065
except Exception:
4166
# Silently handle import errors - OpenAI Agents SDK may not be available

0 commit comments

Comments
 (0)