遥测

ADK-Rust 通过 adk-telemetry crate 提供生产级可观测性,该 crate 使用 tracing 生态系统和 OpenTelemetry 集成了结构化日志和分布式追踪。

概述

遥测系统支持:

  • 结构化日志: 带有上下文信息的丰富、可查询日志
  • 分布式追踪: 跨 Agent 层次结构和服务边界跟踪请求
  • OpenTelemetry 集成: 将追踪导出到可观测性后端(Jaeger、Datadog、Honeycomb 等)
  • 自动上下文传播: Session、user 和 invocation ID 流经所有操作
  • 预配置 Span: 用于常见 ADK 操作的辅助函数

快速入门

基本控制台日志

对于开发和简单部署,初始化控制台日志:

use adk_telemetry::init_telemetry;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize telemetry with your service name
    init_telemetry("my-agent-service")?;
    
    // Your agent code here
    
    Ok(())
}

这会将结构化日志配置为标准输出,并使用合理的默认值。

OpenTelemetry 导出

对于带有分布式追踪的生产部署:

use adk_telemetry::init_with_otlp;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize with OTLP exporter
    init_with_otlp("my-agent-service", "http://localhost:4317")?;
    
    // Your agent code here
    
    // Flush traces before exit
    adk_telemetry::shutdown_telemetry();
    Ok(())
}

这会将追踪和指标导出到 OpenTelemetry 收集器端点。

日志级别

使用 RUST_LOG 环境变量控制日志详细程度:

级别描述用例
error仅错误生产环境(最少)
warn警告和错误生产环境(默认)
info信息性消息开发、测试环境
debug详细调试信息本地开发
trace非常详细的追踪深度调试

设置日志级别

# Set global log level
export RUST_LOG=info

# Set per-module log levels
export RUST_LOG=adk_agent=debug,adk_model=info

# Combine global and module-specific levels
export RUST_LOG=warn,adk_agent=debug

如果未设置 RUST_LOG,遥测系统默认为 info 级别。

日志宏

使用标准的 tracing 宏进行日志记录:

use adk_telemetry::{trace, debug, info, warn, error};

// Informational logging
info!("Agent started successfully");

// Structured logging with fields
info!(
    agent.name = "my_agent",
    session.id = "sess-123",
    "Processing user request"
);

// Debug logging
debug!(user_input = ?input, "Received input");

// Warning and error logging
warn!("Rate limit approaching");
error!(error = ?err, "Failed to call model");

结构化字段

为日志消息添加上下文字段,以便更好地进行过滤和分析:

use adk_telemetry::info;

info!(
    agent.name = "customer_support",
    user.id = "user-456",
    session.id = "sess-789",
    invocation.id = "inv-abc",
    "Agent execution started"
);

这些字段在您的可观测性后端中变得可查询。

遥测

自动遥测

使用 #[instrument] 属性为函数自动创建 span:

use adk_telemetry::{instrument, info};

#[instrument]
async fn process_request(user_id: &str, message: &str) {
    info!("Processing request");
    // Function logic here
}

// 创建一个名为 "process_request" 的 span,包含 user_id 和 message 作为字段

跳过敏感参数

从 traces 中排除敏感数据:

use adk_telemetry::instrument;

#[instrument(skip(api_key))]
async fn call_external_api(api_key: &str, query: &str) {
    // api_key 不会出现在 traces 中
}

自定义 Span 名称

use adk_telemetry::instrument;

#[instrument(name = "external_api_call")]
async fn fetch_data(url: &str) {
    // Span 将被命名为 "external_api_call" 而不是 "fetch_data"
}

预配置 Span

ADK-Telemetry 为常见操作提供了辅助函数:

Agent 执行 Span

use adk_telemetry::agent_run_span;

let span = agent_run_span("my_agent", "inv-123");
let _enter = span.enter();

// Agent execution code here
// 此范围内的所有日志都将继承 span 上下文

模型调用 Span

use adk_telemetry::model_call_span;

let span = model_call_span("gemini-2.0-flash");
let _enter = span.enter();

// Model API call here

工具执行 Span

use adk_telemetry::tool_execute_span;

let span = tool_execute_span("weather_tool");
let _enter = span.enter();

// Tool execution code here

回调 Span

use adk_telemetry::callback_span;

let span = callback_span("before_model");
let _enter = span.enter();

// Callback logic here

添加上下文属性

将用户和会话上下文添加到当前 span:

use adk_telemetry::add_context_attributes;

add_context_attributes("user-456", "sess-789");

手动创建 Span

对于自定义遥测,手动创建 span:

use adk_telemetry::{info, Span};

let span = tracing::info_span!(
    "custom_operation",
    operation.type = "data_processing",
    operation.id = "op-123"
);

let _enter = span.enter();
info!("Performing custom operation");
// Operation code here

Span 属性

动态添加属性:

use adk_telemetry::Span;

let span = Span::current();
span.record("result.count", 42);
span.record("result.status", "success");

OpenTelemetry 配置

OTLP 端点

OTLP exporter 将 traces 发送到 collector 端点:

use adk_telemetry::init_with_otlp;

// Local Jaeger (default OTLP port)
init_with_otlp("my-service", "http://localhost:4317")?;

// Cloud provider endpoint
init_with_otlp("my-service", "https://otlp.example.com:4317")?;

运行本地 Collector

对于开发,运行支持 OTLP 的 Jaeger:

