Skip to content

Commit 1638cbb

Browse files
authored
elastic-opentelemetry-instrumentation-openai: don't update non-recording spans (#25)
1 parent 56d4fe9 commit 1638cbb

File tree

2 files changed

+15
-10
lines changed
  • instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai

2 files changed

+15
-10
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
164164

165165
if self.event_kind == "log":
166166
_send_log_events_from_messages(self.event_logger, messages=messages, attributes=event_attributes)
167-
else:
167+
elif span.is_recording():
168168
try:
169169
span.add_event(EVENT_GEN_AI_CONTENT_PROMPT, attributes={GEN_AI_PROMPT: json.dumps(messages)})
170170
except TypeError:
@@ -195,7 +195,8 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
195195

196196
logger.debug(f"openai.resources.chat.completions.Completions.create result: {result}")
197197

198-
_set_span_attributes_from_response(span, result.id, result.model, result.choices, result.usage)
198+
if span.is_recording():
199+
_set_span_attributes_from_response(span, result.id, result.model, result.choices, result.usage)
199200

200201
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
201202
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
@@ -205,7 +206,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
205206
_send_log_events_from_choices(
206207
self.event_logger, choices=result.choices, attributes=event_attributes
207208
)
208-
else:
209+
elif span.is_recording():
209210
# same format as the prompt
210211
completion = [_message_from_choice(choice) for choice in result.choices]
211212
try:
@@ -238,7 +239,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
238239

239240
if self.event_kind == "log":
240241
_send_log_events_from_messages(self.event_logger, messages=messages, attributes=event_attributes)
241-
else:
242+
elif span.is_recording():
242243
try:
243244
span.add_event(EVENT_GEN_AI_CONTENT_PROMPT, attributes={GEN_AI_PROMPT: json.dumps(messages)})
244245
except TypeError:
@@ -269,7 +270,8 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
269270

270271
logger.debug(f"openai.resources.chat.completions.AsyncCompletions.create result: {result}")
271272

272-
_set_span_attributes_from_response(span, result.id, result.model, result.choices, result.usage)
273+
if span.is_recording():
274+
_set_span_attributes_from_response(span, result.id, result.model, result.choices, result.usage)
273275

274276
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
275277
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
@@ -279,7 +281,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
279281
_send_log_events_from_choices(
280282
self.event_logger, choices=result.choices, attributes=event_attributes
281283
)
282-
else:
284+
elif span.is_recording():
283285
# same format as the prompt
284286
completion = [_message_from_choice(choice) for choice in result.choices]
285287
try:
@@ -314,7 +316,8 @@ def _embeddings_wrapper(self, wrapped, instance, args, kwargs):
314316
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
315317
raise
316318

317-
_set_embeddings_span_attributes_from_response(span, result.model, result.usage)
319+
if span.is_recording():
320+
_set_embeddings_span_attributes_from_response(span, result.model, result.usage)
318321

319322
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
320323
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
@@ -344,7 +347,8 @@ async def _async_embeddings_wrapper(self, wrapped, instance, args, kwargs):
344347
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
345348
raise
346349

347-
_set_embeddings_span_attributes_from_response(span, result.model, result.usage)
350+
if span.is_recording():
351+
_set_embeddings_span_attributes_from_response(span, result.model, result.usage)
348352

349353
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
350354
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def end(self, exc=None):
7777
_record_operation_duration_metric(self.operation_duration_metric, self.span, self.start_time)
7878
return
7979

80-
_set_span_attributes_from_response(self.span, self.response_id, self.model, self.choices, self.usage)
80+
if self.span.is_recording():
81+
_set_span_attributes_from_response(self.span, self.response_id, self.model, self.choices, self.usage)
8182

8283
_record_operation_duration_metric(self.operation_duration_metric, self.span, self.start_time)
8384
if self.usage:
@@ -88,7 +89,7 @@ def end(self, exc=None):
8889
_send_log_events_from_stream_choices(
8990
self.event_logger, choices=self.choices, span=self.span, attributes=self.event_attributes
9091
)
91-
else:
92+
elif self.span.is_recording():
9293
# same format as the prompt
9394
completion = [_message_from_stream_choices(self.choices)]
9495
try:

0 commit comments

Comments
 (0)