|
33 | 33 | from opentelemetry.trace.status import Status, StatusCode |
34 | 34 | from opentelemetry.util.genai.types import ( |
35 | 35 | AgentCreation, |
| 36 | + AgentInvocation, |
36 | 37 | Error, |
37 | 38 | InputMessage, |
38 | 39 | LLMInvocation, |
@@ -62,6 +63,16 @@ def _agent_attr(name: str, fallback: str) -> str: |
62 | 63 | _GEN_AI_AGENT_VERSION = _agent_attr( |
63 | 64 | "GEN_AI_AGENT_VERSION", "gen_ai.agent.version" |
64 | 65 | ) |
| 66 | +_GEN_AI_CONVERSATION_ID = _agent_attr( |
| 67 | + "GEN_AI_CONVERSATION_ID", "gen_ai.conversation.id" |
| 68 | +) |
| 69 | +_GEN_AI_DATA_SOURCE_ID = _agent_attr( |
| 70 | + "GEN_AI_DATA_SOURCE_ID", "gen_ai.data_source.id" |
| 71 | +) |
| 72 | +_GEN_AI_OUTPUT_TYPE = _agent_attr("GEN_AI_OUTPUT_TYPE", "gen_ai.output.type") |
| 73 | +_GEN_AI_TOOL_DEFINITIONS = _agent_attr( |
| 74 | + "GEN_AI_TOOL_DEFINITIONS", "gen_ai.tool.definitions" |
| 75 | +) |
65 | 76 |
|
66 | 77 |
|
67 | 78 | def _get_llm_common_attributes( |
@@ -324,6 +335,149 @@ def _get_base_agent_span_name(agent: _BaseAgent) -> str: |
324 | 335 | return agent.operation_name |
325 | 336 |
|
326 | 337 |
|
| 338 | +def _get_agent_common_attributes( |
| 339 | + invocation: AgentInvocation, |
| 340 | +) -> dict[str, Any]: |
| 341 | + """Get common agent invocation attributes shared by finish() and error() paths.""" |
| 342 | + attrs = _get_base_agent_common_attributes(invocation) |
| 343 | + |
| 344 | + # Invoke-specific conditionally required attributes |
| 345 | + invoke_attrs = ( |
| 346 | + (_GEN_AI_CONVERSATION_ID, invocation.conversation_id), |
| 347 | + (_GEN_AI_DATA_SOURCE_ID, invocation.data_source_id), |
| 348 | + (_GEN_AI_OUTPUT_TYPE, invocation.output_type), |
| 349 | + ) |
| 350 | + attrs.update( |
| 351 | + {key: value for key, value in invoke_attrs if value is not None} |
| 352 | + ) |
| 353 | + |
| 354 | + return attrs |
| 355 | + |
| 356 | + |
| 357 | +def _get_agent_span_name(invocation: AgentInvocation) -> str: |
| 358 | + """Get the span name for an agent invocation.""" |
| 359 | + return _get_base_agent_span_name(invocation) |
| 360 | + |
| 361 | + |
| 362 | +def _get_agent_request_attributes( |
| 363 | + invocation: AgentInvocation, |
| 364 | +) -> dict[str, Any]: |
| 365 | + """Get GenAI request semantic convention attributes for agent invocation.""" |
| 366 | + optional_attrs = ( |
| 367 | + (GenAI.GEN_AI_REQUEST_TEMPERATURE, invocation.temperature), |
| 368 | + (GenAI.GEN_AI_REQUEST_TOP_P, invocation.top_p), |
| 369 | + (GenAI.GEN_AI_REQUEST_FREQUENCY_PENALTY, invocation.frequency_penalty), |
| 370 | + (GenAI.GEN_AI_REQUEST_PRESENCE_PENALTY, invocation.presence_penalty), |
| 371 | + (GenAI.GEN_AI_REQUEST_MAX_TOKENS, invocation.max_tokens), |
| 372 | + (GenAI.GEN_AI_REQUEST_STOP_SEQUENCES, invocation.stop_sequences), |
| 373 | + (GenAI.GEN_AI_REQUEST_SEED, invocation.seed), |
| 374 | + (GenAI.GEN_AI_REQUEST_CHOICE_COUNT, invocation.choice_count), |
| 375 | + ) |
| 376 | + |
| 377 | + return {key: value for key, value in optional_attrs if value is not None} |
| 378 | + |
| 379 | + |
| 380 | +def _get_agent_response_attributes( |
| 381 | + invocation: AgentInvocation, |
| 382 | +) -> dict[str, Any]: |
| 383 | + """Get GenAI response semantic convention attributes for agent invocation.""" |
| 384 | + finish_reasons: list[str] | None |
| 385 | + if invocation.finish_reasons is not None: |
| 386 | + finish_reasons = invocation.finish_reasons |
| 387 | + elif invocation.output_messages: |
| 388 | + finish_reasons = [ |
| 389 | + message.finish_reason |
| 390 | + for message in invocation.output_messages |
| 391 | + if message.finish_reason |
| 392 | + ] |
| 393 | + else: |
| 394 | + finish_reasons = None |
| 395 | + |
| 396 | + unique_finish_reasons = ( |
| 397 | + sorted(set(finish_reasons)) if finish_reasons else None |
| 398 | + ) |
| 399 | + |
| 400 | + optional_attrs = ( |
| 401 | + ( |
| 402 | + GenAI.GEN_AI_RESPONSE_FINISH_REASONS, |
| 403 | + unique_finish_reasons if unique_finish_reasons else None, |
| 404 | + ), |
| 405 | + (GenAI.GEN_AI_RESPONSE_MODEL, invocation.response_model_name), |
| 406 | + (GenAI.GEN_AI_RESPONSE_ID, invocation.response_id), |
| 407 | + (GenAI.GEN_AI_USAGE_INPUT_TOKENS, invocation.input_tokens), |
| 408 | + (GenAI.GEN_AI_USAGE_OUTPUT_TOKENS, invocation.output_tokens), |
| 409 | + ) |
| 410 | + |
| 411 | + return {key: value for key, value in optional_attrs if value is not None} |
| 412 | + |
| 413 | + |
| 414 | +def _get_agent_messages_attributes_for_span( |
| 415 | + input_messages: list[InputMessage], |
| 416 | + output_messages: list[OutputMessage], |
| 417 | + system_instruction: list[MessagePart] | None = None, |
| 418 | + tool_definitions: list[dict[str, Any]] | None = None, |
| 419 | +) -> dict[str, Any]: |
| 420 | + """Get message attributes formatted for span (JSON string format) for agent invocation.""" |
| 421 | + if not is_experimental_mode() or get_content_capturing_mode() not in ( |
| 422 | + ContentCapturingMode.SPAN_ONLY, |
| 423 | + ContentCapturingMode.SPAN_AND_EVENT, |
| 424 | + ): |
| 425 | + return {} |
| 426 | + |
| 427 | + optional_attrs = ( |
| 428 | + ( |
| 429 | + GenAI.GEN_AI_INPUT_MESSAGES, |
| 430 | + gen_ai_json_dumps([asdict(m) for m in input_messages]) |
| 431 | + if input_messages |
| 432 | + else None, |
| 433 | + ), |
| 434 | + ( |
| 435 | + GenAI.GEN_AI_OUTPUT_MESSAGES, |
| 436 | + gen_ai_json_dumps([asdict(m) for m in output_messages]) |
| 437 | + if output_messages |
| 438 | + else None, |
| 439 | + ), |
| 440 | + ( |
| 441 | + GenAI.GEN_AI_SYSTEM_INSTRUCTIONS, |
| 442 | + gen_ai_json_dumps([asdict(p) for p in system_instruction]) |
| 443 | + if system_instruction |
| 444 | + else None, |
| 445 | + ), |
| 446 | + ( |
| 447 | + _GEN_AI_TOOL_DEFINITIONS, |
| 448 | + gen_ai_json_dumps(tool_definitions) |
| 449 | + if tool_definitions |
| 450 | + else None, |
| 451 | + ), |
| 452 | + ) |
| 453 | + |
| 454 | + return {key: value for key, value in optional_attrs if value is not None} |
| 455 | + |
| 456 | + |
| 457 | +def _apply_agent_finish_attributes( |
| 458 | + span: Span, invocation: AgentInvocation |
| 459 | +) -> None: |
| 460 | + """Apply attributes/messages common to agent finish() paths.""" |
| 461 | + span.update_name(_get_agent_span_name(invocation)) |
| 462 | + |
| 463 | + attributes: dict[str, Any] = {} |
| 464 | + attributes.update(_get_agent_common_attributes(invocation)) |
| 465 | + attributes.update(_get_agent_request_attributes(invocation)) |
| 466 | + attributes.update(_get_agent_response_attributes(invocation)) |
| 467 | + attributes.update( |
| 468 | + _get_agent_messages_attributes_for_span( |
| 469 | + invocation.input_messages, |
| 470 | + invocation.output_messages, |
| 471 | + invocation.system_instruction, |
| 472 | + invocation.tool_definitions, |
| 473 | + ) |
| 474 | + ) |
| 475 | + attributes.update(invocation.attributes) |
| 476 | + |
| 477 | + if attributes: |
| 478 | + span.set_attributes(attributes) |
| 479 | + |
| 480 | + |
327 | 481 | def _get_creation_common_attributes( |
328 | 482 | creation: AgentCreation, |
329 | 483 | ) -> dict[str, Any]: |
@@ -375,7 +529,12 @@ def _apply_creation_finish_attributes( |
375 | 529 | "_maybe_emit_llm_event", |
376 | 530 | "_get_base_agent_common_attributes", |
377 | 531 | "_get_base_agent_span_name", |
| 532 | + "_apply_agent_finish_attributes", |
378 | 533 | "_apply_creation_finish_attributes", |
| 534 | + "_get_agent_common_attributes", |
| 535 | + "_get_agent_request_attributes", |
| 536 | + "_get_agent_response_attributes", |
| 537 | + "_get_agent_span_name", |
379 | 538 | "_get_creation_common_attributes", |
380 | 539 | "_get_creation_span_name", |
381 | 540 | ] |
0 commit comments