Class Telemetry
-
Method Summary
Modifier and TypeMethodDescriptionstatic io.opentelemetry.api.trace.TracerGets the tracer.static voidsetTracerForTesting(io.opentelemetry.api.trace.Tracer tracer) Sets the OpenTelemetry instance to be used for tracing.static voidtraceCallLlm(InvocationContext invocationContext, String eventId, LlmRequest llmRequest, LlmResponse llmResponse) Traces a call to the 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) Executes a Flowable with an OpenTelemetry Scope active for its entire lifecycle.static voidtraceSendData(InvocationContext invocationContext, String eventId, List<com.google.genai.types.Content> data) Traces the sending of data (history or new content) to the agent/model.static voidtraceToolCall(Map<String, Object> args) Traces tool call arguments.static voidtraceToolResponse(InvocationContext invocationContext, String eventId, Event functionResponseEvent) Traces tool response event.
-
Method Details
-
setTracerForTesting
public static void setTracerForTesting(io.opentelemetry.api.trace.Tracer tracer) Sets the OpenTelemetry instance to be used for tracing. This is for testing purposes only. -
traceToolCall
-
traceToolResponse
public static void traceToolResponse(InvocationContext invocationContext, String eventId, Event functionResponseEvent) Traces tool response event.- Parameters:
invocationContext- The invocation context for the current agent run.eventId- The ID of the event.functionResponseEvent- The function response event.
-
traceCallLlm
public static void traceCallLlm(InvocationContext invocationContext, String eventId, LlmRequest llmRequest, LlmResponse llmResponse) Traces a call to the 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(InvocationContext invocationContext, String eventId, List<com.google.genai.types.Content> data) Traces the sending of data (history or new content) to the agent/model.- Parameters:
invocationContext- The invocation context.eventId- The ID of the event, if applicable.data- A list of content objects being sent.
-
getTracer
public static io.opentelemetry.api.trace.Tracer getTracer()Gets the 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) Executes a Flowable with an OpenTelemetry Scope active for its entire lifecycle.This helper manages the OpenTelemetry Scope lifecycle for RxJava Flowables to ensure proper context propagation across async boundaries. The scope remains active from when the Flowable is returned through all operators until stream completion (onComplete, onError, or cancel).
Why not try-with-resources? RxJava Flowables execute lazily - operators run at subscription time, not at chain construction time. Using try-with-resources would close the scope before the Flowable subscribes, causing Context.current() to return ROOT in nested operations and breaking parent-child span relationships (fragmenting traces).
The scope is properly closed via doFinally when the stream terminates, ensuring no resource leaks regardless of completion mode (success, error, or cancellation).
- 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 scope lifecycle management
-