Skip to content

Commit 5535cd3

Browse files
committed
Restore GenAI semantic processor instrumentation
1 parent 9e20abb commit 5535cd3

File tree

2 files changed

+10
-71
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2

2 files changed

+10
-71
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/__init__.py

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
# Copyright The OpenTelemetry Authors
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
151
"""OpenAI Agents instrumentation for OpenTelemetry."""
162

173
from __future__ import annotations
184

5+
import importlib
6+
import logging
197
import os
20-
from typing import Collection, Protocol
21-
22-
from agents import tracing
23-
from agents.tracing.processor_interface import TracingProcessor
8+
from typing import Any, Collection
249

10+
from opentelemetry._events import get_event_logger
2511
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
2612
from opentelemetry.semconv._incubating.attributes import (
2713
gen_ai_attributes as GenAI,
@@ -142,46 +128,6 @@ class OpenAIAgentsInstrumentor(BaseInstrumentor):
142128
def __init__(self) -> None:
143129
super().__init__()
144130
self._processor: GenAISemanticProcessor | None = None
145-
from .package import _instruments
146-
from .span_processor import _OpenAIAgentsSpanProcessor
147-
from .version import __version__ # noqa: F401
148-
149-
150-
class _ProcessorHolder(Protocol):
151-
_processors: Collection[TracingProcessor]
152-
153-
154-
class _TraceProviderLike(Protocol):
155-
_multi_processor: _ProcessorHolder
156-
157-
158-
__all__ = ["OpenAIAgentsInstrumentor"]
159-
160-
161-
def _resolve_system(_: str | None) -> str:
162-
# OpenAI spans must report provider name "openai" per semantic conventions.
163-
return GenAI.GenAiSystemValues.OPENAI.value
164-
165-
166-
def _get_registered_processors(
167-
provider: _TraceProviderLike,
168-
) -> list[TracingProcessor]:
169-
"""Return tracing processors registered on the OpenAI Agents trace provider.
170-
171-
The provider exposes a private `_multi_processor` attribute with a `_processors`
172-
collection that stores the currently registered processors in execution order.
173-
"""
174-
multi = getattr(provider, "_multi_processor", None)
175-
processors = getattr(multi, "_processors", ())
176-
return list(processors)
177-
178-
179-
class OpenAIAgentsInstrumentor(BaseInstrumentor):
180-
"""Instrumentation that bridges OpenAI Agents tracing to OpenTelemetry spans."""
181-
182-
def __init__(self) -> None:
183-
super().__init__()
184-
self._processor: _OpenAIAgentsSpanProcessor | None = None
185131

186132
def _instrument(self, **kwargs) -> None:
187133
if self._processor is not None:
@@ -237,17 +183,6 @@ def _instrument(self, **kwargs) -> None:
237183
)
238184

239185
tracing = _load_tracing_module()
240-
system = _resolve_system(kwargs.get("system"))
241-
agent_name_override = kwargs.get("agent_name") or os.getenv(
242-
"OTEL_GENAI_AGENT_NAME"
243-
)
244-
245-
processor = _OpenAIAgentsSpanProcessor(
246-
tracer=tracer,
247-
system=system,
248-
agent_name_override=agent_name_override,
249-
)
250-
251186
provider = tracing.get_trace_provider()
252187
existing = _get_registered_processors(provider)
253188
provider.set_processors([*existing, processor])
@@ -267,8 +202,6 @@ def _uninstrument(self, **kwargs) -> None:
267202
self._processor.shutdown()
268203
finally:
269204
self._processor = None
270-
self._processor.shutdown()
271-
self._processor = None
272205

273206
def instrumentation_dependencies(self) -> Collection[str]:
274207
return _instruments

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/test_tracer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
from opentelemetry.trace import SpanKind # noqa: E402
4949

5050
GEN_AI_PROVIDER_NAME = GenAI.GEN_AI_PROVIDER_NAME
51+
GEN_AI_INPUT_MESSAGES = getattr(
52+
GenAI, "GEN_AI_INPUT_MESSAGES", "gen_ai.input.messages"
53+
)
54+
GEN_AI_OUTPUT_MESSAGES = getattr(
55+
GenAI, "GEN_AI_OUTPUT_MESSAGES", "gen_ai.output.messages"
56+
)
5157

5258

5359
def _instrument_with_provider(**instrument_kwargs):

0 commit comments

Comments
 (0)