Skip to content

Commit cab5ca0

Browse files
committed
clean up extra functions
1 parent 7c017c4 commit cab5ca0

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

util/opentelemetry-util-genai/src/opentelemetry/util/genai/generators.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,18 @@ def _apply_common_span_attributes(
7878
request_model = invocation.attributes.get("request_model")
7979
provider = invocation.attributes.get("provider")
8080

81-
_set_initial_span_attributes(span, request_model, provider)
81+
span.set_attribute(
82+
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
83+
)
84+
if request_model:
85+
span.set_attribute(GenAI.GEN_AI_REQUEST_MODEL, request_model)
86+
if provider is not None:
87+
# TODO: clean provider name to match GenAiProviderNameValues?
88+
span.set_attribute(GenAI.GEN_AI_PROVIDER_NAME, provider)
8289

83-
finish_reasons = _collect_finish_reasons(invocation.chat_generations)
90+
finish_reasons: List[str] = []
91+
for gen in invocation.chat_generations:
92+
finish_reasons.append(gen.finish_reason)
8493
if finish_reasons:
8594
span.set_attribute(
8695
GenAI.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons
@@ -99,21 +108,6 @@ def _apply_common_span_attributes(
99108
)
100109

101110

102-
def _set_initial_span_attributes(
103-
span: Span,
104-
request_model: Optional[str],
105-
provider: Optional[str],
106-
) -> None:
107-
span.set_attribute(
108-
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
109-
)
110-
if request_model:
111-
span.set_attribute(GenAI.GEN_AI_REQUEST_MODEL, request_model)
112-
if provider is not None:
113-
# TODO: clean provider name to match GenAiProviderNameValues?
114-
span.set_attribute(GenAI.GEN_AI_PROVIDER_NAME, provider)
115-
116-
117111
def _set_response_and_usage_attributes(
118112
span: Span,
119113
response_model: Optional[str],
@@ -131,13 +125,6 @@ def _set_response_and_usage_attributes(
131125
span.set_attribute(GenAI.GEN_AI_USAGE_OUTPUT_TOKENS, completion_tokens)
132126

133127

134-
def _collect_finish_reasons(generations: List[OutputMessage]) -> List[str]:
135-
finish_reasons: List[str] = []
136-
for gen in generations:
137-
finish_reasons.append(gen.finish_reason)
138-
return finish_reasons
139-
140-
141128
def _maybe_set_span_messages(
142129
span: Span,
143130
input_messages: List[InputMessage],

util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def start_llm(
7171
run_id: UUID,
7272
parent_run_id: Optional[UUID] = None,
7373
**attributes: Any,
74-
) -> None:
74+
) -> LLMInvocation:
7575
invocation = LLMInvocation(
7676
request_model=request_model,
7777
messages=prompts,
@@ -81,6 +81,7 @@ def start_llm(
8181
)
8282
self._llm_registry[invocation.run_id] = invocation
8383
self._generator.start(invocation)
84+
return invocation
8485

8586
def stop_llm(
8687
self,

0 commit comments

Comments
 (0)