Skip to content

Commit d28860a

Browse files
committed
apply _lifecycle_context and error handling
1 parent c6e9822 commit d28860a

3 files changed

Lines changed: 12 additions & 71 deletions

File tree

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from opentelemetry.trace.propagation import set_span_in_context
3333
from opentelemetry.trace.status import Status, StatusCode
3434
from opentelemetry.util.genai.types import (
35-
AgentCreation,
3635
AgentInvocation,
3736
EmbeddingInvocation,
3837
Error,
@@ -174,9 +173,7 @@ def _get_llm_messages_attributes_for_span(
174173
),
175174
(
176175
GenAI.GEN_AI_TOOL_DEFINITIONS,
177-
gen_ai_json_dumps(tool_definitions)
178-
if tool_definitions
179-
else None,
176+
gen_ai_json_dumps(tool_definitions) if tool_definitions else None,
180177
),
181178
)
182179

@@ -524,24 +521,6 @@ def _apply_agent_finish_attributes(
524521
span.set_attributes(attributes)
525522

526523

527-
def _apply_creation_finish_attributes(
528-
span: Span, creation: AgentCreation
529-
) -> None:
530-
"""Apply attributes common to agent creation finish() paths."""
531-
span.update_name(_get_base_agent_span_name(creation))
532-
533-
attributes: dict[str, Any] = {}
534-
attributes.update(_get_base_agent_common_attributes(creation))
535-
536-
attributes.update(
537-
_get_system_instructions_for_span(creation.system_instruction)
538-
)
539-
attributes.update(creation.attributes)
540-
541-
if attributes:
542-
span.set_attributes(attributes)
543-
544-
545524
__all__ = [
546525
"_apply_llm_finish_attributes",
547526
"_apply_error_attributes",
@@ -558,7 +537,6 @@ def _apply_creation_finish_attributes(
558537
"_get_base_agent_common_attributes",
559538
"_get_base_agent_span_name",
560539
"_apply_agent_finish_attributes",
561-
"_apply_creation_finish_attributes",
562540
"_get_system_instructions_for_span",
563541
"_get_agent_common_attributes",
564542
"_get_agent_request_attributes",

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,22 @@ class EmbeddingInvocation(GenAIInvocation):
314314
@dataclass
315315
class _BaseAgent(GenAIInvocation):
316316
"""
317-
Shared base class for agent lifecycle types (AgentInvocation, AgentCreation).
317+
Shared base class for agent lifecycle types.
318318
319319
Contains fields common to all agent operations: identity, provider,
320320
model, system instructions, server info, and telemetry plumbing.
321321
322322
Follows semconv for GenAI agent spans:
323323
https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-agent-spans.md
324324
325-
Do not instantiate directly — use AgentInvocation or AgentCreation.
325+
Do not instantiate directly — use AgentInvocation.
326326
"""
327327

328328
agent_name: str | None = None
329329
agent_id: str | None = None
330330
agent_description: str | None = None
331331
agent_version: str | None = None
332332

333-
operation_name: str = ""
334333
provider: str | None = None
335334

336335
request_model: str | None = None
@@ -351,21 +350,6 @@ class _BaseAgent(GenAIInvocation):
351350
monotonic_start_s: float | None = None
352351

353352

354-
@dataclass
355-
class AgentCreation(_BaseAgent):
356-
"""
357-
Represents agent creation/initialization (create_agent operation).
358-
359-
Follows semconv for GenAI agent spans:
360-
https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-agent-spans.md#create-agent-span
361-
362-
When creating an AgentCreation object, only update the data attributes.
363-
The span and context_token attributes are set by the TelemetryHandler.
364-
"""
365-
366-
operation_name: str = "create_agent"
367-
368-
369353
@dataclass
370354
class AgentInvocation(_BaseAgent):
371355
"""
@@ -378,7 +362,7 @@ class AgentInvocation(_BaseAgent):
378362
The span and context_token attributes are set by the TelemetryHandler.
379363
"""
380364

381-
operation_name: str = "invoke_agent"
365+
operation_name: str = GenAI.GenAiOperationNameValues.INVOKE_AGENT.value
382366
conversation_id: str | None = None
383367
data_source_id: str | None = None
384368
output_type: str | None = None
@@ -408,8 +392,6 @@ class AgentInvocation(_BaseAgent):
408392
)
409393
tool_definitions: list[dict[str, Any]] | None = None
410394

411-
is_remote: bool = True
412-
413395
metric_attributes: dict[str, Any] = field(
414396
default_factory=_new_str_any_dict
415397
)

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def _make_handler(self) -> TelemetryHandler:
3838

3939

4040
class TestAgentInvocationHandler(_AgentTestBase):
41-
4241
def test_start_stop_creates_span(self) -> None:
4342
handler = self._make_handler()
4443
invocation = AgentInvocation(
@@ -59,31 +58,18 @@ def test_start_stop_creates_span(self) -> None:
5958
self.assertEqual(
6059
span.attributes[GenAI.GEN_AI_AGENT_NAME], "Math Tutor"
6160
)
62-
self.assertEqual(
63-
span.attributes[GenAI.GEN_AI_PROVIDER_NAME], "openai"
64-
)
65-
self.assertEqual(
66-
span.attributes[GenAI.GEN_AI_REQUEST_MODEL], "gpt-4"
67-
)
61+
self.assertEqual(span.attributes[GenAI.GEN_AI_PROVIDER_NAME], "openai")
62+
self.assertEqual(span.attributes[GenAI.GEN_AI_REQUEST_MODEL], "gpt-4")
6863

6964
def test_span_kind_client_by_default(self) -> None:
7065
handler = self._make_handler()
71-
invocation = AgentInvocation(agent_name="Agent", is_remote=True)
66+
invocation = AgentInvocation(agent_name="Agent")
7267
handler.start_agent(invocation)
7368
handler.stop_agent(invocation)
7469
self.assertEqual(
7570
self.span_exporter.get_finished_spans()[0].kind, SpanKind.CLIENT
7671
)
7772

78-
def test_span_kind_internal_for_local(self) -> None:
79-
handler = self._make_handler()
80-
invocation = AgentInvocation(agent_name="Agent", is_remote=False)
81-
handler.start_agent(invocation)
82-
handler.stop_agent(invocation)
83-
self.assertEqual(
84-
self.span_exporter.get_finished_spans()[0].kind, SpanKind.INTERNAL
85-
)
86-
8773
def test_all_attributes(self) -> None:
8874
handler = self._make_handler()
8975
invocation = AgentInvocation(
@@ -114,9 +100,7 @@ def test_all_attributes(self) -> None:
114100
attrs = self.span_exporter.get_finished_spans()[0].attributes
115101
self.assertEqual(attrs[GenAI.GEN_AI_AGENT_NAME], "Full Agent")
116102
self.assertEqual(attrs[GenAI.GEN_AI_AGENT_ID], "agent-123")
117-
self.assertEqual(
118-
attrs[GenAI.GEN_AI_AGENT_DESCRIPTION], "A test agent"
119-
)
103+
self.assertEqual(attrs[GenAI.GEN_AI_AGENT_DESCRIPTION], "A test agent")
120104
self.assertEqual(attrs[GenAI.GEN_AI_RESPONSE_MODEL], "gpt-4-0613")
121105
self.assertEqual(attrs[GenAI.GEN_AI_RESPONSE_ID], "resp-abc")
122106
self.assertEqual(attrs[GenAI.GEN_AI_USAGE_INPUT_TOKENS], 100)
@@ -142,9 +126,7 @@ def test_cache_token_attributes(self) -> None:
142126
self.assertEqual(
143127
attrs[GenAI.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS], 25
144128
)
145-
self.assertEqual(
146-
attrs[GenAI.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS], 50
147-
)
129+
self.assertEqual(attrs[GenAI.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS], 50)
148130

149131
def test_fail_sets_error_status(self) -> None:
150132
handler = self._make_handler()
@@ -179,8 +161,9 @@ def test_context_manager_error(self) -> None:
179161
raise ValueError("test error")
180162

181163
self.assertEqual(
182-
self.span_exporter.get_finished_spans()[0]
183-
.attributes.get("error.type"),
164+
self.span_exporter.get_finished_spans()[0].attributes.get(
165+
"error.type"
166+
),
184167
"ValueError",
185168
)
186169

@@ -209,14 +192,12 @@ def test_fail_without_start_is_noop(self) -> None:
209192

210193

211194
class TestAgentInvocationType(TestCase):
212-
213195
def test_defaults(self) -> None:
214196
inv = AgentInvocation()
215197
self.assertEqual(inv.operation_name, "invoke_agent")
216198
self.assertIsNone(inv.agent_name)
217199
self.assertIsNone(inv.provider)
218200
self.assertIsNone(inv.request_model)
219-
self.assertTrue(inv.is_remote)
220201
self.assertEqual(inv.input_messages, [])
221202
self.assertEqual(inv.output_messages, [])
222203
self.assertIsNone(inv.tool_definitions)

0 commit comments

Comments
 (0)