14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- import json
18
17
import logging
19
18
import os
20
19
from timeit import default_timer
26
25
from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
27
26
from opentelemetry .instrumentation .utils import unwrap
28
27
from opentelemetry .instrumentation .openai .environment_variables import (
29
- ELASTIC_OTEL_GENAI_EVENTS ,
30
28
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT ,
31
29
)
32
30
from opentelemetry .instrumentation .openai .helpers import (
33
31
_get_embeddings_span_attributes_from_wrapper ,
34
32
_get_event_attributes ,
35
33
_get_span_attributes_from_wrapper ,
36
- _message_from_choice ,
37
34
_record_token_usage_metrics ,
38
35
_record_operation_duration_metric ,
39
36
_send_log_events_from_messages ,
46
43
from opentelemetry .instrumentation .openai .version import __version__
47
44
from opentelemetry .instrumentation .openai .wrappers import StreamWrapper
48
45
from opentelemetry .metrics import get_meter
49
- from opentelemetry .semconv ._incubating .attributes .gen_ai_attributes import (
50
- GEN_AI_COMPLETION ,
51
- GEN_AI_PROMPT ,
52
- )
53
46
from opentelemetry .semconv ._incubating .metrics .gen_ai_metrics import (
54
47
create_gen_ai_client_token_usage ,
55
48
create_gen_ai_client_operation_duration ,
@@ -86,13 +79,6 @@ def _instrument(self, **kwargs):
86
79
== "true"
87
80
)
88
81
89
- # we support 3 values for deciding how to send events:
90
- # - "latest" to match latest semconv, as 1.28.0 it's log
91
- # - "log" to send log events (default)
92
- # - "span" to send span events
93
- genai_events = os .environ .get (ELASTIC_OTEL_GENAI_EVENTS , "latest" ).lower ()
94
- self .event_kind = "span" if genai_events == "span" else "log"
95
-
96
82
tracer_provider = kwargs .get ("tracer_provider" )
97
83
self .tracer = get_tracer (
98
84
__name__ ,
@@ -165,13 +151,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
165
151
if self .capture_message_content :
166
152
messages = kwargs .get ("messages" , [])
167
153
168
- if self .event_kind == "log" :
169
- _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
170
- elif span .is_recording ():
171
- try :
172
- span .add_event (EVENT_GEN_AI_CONTENT_PROMPT , attributes = {GEN_AI_PROMPT : json .dumps (messages )})
173
- except TypeError :
174
- logger .error (f"Failed to serialize { EVENT_GEN_AI_CONTENT_PROMPT } " )
154
+ _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
175
155
176
156
start_time = default_timer ()
177
157
try :
@@ -188,7 +168,6 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
188
168
stream = result ,
189
169
span = span ,
190
170
capture_message_content = self .capture_message_content ,
191
- event_kind = self .event_kind ,
192
171
event_attributes = event_attributes ,
193
172
event_logger = self .event_logger ,
194
173
start_time = start_time ,
@@ -205,19 +184,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
205
184
_record_operation_duration_metric (self .operation_duration_metric , span , start_time )
206
185
207
186
if self .capture_message_content :
208
- if self .event_kind == "log" :
209
- _send_log_events_from_choices (
210
- self .event_logger , choices = result .choices , attributes = event_attributes
211
- )
212
- elif span .is_recording ():
213
- # same format as the prompt
214
- completion = [_message_from_choice (choice ) for choice in result .choices ]
215
- try :
216
- span .add_event (
217
- EVENT_GEN_AI_CONTENT_COMPLETION , attributes = {GEN_AI_COMPLETION : json .dumps (completion )}
218
- )
219
- except TypeError :
220
- logger .error (f"Failed to serialize { EVENT_GEN_AI_CONTENT_COMPLETION } " )
187
+ _send_log_events_from_choices (self .event_logger , choices = result .choices , attributes = event_attributes )
221
188
222
189
span .end ()
223
190
@@ -239,14 +206,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
239
206
) as span :
240
207
if self .capture_message_content :
241
208
messages = kwargs .get ("messages" , [])
242
-
243
- if self .event_kind == "log" :
244
- _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
245
- elif span .is_recording ():
246
- try :
247
- span .add_event (EVENT_GEN_AI_CONTENT_PROMPT , attributes = {GEN_AI_PROMPT : json .dumps (messages )})
248
- except TypeError :
249
- logger .error (f"Failed to serialize { EVENT_GEN_AI_CONTENT_PROMPT } " )
209
+ _send_log_events_from_messages (self .event_logger , messages = messages , attributes = event_attributes )
250
210
251
211
start_time = default_timer ()
252
212
try :
@@ -263,7 +223,6 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
263
223
stream = result ,
264
224
span = span ,
265
225
capture_message_content = self .capture_message_content ,
266
- event_kind = self .event_kind ,
267
226
event_attributes = event_attributes ,
268
227
event_logger = self .event_logger ,
269
228
start_time = start_time ,
@@ -280,19 +239,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
280
239
_record_operation_duration_metric (self .operation_duration_metric , span , start_time )
281
240
282
241
if self .capture_message_content :
283
- if self .event_kind == "log" :
284
- _send_log_events_from_choices (
285
- self .event_logger , choices = result .choices , attributes = event_attributes
286
- )
287
- elif span .is_recording ():
288
- # same format as the prompt
289
- completion = [_message_from_choice (choice ) for choice in result .choices ]
290
- try :
291
- span .add_event (
292
- EVENT_GEN_AI_CONTENT_COMPLETION , attributes = {GEN_AI_COMPLETION : json .dumps (completion )}
293
- )
294
- except TypeError :
295
- logger .error (f"Failed to serialize { EVENT_GEN_AI_CONTENT_COMPLETION } " )
242
+ _send_log_events_from_choices (self .event_logger , choices = result .choices , attributes = event_attributes )
296
243
297
244
span .end ()
298
245
0 commit comments