Skip to content

Commit f1feaf7

Browse files
committed
refactor: use OTel imported semconv attributes.
1 parent e1738c2 commit f1feaf7

File tree

1 file changed

+25
-11
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai

1 file changed

+25
-11
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@
4848
)
4949
from opentelemetry.semconv.attributes import error_attributes
5050
from opentelemetry.trace.span import Span
51-
from opentelemetry.util.genai.types import ContentCapturingMode, MessagePart
51+
from opentelemetry.util.genai.types import (
52+
ContentCapturingMode,
53+
MessagePart,
54+
InputMessage,
55+
OutputMessage,
56+
)
5257
from opentelemetry.util.genai.upload_hook import load_upload_hook
5358

5459
from .allowlist_util import AllowList
5560
from .custom_semconv import GCP_GENAI_OPERATION_CONFIG
5661
from .dict_util import flatten_dict
5762
from .flags import is_content_recording_enabled
5863
from .message import (
59-
InputMessage,
60-
OutputMessage,
6164
to_input_messages,
6265
to_output_messages,
6366
to_system_instructions,
@@ -263,11 +266,17 @@ def _create_completion_details_attributes(
263266
as_str: bool = False,
264267
) -> dict[str, Any]:
265268
attributes: dict[str, Any] = {
266-
"gen_ai.input.messages": [dataclasses.asdict(input_message) for input_message in input_messages],
267-
"gen_ai.output.messages": [dataclasses.asdict(output_message) for output_message in output_messages],
269+
gen_ai_attributes.GEN_AI_INPUT_MESSAGES: [
270+
dataclasses.asdict(input_message) for input_message in input_messages
271+
],
272+
gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES: [
273+
dataclasses.asdict(output_message) for output_message in output_messages
274+
],
268275
}
269276
if system_instructions:
270-
attributes["gen_ai.system.instructions"] = [dataclasses.asdict(sys_instr) for sys_instr in system_instructions]
277+
attributes[gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS] = [
278+
dataclasses.asdict(sys_instr) for sys_instr in system_instructions
279+
]
271280

272281
if as_str:
273282
return {k: json.dumps(v) for k, v in attributes.items()}
@@ -455,15 +464,16 @@ def _maybe_log_completion_details(
455464
input_messages = to_input_messages(contents=transformers.t_contents(request))
456465
output_messages = to_output_messages(candidates=response.candidates or [])
457466

458-
459-
460467
span = None
461468
if self._content_recording_enabled in [
462469
ContentCapturingMode.SPAN_ONLY,
463470
ContentCapturingMode.SPAN_AND_EVENT,
464471
]:
465472
completion_details_attributes = _create_completion_details_attributes(
466-
input_messages, output_messages, system_instructions, as_str=True,
473+
input_messages,
474+
output_messages,
475+
system_instructions,
476+
as_str=True,
467477
)
468478
span = trace.get_current_span()
469479
span.set_attributes(completion_details_attributes)
@@ -472,10 +482,14 @@ def _maybe_log_completion_details(
472482
ContentCapturingMode.SPAN_AND_EVENT,
473483
]:
474484
completion_details_attributes = _create_completion_details_attributes(
475-
input_messages, output_messages, system_instructions,
485+
input_messages,
486+
output_messages,
487+
system_instructions,
476488
)
477489
attributes.update(completion_details_attributes)
478-
event = Event(name="gen_ai.client.inference.operation.details", attributes=attributes)
490+
event = Event(
491+
name="gen_ai.client.inference.operation.details", attributes=attributes
492+
)
479493
hook = load_upload_hook()
480494
hook.upload(
481495
inputs=input_messages,

0 commit comments

Comments
 (0)