类追踪(Class Tracing)
java.lang.Object
com.google.adk.telemetry.Tracing
这是一个用于捕获和报告 ADK(Android Development Kit)中遥测数据的实用类。该类提供了多种方法,用于追踪代理程序执行过程中的各个环节,包括工具调用、工具响应、大语言模型(LLM)的交互过程以及数据处理的详细信息。该类利用 OpenTelemetry 技术来实现追踪和日志记录功能。这些追踪数据可以通过 ADK 开发服务器(ADK Dev Server)的用户界面进行导出。
-
嵌套类概述
Nested ClassesModifier and TypeClassDescriptionstatic final class一种变压器(transformer),能够在用户订阅该数据流(stream)的整个期间内,重新激活特定的上下文(context)。static final class一个用于管理 RxJava 流中的 OpenTelemetry “span”(数据片段)和 “scope”(数据范围)的转换器(transformer)。 -
方法概述
Modifier and TypeMethodDescriptionstatic io.opentelemetry.api.trace.Tracer用于获取追踪器(tracer)。static voidsetTracerForTesting(io.opentelemetry.api.trace.Tracer tracer) 设置用于追踪的 OpenTelemetry 实例。static <T> Tracing.TracerProvider<T> 返回一个用于追踪 RxJava 流执行过程的转换器(transformer)。static <T> Tracing.TracerProvider<T> traceAgent(String spanName, String agentName, String agentDescription, InvocationContext invocationContext) 已弃用(Deprecated)。static voidtraceAgentInvocation(io.opentelemetry.api.trace.Span span, String agentName, String agentDescription, InvocationContext invocationContext) 根据 OTEL semconv 版本 1.37 的规定,这些设置会立即应用于代理(agent)被调用时所具备的属性。static voidtraceCallLlm(io.opentelemetry.api.trace.Span span, InvocationContext invocationContext, String eventId, LlmRequest llmRequest, LlmResponse llmResponse, @Nullable Exception error) 记录对大型语言模型(LLM)的调用过程。static <T> io.reactivex.rxjava3.core.Flowable<T> traceFlowable(io.opentelemetry.context.Context spanContext, io.opentelemetry.api.trace.Span span, Supplier<io.reactivex.rxjava3.core.Flowable<T>> flowableSupplier) 执行一个Flowable“supplier within” 可理解为“公司内部的供应商”或“隶属于该公司的供应商”。spanContext并将该上下文通过订阅(subscription)和流式数据传输(stream emissions)的方式传播出去。withContext(Context).static voidtraceMergedToolCalls(io.opentelemetry.api.trace.Span span, String responseEventId, Event functionResponseEvent) 该工具可以合并多个工具调用事件(tool call events)的记录。static voidtraceSendData(io.opentelemetry.api.trace.Span span, InvocationContext invocationContext, String eventId, List<com.google.genai.types.Content> data) 记录数据(历史数据或新内容)被发送到代理/模型的过程。static voidtraceToolExecution(io.opentelemetry.api.trace.Span span, String toolName, String toolDescription, String toolType, Map<String, Object> args, @Nullable Event functionResponseEvent, @Nullable Throwable error) 该工具用于追踪程序的执行过程,包括程序接收的参数、程序返回的结果以及可能出现的任何错误。static <T> Tracing.ContextTransformer<T> withContext(io.opentelemetry.context.Context context) 返回一个转换器(transformer),该转换器会在订阅流(subscription stream)的有效期内重新激活指定的上下文(context)。
-
方法详细信息(Method Details)
-
setTracerForTesting
public static void setTracerForTesting(io.opentelemetry.api.trace.Tracer tracer) 用于设置用于追踪的 OpenTelemetry 实例。此功能仅用于测试目的。 -
traceAgentInvocation
public static void traceAgentInvocation(io.opentelemetry.api.trace.Span span, String agentName, String agentDescription, InvocationContext invocationContext) 根据 OTEL semconv 版本 1.37 的规定,这些设置会立即应用于代理(agent)被调用时所具备的属性。- Parameters:
span- Span on which attributes are set.agentName- Agent name from which attributes are gathered.agentDescription- Agent description from which attributes are gathered.invocationContext- InvocationContext from which attributes are gathered.
-
traceToolExecution
public static void traceToolExecution(io.opentelemetry.api.trace.Span span, String toolName, String toolDescription, String toolType, Map<String, Object> args, @Nullable Event functionResponseEvent, @Nullable Throwable error) 该工具用于追踪程序的执行过程,包括程序接收的参数、程序返回的结果以及可能出现的任何错误。- Parameters:
span- The span representing the tool execution.toolName- The name of the tool.toolDescription- The tool's description.toolType- The tool's type (e.g., "FunctionTool").args- The arguments passed to the tool.functionResponseEvent- The event containing the tool's response, if successful.error- The exception thrown during execution, if any.
-
traceCallLlm
public static void traceCallLlm(io.opentelemetry.api.trace.Span span, InvocationContext invocationContext, String eventId, LlmRequest llmRequest, LlmResponse llmResponse, @Nullable Exception error) 记录对大型语言模型(LLM)的调用过程。- Parameters:
invocationContext- The invocation context.eventId- The ID of the event associated with this LLM call/response.llmRequest- The LLM request object.llmResponse- The LLM response object.
-
traceSendData
public static void traceSendData(io.opentelemetry.api.trace.Span span, InvocationContext invocationContext, String eventId, List<com.google.genai.types.Content> data) 记录数据(历史数据或新内容)被发送到代理/模型的过程。- Parameters:
invocationContext- The invocation context.eventId- The ID of the event, if applicable.data- A list of content objects being sent.
-
traceMergedToolCalls
public static void traceMergedToolCalls(io.opentelemetry.api.trace.Span span, String responseEventId, Event functionResponseEvent) 该工具可以合并多个工具调用事件(tool call events)的记录。对于遥测(telemetry)目的而言,无需调用此函数。该函数的设置主要是为了防止那些用于调试或追踪请求(通常由网页用户界面发送的请求)被执行。
- Parameters:
responseEventId- The ID of the response event.functionResponseEvent- The merged response event.
-
getTracer
public static io.opentelemetry.api.trace.Tracer getTracer()用于获取追踪器(tracer)。- Returns:
- The tracer.
-
traceFlowable
public static <T> io.reactivex.rxjava3.core.Flowable<T> traceFlowable(io.opentelemetry.context.Context spanContext, io.opentelemetry.api.trace.Span span, Supplier<io.reactivex.rxjava3.core.Flowable<T>> flowableSupplier) 执行一个Flowable“supplier within” 可理解为“公司内部的供应商”或“隶属于该公司的供应商”。spanContext并将该上下文通过订阅(subscription)和流式数据传输(stream emissions)的方式传播出去。withContext(Context). 结束span当数据流终止或被取消时。- Type Parameters:
T- The type of items emitted by the Flowable- Parameters:
spanContext- The context containing the span to activatespan- The span to end when the stream completesflowableSupplier- Supplier that creates the Flowable to execute with active scope- Returns:
- Flowable with OpenTelemetry context propagation and span lifecycle management
-
trace
返回一个用于追踪 RxJava 流执行过程的转换器(transformer)。- Type Parameters:
T- The type of the stream.- Parameters:
spanName- The name of the span to create.- Returns:
- A TracerProvider that can be used with .compose().
-
traceAgent
@Deprecated public static <T> Tracing.TracerProvider<T> traceAgent(String spanName, String agentName, String agentDescription, InvocationContext invocationContext) Deprecated.返回一个用于追踪代理调用的转换器(transformer)。- Type Parameters:
T- The type of the stream.- Parameters:
spanName- The name of the span to create.agentName- The name of the agent.agentDescription- The description of the agent.invocationContext- The invocation context.- Returns:
- A TracerProvider configured for agent invocation.
-
withContext
public static <T> Tracing.ContextTransformer<T> withContext(io.opentelemetry.context.Context context) 返回一个转换器(transformer),该转换器会在订阅流(subscription stream)的有效期内重新激活指定的上下文(context)。- Type Parameters:
T- The type of the stream.- Parameters:
context- The context to re-activate.- Returns:
- A transformer that re-activates the context.
-