Skip to content

Commit 8a0fece

Browse files
committed
feat(openai-agents): add clear flag to support two instrumentation modes
1 parent f782741 commit 8a0fece

File tree

2 files changed

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

2 files changed

+30
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ node_modules
3131
/libpeerconnection.log
3232
npm-debug.log
3333
yarn-error.log
34+
.yarn/
3435
testem.log
3536
/typings
3637

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,33 @@
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: Optional bool and default to False.
23+
If True, existing trace processors are dropped
24+
and this instrumentor's processor is set as the only one.
25+
This is useful for replacing the default OpenAI instrumentation
26+
(enabled by default) with this one.
27+
"""
28+
# base class BaseInstrumentor is an ABC without __init__
29+
self._clear: bool = clear
30+
1831
def instrumentation_dependencies(self) -> Collection[str]:
1932
return _instruments
2033

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

@@ -30,12 +53,15 @@ def _instrument(self, **kwargs):
3053

3154
# Use hook-based approach with OpenAI Agents SDK callbacks
3255
try:
33-
from agents import add_trace_processor
56+
from agents import add_trace_processor, set_trace_processors
3457
from ._hooks import OpenTelemetryTracingProcessor
3558

3659
# Create and add our OpenTelemetry processor
3760
otel_processor = OpenTelemetryTracingProcessor(tracer)
38-
add_trace_processor(otel_processor)
61+
if self._clear:
62+
set_trace_processors([otel_processor])
63+
else:
64+
add_trace_processor(otel_processor)
3965

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

0 commit comments

Comments
 (0)