Skip to content

Commit 7656fc8

Browse files
lzchenlmolkova
andauthored
OpenAI Instrumentation: Switch to Latest GenAI SemConv Path (chat) (#108)
* genai * Update instrumentation/opentelemetry-instrumentation-genai-openai/tests/conftest.py Co-authored-by: Liudmila Molkova <neskazu@gmail.com> --------- Co-authored-by: Liudmila Molkova <neskazu@gmail.com>
1 parent 0b38ca7 commit 7656fc8

4 files changed

Lines changed: 17 additions & 500 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Switch OpenAI instrumentation and tests to the latest GenAI semantic convention mode.

instrumentation/opentelemetry-instrumentation-genai-openai/src/opentelemetry/instrumentation/genai/openai/__init__.py

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,14 @@
2828
Configuration
2929
-------------
3030
31-
By default, the instrumentation aligns with `Semantic Conventions v1.30.0
32-
<https://github.com/open-telemetry/semantic-conventions/tree/v1.30.0/docs/gen-ai>`_
33-
and does not capture prompt or completion content. Behavior is controlled
34-
via environment variables:
35-
36-
- ``OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental`` - opt into the
37-
latest GenAI semantic conventions. Required to access the newer attributes
38-
and the ``span_only`` / ``event_only`` / ``span_and_event`` content modes.
39-
Without this flag, the instrumentation stays on v1.30.0 conventions.
31+
This instrumentation emits telemetry using the latest GenAI semantic
32+
conventions and does not capture prompt or completion content by default.
33+
Behavior is controlled via environment variables:
34+
4035
- ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`` - enable capture of
41-
prompts, completions, tool arguments, and return values. Set to ``true``
42-
on the legacy path, or one of ``span_only``, ``event_only``,
43-
``span_and_event`` when experimental conventions are enabled.
36+
prompts, completions, tool arguments, and return values. Supported values
37+
are ``span_only``, ``event_only``, and ``span_and_event``. This requires
38+
``OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental``.
4439
- ``OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload`` together with
4540
``OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH=<fsspec-uri>`` - upload
4641
prompts and completions to an ``fsspec``-compatible destination
@@ -69,9 +64,7 @@
6964

7065
from wrapt import wrap_function_wrapper
7166

72-
from opentelemetry._logs import get_logger
7367
from opentelemetry.instrumentation.genai.openai.package import _instruments
74-
from opentelemetry.instrumentation.genai.openai.utils import is_content_enabled
7568
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7669
from opentelemetry.instrumentation.utils import unwrap
7770
from opentelemetry.metrics import get_meter
@@ -81,15 +74,12 @@
8174
from opentelemetry.util.genai.handler import (
8275
TelemetryHandler,
8376
)
84-
from opentelemetry.util.genai.utils import is_experimental_mode
8577

8678
from .instruments import Instruments
8779
from .patch import (
8880
async_chat_completions_create_v_new,
89-
async_chat_completions_create_v_old,
9081
async_embeddings_create,
9182
chat_completions_create_v_new,
92-
chat_completions_create_v_old,
9383
embeddings_create,
9484
)
9585
from .patch_responses import (
@@ -124,7 +114,6 @@ def instrumentation_dependencies(self) -> Collection[str]:
124114
def _instrument(self, **kwargs):
125115
"""Enable OpenAI instrumentation."""
126116

127-
latest_experimental_enabled = is_experimental_mode()
128117
tracer_provider = kwargs.get("tracer_provider")
129118
tracer = get_tracer(
130119
__name__,
@@ -133,12 +122,6 @@ def _instrument(self, **kwargs):
133122
schema_url=Schemas.V1_30_0.value, # only used on the legacy path
134123
)
135124
logger_provider = kwargs.get("logger_provider")
136-
logger = get_logger(
137-
__name__,
138-
"",
139-
logger_provider=logger_provider,
140-
schema_url=Schemas.V1_30_0.value, # only used on the legacy path
141-
)
142125
meter_provider = kwargs.get("meter_provider")
143126
self._meter = get_meter(
144127
__name__,
@@ -160,42 +143,26 @@ def _instrument(self, **kwargs):
160143
wrap_function_wrapper(
161144
"openai.resources.chat.completions",
162145
"Completions.create",
163-
(
164-
chat_completions_create_v_new(handler)
165-
if latest_experimental_enabled
166-
else chat_completions_create_v_old(
167-
tracer, logger, instruments, is_content_enabled()
168-
)
169-
),
146+
chat_completions_create_v_new(handler),
170147
)
171148

172149
wrap_function_wrapper(
173150
"openai.resources.chat.completions",
174151
"AsyncCompletions.create",
175-
(
176-
async_chat_completions_create_v_new(handler)
177-
if latest_experimental_enabled
178-
else async_chat_completions_create_v_old(
179-
tracer, logger, instruments, is_content_enabled()
180-
)
181-
),
152+
async_chat_completions_create_v_new(handler),
182153
)
183154

184155
# Add instrumentation for the embeddings API
185156
wrap_function_wrapper(
186157
"openai.resources.embeddings",
187158
"Embeddings.create",
188-
embeddings_create(
189-
tracer, instruments, latest_experimental_enabled
190-
),
159+
embeddings_create(tracer, instruments, True),
191160
)
192161

193162
wrap_function_wrapper(
194163
"openai.resources.embeddings",
195164
"AsyncEmbeddings.create",
196-
async_embeddings_create(
197-
tracer, instruments, latest_experimental_enabled
198-
),
165+
async_embeddings_create(tracer, instruments, True),
199166
)
200167

201168
# parse() wraps create() internally in the OpenAI SDK and returns a
@@ -207,34 +174,17 @@ def _instrument(self, **kwargs):
207174
wrap_function_wrapper(
208175
"openai.resources.chat.completions",
209176
"Completions.parse",
210-
(
211-
chat_completions_create_v_new(handler)
212-
if latest_experimental_enabled
213-
else chat_completions_create_v_old(
214-
tracer, logger, instruments, is_content_enabled()
215-
)
216-
),
177+
chat_completions_create_v_new(handler),
217178
)
218179

219180
wrap_function_wrapper(
220181
"openai.resources.chat.completions",
221182
"AsyncCompletions.parse",
222-
(
223-
async_chat_completions_create_v_new(handler)
224-
if latest_experimental_enabled
225-
else async_chat_completions_create_v_old(
226-
tracer, logger, instruments, is_content_enabled()
227-
)
228-
),
183+
async_chat_completions_create_v_new(handler),
229184
)
230185

231186
responses_module = _get_responses_module()
232-
# Responses instrumentation is intentionally limited to the latest
233-
# experimental semconv path. Unlike chat completions, we do not carry
234-
# a second legacy wrapper here; the current implementation is built on
235-
# the inference handler lifecycle and would need a separate old-path
236-
# implementation to support legacy semconv mode.
237-
if responses_module is not None and latest_experimental_enabled:
187+
if responses_module is not None:
238188
wrap_function_wrapper(
239189
"openai.resources.responses.responses",
240190
"Responses.create",

0 commit comments

Comments
 (0)