使用 Galileo 进行智能体可观测性和评估¶
Supported in ADKPython
Galileo 是一个 AI 评估和可观测性平台,为 AI 应用程序提供端到端的追踪、评估和监控。Galileo 支持从 ADK 直接导入 OpenTelemetry (OTel) 追踪数据,包括智能体运行、工具调用和模型请求。
如果你想了解更多信息,请参阅 Galileo 官方提供的 Google ADK 集成 文档。
前提条件¶
- 拥有一个 Galileo API 密钥。
- 创建一个 Galileo 项目和日志流(Log stream)。
- 拥有一个 Gemini API 密钥。
安装依赖¶
或者,你也可以使用完整示例项目中的 requirements.txt 进行安装。
设置环境变量¶
配置以下相关环境变量:
# Gemini 环境变量
GOOGLE_GENAI_USE_VERTEXAI=0
GOOGLE_API_KEY="你的_API_密钥"
# Galileo 环境变量
GALILEO_API_KEY="你的_API_密钥"
GALILEO_PROJECT="你的_项目名称"
GALILEO_LOG_STREAM="你的_日志流名称"
配置 OpenTelemetry(必选)¶
在开始使用任何 ADK 组件之前,你必须配置一个 OTLP 导出器(Exporter)并设置全局追踪器提供程序(Tracer Provider),以便将追踪数据发送到 Galileo。
# my_agent/agent.py
from dotenv import load_dotenv
load_dotenv()
# OpenTelemetry 导入
from opentelemetry.sdk import trace as trace_sdk
# Galileo Span 处理器(会自动根据环境变量配置 OTLP 标头和端点)
from galileo import otel
# 针对 ADK 的 OpenInference 仪表化
from openinference.instrumentation.google_adk import GoogleADKInstrumentor
# 创建追踪器提供程序并注册 Galileo Span 处理器
tracer_provider = trace_sdk.TracerProvider()
galileo_span_processor = otel.GalileoSpanProcessor()
tracer_provider.add_span_processor(galileo_span_processor)
# 使用 OpenInference 对 Google ADK 进行仪表化(这将捕获输入/输出)
GoogleADKInstrumentor().instrument(tracer_provider=tracer_provider)
示例:追踪一个 ADK 智能体¶
在完成 OTLP 导出器和追踪器提供程序的配置后,你可以编写一个简单的具有获取城市时间功能的智能体:
# my_agent/agent.py
from google.adk.agents import Agent
def get_current_time(city: str) -> dict:
"""返回指定城市的当前时间。"""
return {"status": "success", "city": city, "time": "10:30 AM"}
root_agent = Agent(
model="gemini-2.0-flash",
name="root_agent",
description="告知指定城市的当前时间。",
instruction=(
"你是一个乐于助人的助手,能够告知城市中的当前时间。"
"为此请使用 'get_current_time' 工具。"
),
tools=[get_current_time],
)
运行该智能体:
与智能体交互:
查看完整的 Google ADK + OpenTelemetry 示例项目 以获取完整代码实现。
在 Galileo 中查看追踪结果¶
选择你的项目,然后在日志流(Log Stream)中检查追踪记录(Traces)和跨度(Spans)。

相关资源¶
- Galileo Google ADK 集成官方文档:详细说明了如何使用 OpenTelemetry 和 OpenInference 将 Google ADK 项目与 Galileo 进行集成。
- Google ADK + OpenTelemetry 示例项目:这是一个演示如何在 Google ADK 中使用 Galileo 的配套应用。该示例是基于面向 Python 的 Google ADK 快速入门进行 Galileo 仪表化后的完整版本。