Skip to content

Commit 4a08d7f

Browse files
committed
clean up context handler, clarify unit tests
1 parent c1ff846 commit 4a08d7f

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def fail_llm(
9898
return invocation
9999

100100
@contextmanager
101-
def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
101+
def llm(
102+
self, invocation: Optional[LLMInvocation] = None
103+
) -> Iterator[LLMInvocation]:
102104
"""Context manager for LLM invocations.
103105
104106
Only set data attributes on the invocation object, do not modify the span or context.
@@ -107,6 +109,10 @@ def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
107109
If an exception occurs inside the context, marks the span as error, ends it, and
108110
re-raises the original exception.
109111
"""
112+
if invocation is None:
113+
invocation = LLMInvocation(
114+
request_model="",
115+
)
110116
self.start_llm(invocation)
111117
try:
112118
yield invocation

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def _apply_common_span_attributes(
4848
"""
4949
request_model = invocation.request_model
5050
provider = invocation.provider
51-
51+
span.update_name(
52+
f"{GenAI.GenAiOperationNameValues.CHAT.value} {request_model}"
53+
)
5254
span.set_attribute(
5355
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
5456
)

util/opentelemetry-util-genai/tests/test_utils.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,11 @@ def test_llm_start_and_stop_creates_span(self): # pylint: disable=no-self-use
135135
)
136136

137137
# Start and stop LLM invocation using context manager
138-
invocation = LLMInvocation(
139-
request_model="test-model",
140-
input_messages=[message],
141-
provider="test-provider",
142-
attributes={"custom_attr": "value"},
143-
)
144-
145-
with self.telemetry_handler.llm(invocation):
138+
with self.telemetry_handler.llm() as invocation:
139+
invocation.request_model = "test-model"
140+
invocation.input_messages = [message]
141+
invocation.provider = "test-provider"
142+
invocation.attributes = {"custom_attr": "value"}
146143
assert invocation.span is not None
147144
invocation.output_messages = [chat_generation]
148145
invocation.attributes.update({"extra": "info"})
@@ -234,20 +231,16 @@ def test_parent_child_span_relationship(self):
234231
role="AI", parts=[Text(content="ok")], finish_reason="stop"
235232
)
236233

237-
# Start parent and child using nested contexts (child becomes child span of parent)
238-
parent_invocation = LLMInvocation(
239-
request_model="parent-model",
240-
input_messages=[message],
241-
provider="test-provider",
242-
)
243-
child_invocation = LLMInvocation(
244-
request_model="child-model",
245-
input_messages=[message],
246-
provider="test-provider",
247-
)
248-
249-
with self.telemetry_handler.llm(parent_invocation):
250-
with self.telemetry_handler.llm(child_invocation):
234+
with self.telemetry_handler.llm() as parent_invocation:
235+
parent_invocation.request_model = "parent-model"
236+
parent_invocation.input_messages = [message]
237+
parent_invocation.provider = "test-provider"
238+
# Perform things here, calling a tool, processing, etc.
239+
with self.telemetry_handler.llm() as child_invocation:
240+
child_invocation.request_model = "child-model"
241+
child_invocation.input_messages = [message]
242+
child_invocation.provider = "test-provider"
243+
# Perform things here, calling a tool, processing, etc.
251244
# Stop child first by exiting inner context
252245
child_invocation.output_messages = [chat_generation]
253246
# Then stop parent by exiting outer context

0 commit comments

Comments
 (0)