本文档概述了如何使用 OpenTelemetry 对 LangGraph ReAct 智能体进行插桩,从而从智能体收集遥测数据。用户提示、智能体回答和选择项作为附加到 span 的属性包含在遥测数据中。与包含生成式 AI 事件的 span 相关联的日志条目中也会包含智能体回答。当智能体使用 Langchain 的 ChatVertexAI 调用 Gemini 模型时,本文档中的说明适用。
对生成式 AI 应用进行插桩以收集遥测数据
如需对生成型 AI 应用进行插桩以收集日志、指标和跟踪记录数据,请执行以下操作:
安装 OpenTelemetry 软件包
添加以下 OpenTelemetry 插桩和导出器软件包:
pip install 'opentelemetry-instrumentation-vertexai>=2.0b0' \
'opentelemetry-instrumentation-sqlite' \
'opentelemetry-exporter-gcp-logging' \
'opentelemetry-exporter-gcp-monitoring' \
'opentelemetry-exporter-otlp-proto-grpc'
日志和指标数据使用 Cloud Logging API 或 Cloud Monitoring API 发送到您的 Google Cloud 项目。opentelemetry-exporter-gcp-logging
和 opentelemetry-exporter-gcp-monitoring
库会调用这些 API 中的端点。
跟踪数据使用支持 OTLP 格式的遥测 (OTLP) API 发送到 Google Cloud 。通过此端点接收的数据也会以 OTLP 格式存储。
opentelemetry-exporter-otlp-proto-grpc
库会调用遥测 (OTLP) API 端点。
配置 OpenTelemetry 以收集和发送遥测数据
在 LangGraph 智能体的初始化代码中,将 OpenTelemetry 配置为捕获遥测数据并将其发送到您的 Google Cloud 项目:
如需查看完整示例,请点击 more_vert 更多,然后选择在 GitHub 上查看。
跟踪生成式 AI 智能体的调用情况
如需跟踪 LangGraph 智能体调用的执行情况,请围绕智能体调用创建自定义 span:
如需查看完整示例,请点击 more_vert 更多,然后选择在 GitHub 上查看。
您可能需要将上述代码添加到应用代码中的关键位置。
如需详细了解如何添加自定义 span 和指标,请参阅向应用添加自定义跟踪记录和指标。
运行示例
此示例是一个使用 OpenTelemetry 插桩的 LangGraph 智能体,用于将包含生成式 AI 提示和回答以及指标的跟踪记录和日志发送到您的Google Cloud 项目。
LangGraph 智能体角色
LangGraph 智能体定义为可完全访问临时 SQLite 数据库的 SQL 专家。该智能体使用 LangGraph 预构建的 ReAct 智能体实现,并使用 SQLDatabaseToolkit 访问最初为空的数据库。
准备工作
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
Enable the Vertex AI, Telemetry, Cloud Logging, Cloud Monitoring, and Cloud Trace APIs:
gcloud services enable aiplatform.googleapis.com
telemetry.googleapis.com logging.googleapis.com monitoring.googleapis.com cloudtrace.googleapis.com -
如需获得让示例应用写入日志、指标和跟踪记录数据所需的权限,请让管理员为您授予项目的以下 IAM 角色:
-
Cloud Telemetry Traces Writer (
roles/telemetry.tracesWriter
) -
Logs Writer (
roles/logging.logWriter
) -
Monitoring Metric Writer (
roles/monitoring.metricWriter
) -
Vertex AI User (
roles/aiplatform.user
)
-
Cloud Telemetry Traces Writer (
运行示例
如需运行示例,请执行以下操作:
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
克隆代码库:
git clone https://github.com/GoogleCloudPlatform/opentelemetry-operations-python.git
转到示例目录:
cd opentelemetry-operations-python/samples/langgraph-sql-agent
配置环境变量:
# Capture GenAI prompts and responses export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true # Capture application logs automatically export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
创建虚拟环境并运行示例:
python -m venv venv/ source venv/bin/activate pip install -r requirements.txt python main.py
应用会显示类似以下内容的消息:
Starting agent using ephemeral SQLite DB.
如需创建数据库,请在 Talk to the SQL agent >> 提示中输入一个值,然后按 Enter 键。
然后,智能体执行的操作会显示在您的 Cloud Shell 中。
以下示例展示了用户与应用之间的互动:
Talk to the SQL agent >> Create a new table to hold weather data. 👤 User: Create a new table to hold weather data. 🤖 Agent: I need to know what columns the table should have. What information about the weather do you want to store? For example, I could include columns for date, location, temperature, humidity, and precipitation. Talk to the SQL agent >> Create a new table to hold weather data. Include date, location, temperature, humidity, and precipitation. 👤 User: Create a new table to hold weather data. Include date, location, temperature, humidity, and precipitation. 🤖 Agent CREATE TABLE weather ( date DATE, location VARCHAR(255), temperature REAL, humidity REAL, precipitation REAL );
如需退出,请输入
Ctrl-C
。
生成式 AI 智能体执行的操作不是确定性的,因此您可能会看到针对同一提示的不同回答。
查看跟踪记录、指标和日志
本部分介绍如何查看生成式 AI 事件。
准备工作
如需获得查看日志、指标和跟踪记录数据所需的权限,请让管理员为您授予项目的以下 IAM 角色:
-
Logs Viewer (
roles/logging.viewer
) -
Monitoring Viewer (
roles/monitoring.viewer
) -
Cloud Trace User (
roles/cloudtrace.user
)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
查看遥测数据
如需查看生成式 AI 事件,请使用 Trace 探索器页面:
-
在 Google Cloud 控制台中,前往 Trace 探索器页面:
您也可以使用搜索栏查找此页面。
在工具栏中,依次选择添加过滤条件、Span 名称和
invoke agent
。运行示例部分包含一个执行示例,其中向应用发送了两个提示。下面展示了过滤数据后的 Trace 探索器页面:
如果您之前从未使用过 Cloud Trace,Google Cloud Observability 需要创建一个数据库来存储跟踪记录数据。创建数据库可能需要几分钟时间,在此期间,您无法查看任何跟踪记录数据。
如需浏览 span 和日志数据,请在 Span 表格中选择 span。
系统会打开详细信息页面。此页面会显示关联的跟踪记录及其 span。页面上的表格会显示您所选 span 的详细信息。这些信息包括:
GenAI 标签页会显示生成式 AI 智能体的事件。如需详细了解这些事件,请参阅查看生成式 AI 事件。
以下屏幕截图展示了一条跟踪记录,其中一个 span 的名称为
invoke_agent
。该 span 会调用 Gemini。 Gemini span 包含以下生成式 AI 事件:日志和事件标签页会列出与该 span 关联的日志条目和事件。如果您想在 Logs Explorer 中查看日志数据,请在该标签页的工具栏中选择查看日志。
日志数据包含 LangGraph 智能体的回答。例如,对于示例运行,JSON 载荷包含以下内容:
{ logName: "projects/my-project/logs/otel_python_inprocess_log_name_temp" jsonPayload: { finish_reason: "stop" message: { role: "model" content: [ 0: { text: "I need to know what columns the table should have. What information about the weather do you want to store? For example, I could include columns for date, location, temperature, humidity, and precipitation." } ] } index: 0 } ... }
当前示例经过插桩,可将指标数据发送到您的 Google Cloud 项目,但不会生成任何指标。