A programming framework for agentic AI
improve Otel tracing (#6499)
peterychang3db7a294034e
diff --git a/python/packages/autogen-agentchat/tests/test_group_chat.py b/python/packages/autogen-agentchat/tests/test_group_chat.pyindex e7e8ba436..889b67d87 100644--- a/python/packages/autogen-agentchat/tests/test_group_chat.py+++ b/python/packages/autogen-agentchat/tests/test_group_chat.py@@ -6,24 +6,6 @@ from typing import Any, AsyncGenerator, Dict, List, Mapping, Sequenceimport pytestimport pytest_asyncio-from autogen_core import AgentId, AgentRuntime, CancellationToken, FunctionCall, SingleThreadedAgentRuntime-from autogen_core.model_context import BufferedChatCompletionContext-from autogen_core.models import (- AssistantMessage,- CreateResult,- FunctionExecutionResult,- FunctionExecutionResultMessage,- LLMMessage,- RequestUsage,- UserMessage,-)-from autogen_core.tools import FunctionTool-from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor-from autogen_ext.models.openai import OpenAIChatCompletionClient-from autogen_ext.models.replay import ReplayChatCompletionClient-from pydantic import BaseModel-from utils import FileLogHandler-from autogen_agentchat import EVENT_LOGGER_NAMEfrom autogen_agentchat.agents import (AssistantAgent,@@ -57,6 +39,23 @@ from autogen_agentchat.teams._group_chat._round_robin_group_chat import RoundRobfrom autogen_agentchat.teams._group_chat._selector_group_chat import SelectorGroupChatManagerfrom autogen_agentchat.teams._group_chat._swarm_group_chat import SwarmGroupChatManagerfrom autogen_agentchat.ui import Console+from autogen_core import AgentId, AgentRuntime, CancellationToken, FunctionCall, SingleThreadedAgentRuntime+from autogen_core.model_context import BufferedChatCompletionContext+from autogen_core.models import (+ AssistantMessage,+ CreateResult,+ FunctionExecutionResult,+ FunctionExecutionResultMessage,+ LLMMessage,+ RequestUsage,+ UserMessage,+)+from autogen_core.tools import FunctionTool+from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor+from autogen_ext.models.openai import OpenAIChatCompletionClient+from autogen_ext.models.replay import ReplayChatCompletionClient+from pydantic import BaseModel+from utils import FileLogHandlerlogger = logging.getLogger(EVENT_LOGGER_NAME)logger.setLevel(logging.DEBUG)diff --git a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.pyindex 9610e7f54..f806ad31f 100644--- a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py+++ b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport asyncioimport inspect+import jsonimport loggingimport sysimport uuid@@ -276,6 +277,55 @@ class SingleThreadedAgentRuntime(AgentRuntime):def _known_agent_names(self) -> Set[str]:return set(self._agent_factories.keys())+ async def _create_otel_attributes(+ self,+ sender_agent_id: AgentId | None = None,+ recipient_agent_id: AgentId | None = None,+ message_context: MessageContext | None = None,+ message: Any = None,+ ) -> Mapping[str, str]:+ """Create OpenTelemetry attributes for the given agent and message.++ Args:+ sender_agent (Agent, optional): The sender agent instance.+ recipient_agent (Agent, optional): The recipient agent instance.+ message (Any): The message instance.++ Returns:+ Attributes: A dictionary of OpenTelemetry attributes.+ """+ if not sender_agent_id and not recipient_agent_id and not message:+ return {}+ attributes: Dict[str, str] = {}+ if sender_agent_id:+ sender_agent = await self._get_agent(sender_agent_id)+ attributes["sender_agent_type"] = sender_agent.id.type+ attributes["sender_agent_class"] = sender_agent.__class__.__name__+ if recipient_agent_id:+ recipient_agent = await self._get_agent(recipient_agent_id)+ attributes["recipient_agent_type"] = recipient_agent.id.type+ attributes["recipient_agent_class"] = recipient_agent.__class__.__name__++ if message_context:+ serialized_message_context = {+ "sender": str(message_context.sender),+ "topic_id": str(message_context.topic_id),+ "is_rpc": message_context.is_rpc,+ "message_id": message_context.message_id,+ }+ attributes["message_context"] = json.dumps(serialized_message_context)++ if message:+ try:+ serialized_message = self._try_serialize(message)+ except Exception as e:+ serialized_message = str(e)+ else:+ serialized_message = "No Message"+ attributes["message"] = serialized_message++ return attributes+# Returns the response of the messageasync def send_message(self,@@ -440,7 +490,17 @@ class SingleThreadedAgentRuntime(AgentRuntime):cancellation_token=message_envelope.cancellation_token,message_id=message_envelope.message_id,)- with self._tracer_helper.trace_block("process", recipient_agent.id, parent=message_envelope.metadata):+ with self._tracer_helper.trace_block(+ "process",+ recipient_agent.id,+ parent=message_envelope.metadata,+ attributes=await self._create_otel_attributes(+ sender_agent_id=message_envelope.sender,+ recipient_agent_id=recipient,+ message_context=message_context,+ message=message_envelope.message,+ ),+ ):with MessageHandlerContext.populate_context(recipient_agent.id):response = await recipient_agent.on_message(message_envelope.message,@@ -527,7 +587,17 @@ class SingleThreadedAgentRuntime(AgentRuntime):agent = await self._get_agent(agent_id)async def _on_message(agent: Agent, message_context: MessageContext) -> Any:- with self._tracer_helper.trace_block("process", agent.id, parent=message_envelope.metadata):+ with self._tracer_helper.trace_block(+ "process",+ agent.id,+ parent=message_envelope.metadata,+ attributes=await self._create_otel_attributes(+ sender_agent_id=message_envelope.sender,+ recipient_agent_id=agent.id,+ message_context=message_context,+ message=message_envelope.message,+ ),+ ):with MessageHandlerContext.populate_context(agent.id):try:return await agent.on_message(@@ -557,7 +627,16 @@ class SingleThreadedAgentRuntime(AgentRuntime):# TODO if responses are given for a publishasync def _process_response(self, message_envelope: ResponseMessageEnvelope) -> None:- with self._tracer_helper.trace_block("ack", message_envelope.recipient, parent=message_envelope.metadata):+ with self._tracer_helper.trace_block(+ "ack",+ message_envelope.recipient,+ parent=message_envelope.metadata,+ attributes=await self._create_otel_attributes(+ sender_agent_id=message_envelope.sender,+ recipient_agent_id=message_envelope.recipient,+ message=message_envelope.message,+ ),+ ):content = (message_envelope.message.__dict__if hasattr(message_envelope.message, "__dict__")diff --git a/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py b/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.pyindex b374f5371..81c890efa 100644--- a/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py+++ b/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py@@ -1,8 +1,8 @@# mypy: disable-error-code="no-any-unimported"import asyncioimport os-import sysimport shutil+import sysimport tempfilefrom pathlib import Pathfrom typing import AsyncGenerator, TypeAlias