25
25
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT ,
26
26
)
27
27
from opentelemetry .instrumentation .openai .helpers import (
28
- _get_embeddings_span_attributes_from_wrapper ,
28
+ _get_attributes_from_response ,
29
+ _get_attributes_from_wrapper ,
30
+ _get_embeddings_attributes_from_response ,
31
+ _get_embeddings_attributes_from_wrapper ,
29
32
_get_event_attributes ,
30
- _get_span_attributes_from_wrapper ,
31
33
_record_operation_duration_metric ,
32
34
_record_token_usage_metrics ,
33
35
_send_log_events_from_choices ,
34
36
_send_log_events_from_messages ,
35
- _set_embeddings_span_attributes_from_response ,
36
- _set_span_attributes_from_response ,
37
- _span_name_from_span_attributes ,
37
+ _span_name_from_attributes ,
38
38
)
39
39
from opentelemetry .instrumentation .openai .package import _instruments
40
40
from opentelemetry .instrumentation .openai .version import __version__
@@ -134,10 +134,10 @@ def _uninstrument(self, **kwargs):
134
134
def _chat_completion_wrapper (self , wrapped , instance , args , kwargs ):
135
135
logger .debug (f"openai.resources.chat.completions.Completions.create kwargs: { kwargs } " )
136
136
137
- span_attributes = _get_span_attributes_from_wrapper (instance , kwargs )
137
+ span_attributes = _get_attributes_from_wrapper (instance , kwargs )
138
138
event_attributes = _get_event_attributes ()
139
139
140
- span_name = _span_name_from_span_attributes (span_attributes )
140
+ span_name = _span_name_from_attributes (span_attributes )
141
141
with self .tracer .start_as_current_span (
142
142
name = span_name ,
143
143
kind = SpanKind .CLIENT ,
@@ -160,13 +160,15 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
160
160
span .set_status (StatusCode .ERROR , str (exc ))
161
161
span .set_attribute (ERROR_TYPE , exc .__class__ .__qualname__ )
162
162
span .end ()
163
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
163
+ error_attributes = {** span_attributes , ERROR_TYPE : exc .__class__ .__qualname__ }
164
+ _record_operation_duration_metric (self .operation_duration_metric , error_attributes , start_time )
164
165
raise
165
166
166
167
if kwargs .get ("stream" ):
167
168
return StreamWrapper (
168
169
stream = result ,
169
170
span = span ,
171
+ span_attributes = span_attributes ,
170
172
capture_message_content = self .capture_message_content ,
171
173
event_attributes = event_attributes ,
172
174
event_logger = self .event_logger ,
@@ -177,13 +179,16 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
177
179
178
180
logger .debug (f"openai.resources.chat.completions.Completions.create result: { result } " )
179
181
182
+ response_attributes = _get_attributes_from_response (
183
+ result .id , result .model , result .choices , result .usage , getattr (result , "service_tier" , None )
184
+ )
180
185
if span .is_recording ():
181
- _set_span_attributes_from_response (
182
- span , result .id , result .model , result .choices , result .usage , getattr (result , "service_tier" , None )
183
- )
186
+ for k , v in response_attributes .items ():
187
+ span .set_attribute (k , v )
184
188
185
- _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
186
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
189
+ metrics_attributes = {** span_attributes , ** response_attributes }
190
+ _record_token_usage_metrics (self .token_usage_metric , metrics_attributes , result .usage )
191
+ _record_operation_duration_metric (self .operation_duration_metric , metrics_attributes , start_time )
187
192
188
193
_send_log_events_from_choices (
189
194
self .event_logger ,
@@ -199,10 +204,10 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
199
204
async def _async_chat_completion_wrapper (self , wrapped , instance , args , kwargs ):
200
205
logger .debug (f"openai.resources.chat.completions.AsyncCompletions.create kwargs: { kwargs } " )
201
206
202
- span_attributes = _get_span_attributes_from_wrapper (instance , kwargs )
207
+ span_attributes = _get_attributes_from_wrapper (instance , kwargs )
203
208
event_attributes = _get_event_attributes ()
204
209
205
- span_name = _span_name_from_span_attributes (span_attributes )
210
+ span_name = _span_name_from_attributes (span_attributes )
206
211
with self .tracer .start_as_current_span (
207
212
name = span_name ,
208
213
kind = SpanKind .CLIENT ,
@@ -225,13 +230,15 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
225
230
span .set_status (StatusCode .ERROR , str (exc ))
226
231
span .set_attribute (ERROR_TYPE , exc .__class__ .__qualname__ )
227
232
span .end ()
228
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
233
+ error_attributes = {ERROR_TYPE : exc .__class__ .__qualname__ }
234
+ _record_operation_duration_metric (self .operation_duration_metric , error_attributes , start_time )
229
235
raise
230
236
231
237
if kwargs .get ("stream" ):
232
238
return StreamWrapper (
233
239
stream = result ,
234
240
span = span ,
241
+ span_attributes = span_attributes ,
235
242
capture_message_content = self .capture_message_content ,
236
243
event_attributes = event_attributes ,
237
244
event_logger = self .event_logger ,
@@ -242,13 +249,16 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
242
249
243
250
logger .debug (f"openai.resources.chat.completions.AsyncCompletions.create result: { result } " )
244
251
252
+ response_attributes = _get_attributes_from_response (
253
+ result .id , result .model , result .choices , result .usage , getattr (result , "service_tier" , None )
254
+ )
245
255
if span .is_recording ():
246
- _set_span_attributes_from_response (
247
- span , result .id , result .model , result .choices , result .usage , getattr (result , "service_tier" , None )
248
- )
256
+ for k , v in response_attributes .items ():
257
+ span .set_attribute (k , v )
249
258
250
- _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
251
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
259
+ metrics_attributes = {** span_attributes , ** response_attributes }
260
+ _record_token_usage_metrics (self .token_usage_metric , metrics_attributes , result .usage )
261
+ _record_operation_duration_metric (self .operation_duration_metric , metrics_attributes , start_time )
252
262
253
263
_send_log_events_from_choices (
254
264
self .event_logger ,
@@ -262,9 +272,9 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
262
272
return result
263
273
264
274
def _embeddings_wrapper (self , wrapped , instance , args , kwargs ):
265
- span_attributes = _get_embeddings_span_attributes_from_wrapper (instance , kwargs )
275
+ span_attributes = _get_embeddings_attributes_from_wrapper (instance , kwargs )
266
276
267
- span_name = _span_name_from_span_attributes (span_attributes )
277
+ span_name = _span_name_from_attributes (span_attributes )
268
278
with self .tracer .start_as_current_span (
269
279
name = span_name ,
270
280
kind = SpanKind .CLIENT ,
@@ -279,23 +289,27 @@ def _embeddings_wrapper(self, wrapped, instance, args, kwargs):
279
289
span .set_status (StatusCode .ERROR , str (exc ))
280
290
span .set_attribute (ERROR_TYPE , exc .__class__ .__qualname__ )
281
291
span .end ()
282
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
292
+ error_attributes = {** span_attributes , ERROR_TYPE : exc .__class__ .__qualname__ }
293
+ _record_operation_duration_metric (self .operation_duration_metric , error_attributes , start_time )
283
294
raise
284
295
296
+ response_attributes = _get_embeddings_attributes_from_response (result .model , result .usage )
285
297
if span .is_recording ():
286
- _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
298
+ for k , v in response_attributes .items ():
299
+ span .set_attribute (k , v )
287
300
288
- _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
289
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
301
+ metrics_attributes = {** span_attributes , ** response_attributes }
302
+ _record_token_usage_metrics (self .token_usage_metric , metrics_attributes , result .usage )
303
+ _record_operation_duration_metric (self .operation_duration_metric , metrics_attributes , start_time )
290
304
291
305
span .end ()
292
306
293
307
return result
294
308
295
309
async def _async_embeddings_wrapper (self , wrapped , instance , args , kwargs ):
296
- span_attributes = _get_embeddings_span_attributes_from_wrapper (instance , kwargs )
310
+ span_attributes = _get_embeddings_attributes_from_wrapper (instance , kwargs )
297
311
298
- span_name = _span_name_from_span_attributes (span_attributes )
312
+ span_name = _span_name_from_attributes (span_attributes )
299
313
with self .tracer .start_as_current_span (
300
314
name = span_name ,
301
315
kind = SpanKind .CLIENT ,
@@ -310,14 +324,18 @@ async def _async_embeddings_wrapper(self, wrapped, instance, args, kwargs):
310
324
span .set_status (StatusCode .ERROR , str (exc ))
311
325
span .set_attribute (ERROR_TYPE , exc .__class__ .__qualname__ )
312
326
span .end ()
313
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
327
+ error_attributes = {** span_attributes , ERROR_TYPE : exc .__class__ .__qualname__ }
328
+ _record_operation_duration_metric (self .operation_duration_metric , error_attributes , start_time )
314
329
raise
315
330
331
+ response_attributes = _get_embeddings_attributes_from_response (result .model , result .usage )
316
332
if span .is_recording ():
317
- _set_embeddings_span_attributes_from_response (span , result .model , result .usage )
333
+ for k , v in response_attributes .items ():
334
+ span .set_attribute (k , v )
318
335
319
- _record_token_usage_metrics (self .token_usage_metric , span , result .usage )
320
- _record_operation_duration_metric (self .operation_duration_metric , span , start_time )
336
+ metrics_attributes = {** span_attributes , ** response_attributes }
337
+ _record_token_usage_metrics (self .token_usage_metric , metrics_attributes , result .usage )
338
+ _record_operation_duration_metric (self .operation_duration_metric , metrics_attributes , start_time )
321
339
322
340
span .end ()
323
341
0 commit comments