docker run -d --name jaeger \
  -p 4317:4317 \
  -p 16686:16686 \
  jaegertracing/all-in-one:latest

# View traces at http://localhost:16686

Trace 可视化

配置后,traces 将出现在您的可观测性后端,显示:

  • Agent 执行层级
  • Model 调用延迟
  • Tool 执行时间
  • 错误传播
  • 上下文流(用户 ID、会话 ID 等)

与 ADK 集成

当遥测系统初始化时,adk-rust 组件会自动发出遥测数据:

use adk_rust::prelude::*;
use adk_telemetry::init_telemetry;
use std::sync::Arc;

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    // Initialize telemetry first
    init_telemetry("my-agent-app")?;
    
    let api_key = std::env::var("GOOGLE_API_KEY")?;
    let model = Arc::new(GeminiModel::new(&api_key, "gemini-2.5-flash")?);
    
    let agent = LlmAgentBuilder::new("support_agent")
        .model(model)
        .instruction("You are a helpful support agent.")
        .build()?;
    
    // Use Launcher for simple execution
    Launcher::new(Arc::new(agent)).run().await?;
    
    Ok(())
}

Agent、Model 和 Tool 操作将自动发出结构化日志和跟踪。

Tool 中的自定义遥测

将遥测添加到自定义 Tool 中:

use adk_rust::prelude::*;
use adk_telemetry::{info, instrument, tool_execute_span};
use serde_json::{json, Value};

#[instrument(skip(ctx))]
async fn weather_tool_impl(
    ctx: Arc<dyn ToolContext>,
    args: Value,
) -> Result<Value> {
    let span = tool_execute_span("weather_tool");
    let _enter = span.enter();
    
    let location = args["location"].as_str().unwrap_or("unknown");
    info!(location = location, "Fetching weather data");
    
    // Tool logic here
    let result = json!({
        "temperature": 72,
        "condition": "sunny"
    });
    
    info!(location = location, "Weather data retrieved");
    Ok(result)
}

let weather_tool = FunctionTool::new(
    "get_weather",
    "Get current weather for a location",
    json!({
        "type": "object",
        "properties": {
            "location": {"type": "string"}
        },
        "required": ["location"]
    }),
    weather_tool_impl,
);

回调中的自定义遥测

向回调添加可观测性:

use adk_rust::prelude::*;
use adk_telemetry::{info, callback_span};
use std::sync::Arc;

let agent = LlmAgentBuilder::new("observed_agent")
    .model(model)
    .before_callback(Box::new(|ctx| {
        Box::pin(async move {
            let span = callback_span("before_agent");
            let _enter = span.enter();
            
            info!(
                agent.name = ctx.agent_name(),
                user.id = ctx.user_id(),
                session.id = ctx.session_id(),
                "Agent execution starting"
            );
            
            Ok(None)
        })
    }))
    .after_callback(Box::new(|ctx| {
        Box::pin(async move {
            let span = callback_span("after_agent");
            let _enter = span.enter();
            
            info!(
                agent.name = ctx.agent_name(),
                "Agent execution completed"
            );
            
            Ok(None)
        })
    }))
    .build()?;

性能考量

采样

对于高吞吐量系统,请考虑跟踪采样:

// Note: Sampling configuration depends on your OpenTelemetry setup
// Configure sampling in your OTLP collector or backend

异步 Span

始终在异步函数上使用 #[instrument] 以确保正确的 span 上下文:

use adk_telemetry::instrument;

// ✅ 正确 - span 上下文在 await 点之间得到保留
#[instrument]
async fn async_operation() {
    tokio::time::sleep(Duration::from_secs(1)).await;
}

// ❌ 不正确 - 手动 span 可能会丢失上下文
async fn manual_span_operation() {
    let span = tracing::info_span!("operation");
    let _enter = span.enter();
    tokio::time::sleep(Duration::from_secs(1)).await;
    // 在 await 之后上下文可能会丢失
}

生产环境中的日志级别

在生产环境中使用 infowarn 级别以减少开销:

export RUST_LOG=warn,my_app=info

故障排除

没有日志出现

  1. 检查 RUST_LOG 环境变量是否已设置
  2. 确保在任何日志记录之前调用了 init_telemetry()
  3. 验证遥测只初始化一次(内部使用 Once

跟踪未导出

  1. 验证 OTLP 端点可达
  2. 检查 collector 正在运行并接受连接
  3. 在应用程序退出前调用 shutdown_telemetry() 以刷新待处理的 spans
  4. 检查网络/防火墙问题

Spans 中缺少上下文

  1. 在 async 函数上使用 #[instrument]
  2. 确保使用 let _enter = span.enter() 进入 spans
  3. 在操作期间保持 _enter 守卫在作用域内

最佳实践

  1. 尽早初始化: 在 main() 开始时调用 init_telemetry()
  2. 使用结构化字段: 使用键值对添加上下文,而不是字符串插值
  3. 检测 async 函数: 始终在 async 函数上使用 #[instrument]
  4. 退出时刷新: 在应用程序终止前调用 shutdown_telemetry()
  5. 适当的日志级别: 对重要事件使用 info,对详细信息使用 debug
  6. 避免敏感数据: 使用 #[instrument(skip(...))] 跳过敏感参数
  7. 一致的命名: 使用一致的字段名(例如 user.idsession.id

相关


上一页: ← 事件 | 下一页: Launcher →