使用 Google ADK构建的观察器
Google 代理开发工具包(ADK)是一个用于构建、协调和部署基于智能代理的应用程序的框架。 它提供了一种结构化且可扩展的方式,用于定义代理工作流、集成工具和外部服务,以及管理代理的推理和执行。 ADK 旨在通过提供任务协调、工具调用以及与大型语言模型交互的抽象层,来支持生产就绪的人工智能代理。
使用 Instana 监控 Google ADK应用程序,可为基于代理的工作流提供全面的端到端可观测性。 通过对 ADK 应用程序进行性能监控,您可以捕获涵盖代理编排逻辑、工具调用、外部服务调用以及底层基础设施的详细执行路径。
先决条件
确保满足下列先决条件:
Python 3.8 已安装该版本或更高版本。
用于访问Gemini模型的 Google API 密钥,请参阅 Google AI Studio。
Instana 已根据您的应用程序进行配置,请参阅 “入门指南 ”。
为 Google ADK应用程序添加性能监控
要为您的 Google ADK应用程序配置 Instana 可观测性,请完成以下步骤:
安装 Google ADK所需的软件包:
pip install google-adk traceloop-sdk创建一个新的 Google ADK 代理项目:
adk create test_agent这将创建一个具有以下结构的项目,其中
agent.py包含代理的核心控制逻辑:图 1. 代理文件结构 
在
.env文件中设置您的 Google API 密钥:GOOGLE_API_KEY=<your-google-api-key>要创建或管理您的 API 密钥,请访问 Google AI Studio。
使用以下代码更新该
agent.py文件,以创建一个采用 OpenLLMetry 监控功能的多代理应用程序:from google.adk.agents.llm_agent import Agent from traceloop.sdk import Traceloop # Initialize OpenLLMetry with app name Traceloop.init(app_name="multi_agent_adk_app") # Tool implementations def get_current_time(city: str) -> dict: """Returns the current time for a given city.""" return {"city": city, "time": "10:30 AM"} def get_weather(city: str) -> dict: """Returns the current weather for a given city.""" return {"city": city, "weather": "Sunny", "temperature": "30°C"} # Time Agent time_agent = Agent( name="time_agent", model="gemini-2.0-flash-exp", description="Provides the current time for a city.", instruction=( "You provide the current time for a given city. " "Use the get_current_time tool to fetch the information." ), tools=[get_current_time], ) # Weather Agent weather_agent = Agent( name="weather_agent", model="gemini-2.0-flash-exp", description="Provides the current weather for a city.", instruction=( "You provide the current weather details for a given city. " "Use the get_weather tool to fetch the information." ), tools=[get_weather], ) # Root Agent orchestrating other agents root_agent = Agent( name="root_agent", model="gemini-2.0-flash-exp", description="Orchestrates multiple agents to answer user queries.", instruction=( "You are a coordinating agent. Based on the user request, " "delegate tasks to the appropriate agent (time_agent or weather_agent) " "and combine the responses into a single answer." ), sub_agents=[time_agent, weather_agent], )此示例创建了三个代理:
time_agent :提供各城市的当前时间
weather_agent :提供各城市的天气信息
root_agent :协调其他代理处理用户请求
启动 Google ADK代理:
adk run test_agent该代理将启动并等待用户输入。 你可以通过提问来与它互动,例如:“纽约的天气怎么样?” 或者“伦敦现在几点了?”
在 Instana 中查看数据。
运行应用程序并与代理进行交互后,数据将显示在 Instana 的Gen AI可观测性仪表盘中:
图 2. 对用户请求的回复 
图 3. Instana 中的痕迹 
仪表盘显示:
代理协调流程
工具调用(get_current_time、get_weather)
LLM 与 Gemini 模型的交互
响应时间和令牌使用情况
故障诊断
对于“轨迹未显示”或“连接错误”等常见问题,请参阅 “故障排除 ”。
Google API 主要错误
问题 :运行代理时出现身份验证错误。
要解决此问题,请尝试以下步骤:
请确认您的 Google API 密钥已在 文件
.env中正确设置确保 API 密钥具有访问Gemini模型的权限
请确认该
.env文件位于项目根目录下