跳转到内容

代理程序的可观测性(Observability for agents)

在 ADK 中得到支持。Python v0.1.0Go v0.1.0Kotlin v0.1.0

代理的可观测性(Observability)允许通过分析系统的外部遥测数据(telemetry)和结构化日志(structured logs),来测量系统的内部状态,包括推理过程(reasoning traces)、工具调用(tool calls)以及潜在模型的输出(latent model outputs)。在开发代理程序时,这些功能对于帮助调试和诊断代理程序在运行过程中的行为至关重要。对于那些具有较高复杂性的代理程序来说,仅依靠基本的输入/输出监控(input/output monitoring)通常是远远不够的。

代理开发工具包(Agent Development Kit,简称 ADK)提供了内置的监控(observability)功能,帮助开发者更好地了解系统的运行状态和性能表现。 日志记录(logging), metrics,以及 痕迹;踪迹 这些工具可以帮助您监控和调试您的代理程序(agents)。不过,您可能还需要考虑一些更高级的解决方案。 可观测性(Observability)与 ADK(Application Development Kit)的集成 用于监控和分析。

快速入门:在 Kotlin 中启用可观测性(Observability)功能

在 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(
        App(appName = "my_agent", rootAgent = agent, plugins = 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 的预构建可观测性(observability)库的列表,请参见…… 工具与集成(Tools and Integrations).