Skip to content

Commit 46b10d1

Browse files
committed
refactor: deduplicate creating attributes, use gen_ai_json_dumps
1 parent 5c35853 commit 46b10d1

File tree

1 file changed

+14
-27
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai

1 file changed

+14
-27
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
MessagePart,
5858
OutputMessage,
5959
)
60-
from opentelemetry.util.genai.utils import _GenAiJsonEncoder
60+
from opentelemetry.util.genai.utils import gen_ai_json_dumps
6161

6262
from .allowlist_util import AllowList
6363
from .custom_semconv import GCP_GENAI_OPERATION_CONFIG
@@ -314,12 +314,6 @@ def _create_completion_details_attributes(
314314
dataclasses.asdict(sys_instr) for sys_instr in system_instructions
315315
]
316316

317-
if as_str:
318-
return {
319-
k: json.dumps(v, cls=_GenAiJsonEncoder)
320-
for k, v in attributes.items()
321-
}
322-
323317
return attributes
324318

325319

@@ -521,37 +515,30 @@ def _maybe_log_completion_details(
521515
span=span,
522516
log_record=event,
523517
)
524-
if self._content_recording_enabled in [
525-
ContentCapturingMode.SPAN_ONLY,
526-
ContentCapturingMode.SPAN_AND_EVENT,
527-
]:
528-
completion_details_attributes = (
529-
_create_completion_details_attributes(
530-
input_messages,
531-
output_messages,
532-
system_instructions,
533-
as_str=True,
534-
)
518+
completion_details_attributes = (
519+
_create_completion_details_attributes(
520+
input_messages,
521+
output_messages,
522+
system_instructions,
535523
)
536-
span.set_attributes(completion_details_attributes)
537-
span.set_attributes(attributes)
524+
)
538525
if self._content_recording_enabled in [
539526
ContentCapturingMode.EVENT_ONLY,
540527
ContentCapturingMode.SPAN_AND_EVENT,
541528
]:
542-
completion_details_attributes = (
543-
_create_completion_details_attributes(
544-
input_messages,
545-
output_messages,
546-
system_instructions,
547-
)
548-
)
549529
event.attributes = {
550530
**(event.attributes or {}),
551531
**completion_details_attributes,
552532
}
553533
self._otel_wrapper.log_completion_details(event=event)
554534

535+
if self._content_recording_enabled in [
536+
ContentCapturingMode.SPAN_ONLY,
537+
ContentCapturingMode.SPAN_AND_EVENT,
538+
]:
539+
span.set_attributes({k: gen_ai_json_dumps(v) for k, v in completion_details_attributes.items()})
540+
span.set_attributes(attributes)
541+
555542
def _maybe_log_system_instruction(
556543
self, config: Optional[GenerateContentConfigOrDict] = None
557544
):

0 commit comments

Comments
 (0)