Skip to content

智能体的可观测性

Supported in ADKPython v0.1.0Go v0.1.0Kotlin v0.1.0

智能体的可观测性通过分析其外部遥测数据和结构化日志,实现对系统内部状态的度量,包括推理追踪、工具调用和潜在模型输出。在构建智能体时,你可能需要这些功能来帮助调试和诊断其进程内行为。对于任何具有显著复杂性的智能体,基本的输入和输出监控通常是不够的。

Agent Development Kit (ADK) 通过日志指标追踪提供内置的可观测性,帮助你监控和调试智能体。然而,对于监控和分析,你可能需要考虑更高级的可观测性 ADK 集成

快速开始:在 Kotlin 中启用可观测性

在 Kotlin 中,你可以通过为追踪配置 OpenTelemetry 并使用 LoggingPlugin 获取详细控制台输出来启用全面的可观测性。

// 1. Configure OpenTelemetry (Traces)
// ADK Kotlin uses GlobalOpenTelemetry to resolve its tracer on the JVM.
val spanExporter = OtlpGrpcSpanExporter.builder().setEndpoint("http://localhost:4317").build()

val resource =
    Resource.getDefault()
        .merge(
            Resource.create(
                Attributes.of(AttributeKey.stringKey("service.name"), "my-kotlin-agent"),
            ),
        )

val tracerProvider =
    SdkTracerProvider.builder()
        .addSpanProcessor(BatchSpanProcessor.builder(spanExporter).build())
        .setResource(resource)
        .build()

OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).buildAndRegisterGlobal()

// 2. Optional: Configure ADK Telemetry behavior
// Enable capturing full message content in traces (use with caution in production)
TelemetryConfig.captureMessageContent = true

// 3. Initialize Agent and Runner with LoggingPlugin for console output
val agent = LlmAgent(name = "my_agent", model = Gemini(name = "gemini-flash-latest"))

val runner =
    InMemoryRunner(agent = agent, pluginManager = PluginManager(listOf(LoggingPlugin())))

// The runner will now automatically emit traces via GlobalOpenTelemetry
// and log activity to the console via the LoggingPlugin.
runner.run(
    userId = "user123",
    sessionId = "session456",
    newMessage = Content.fromText(Role.USER, "Hello!"),
)

ADK 可观测性集成

有关预置的 ADK 可观测性库列表,请参阅工具与集成