Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/adk/JsonBaseModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static ObjectMapper createObjectMapper() {
}

/** Serializes an object to a Json string. */
protected static String toJsonString(Object object) {
public static String toJsonString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/google/adk/agents/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.adk.Telemetry;
import com.google.adk.agents.Callbacks.AfterAgentCallback;
import com.google.adk.agents.Callbacks.BeforeAgentCallback;
import com.google.adk.events.Event;
import com.google.adk.plugins.Plugin;
import com.google.adk.telemetry.Tracing;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
Expand Down Expand Up @@ -222,7 +222,7 @@ private InvocationContext createInvocationContext(InvocationContext parentContex
* @return stream of agent-generated events.
*/
public Flowable<Event> runAsync(InvocationContext parentContext) {
Tracer tracer = Telemetry.getTracer();
Tracer tracer = Tracing.getTracer();
return Flowable.defer(
() -> {
Span span =
Expand All @@ -234,7 +234,7 @@ public Flowable<Event> runAsync(InvocationContext parentContext) {

InvocationContext invocationContext = createInvocationContext(parentContext);

return Telemetry.traceFlowable(
return Tracing.traceFlowable(
spanContext,
span,
() ->
Expand Down Expand Up @@ -361,7 +361,7 @@ private Single<Optional<Event>> callCallback(
* @return stream of agent-generated events.
*/
public Flowable<Event> runLive(InvocationContext parentContext) {
Tracer tracer = Telemetry.getTracer();
Tracer tracer = Tracing.getTracer();
return Flowable.defer(
() -> {
Span span =
Expand All @@ -373,7 +373,7 @@ public Flowable<Event> runLive(InvocationContext parentContext) {

InvocationContext invocationContext = createInvocationContext(parentContext);

return Telemetry.traceFlowable(spanContext, span, () -> runLiveImpl(invocationContext));
return Tracing.traceFlowable(spanContext, span, () -> runLiveImpl(invocationContext));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.adk.flows.llmflows;

import com.google.adk.Telemetry;
import com.google.adk.agents.ActiveStreamingTool;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.CallbackContext;
Expand All @@ -38,6 +37,7 @@
import com.google.adk.models.LlmRegistry;
import com.google.adk.models.LlmRequest;
import com.google.adk.models.LlmResponse;
import com.google.adk.telemetry.Tracing;
import com.google.adk.tools.ToolContext;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -178,7 +178,7 @@ private Flowable<LlmResponse> callLlm(
return Flowable.defer(
() -> {
Span llmCallSpan =
Telemetry.getTracer()
Tracing.getTracer()
.spanBuilder("call_llm")
.setParent(Context.current())
.startSpan();
Expand All @@ -199,7 +199,7 @@ private Flowable<LlmResponse> callLlm(
.doOnNext(
llmResp -> {
try (Scope innerScope = llmCallSpan.makeCurrent()) {
Telemetry.traceCallLlm(
Tracing.traceCallLlm(
context,
eventForCallbackUsage.id(),
llmRequestBuilder.build(),
Expand Down Expand Up @@ -474,7 +474,7 @@ public Flowable<Event> runLive(InvocationContext invocationContext) {
: Completable.defer(
() -> {
Span sendDataSpan =
Telemetry.getTracer()
Tracing.getTracer()
.spanBuilder("send_data")
.setParent(Context.current())
.startSpan();
Expand All @@ -484,7 +484,7 @@ public Flowable<Event> runLive(InvocationContext invocationContext) {
.doOnComplete(
() -> {
try (Scope innerScope = sendDataSpan.makeCurrent()) {
Telemetry.traceSendData(
Tracing.traceSendData(
invocationContext,
eventIdForSendData,
llmRequestAfterPreprocess.contents());
Expand All @@ -496,7 +496,7 @@ public Flowable<Event> runLive(InvocationContext invocationContext) {
StatusCode.ERROR, error.getMessage());
sendDataSpan.recordException(error);
try (Scope innerScope = sendDataSpan.makeCurrent()) {
Telemetry.traceSendData(
Tracing.traceSendData(
invocationContext,
eventIdForSendData,
llmRequestAfterPreprocess.contents());
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/com/google/adk/flows/llmflows/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.adk.Telemetry;
import com.google.adk.agents.ActiveStreamingTool;
import com.google.adk.agents.Callbacks.AfterToolCallback;
import com.google.adk.agents.Callbacks.BeforeToolCallback;
Expand All @@ -31,6 +30,7 @@
import com.google.adk.events.Event;
import com.google.adk.events.EventActions;
import com.google.adk.events.ToolConfirmation;
import com.google.adk.telemetry.Tracing;
import com.google.adk.tools.BaseTool;
import com.google.adk.tools.FunctionTool;
import com.google.adk.tools.ToolContext;
Expand Down Expand Up @@ -178,11 +178,11 @@ public static Maybe<Event> handleFunctionCalls(
var mergedEvent = maybeMergedEvent.get();

if (events.size() > 1) {
Tracer tracer = Telemetry.getTracer();
Tracer tracer = Tracing.getTracer();
Span mergedSpan =
tracer.spanBuilder("tool_response").setParent(Context.current()).startSpan();
try (Scope scope = mergedSpan.makeCurrent()) {
Telemetry.traceToolResponse(invocationContext, mergedEvent.id(), mergedEvent);
Tracing.traceToolResponse(invocationContext, mergedEvent.id(), mergedEvent);
} finally {
mergedSpan.end();
}
Expand Down Expand Up @@ -552,7 +552,7 @@ private static Maybe<Map<String, Object>> maybeInvokeAfterToolCall(

private static Maybe<Map<String, Object>> callTool(
BaseTool tool, Map<String, Object> args, ToolContext toolContext) {
Tracer tracer = Telemetry.getTracer();
Tracer tracer = Tracing.getTracer();
return Maybe.defer(
() -> {
Span span =
Expand All @@ -561,7 +561,7 @@ private static Maybe<Map<String, Object>> callTool(
.setParent(Context.current())
.startSpan();
try (Scope scope = span.makeCurrent()) {
Telemetry.traceToolCall(args);
Tracing.traceToolCall(args);
return tool.runAsync(args, toolContext)
.toMaybe()
.doOnError(span::recordException)
Expand All @@ -579,7 +579,7 @@ private static Event buildResponseEvent(
Map<String, Object> response,
ToolContext toolContext,
InvocationContext invocationContext) {
Tracer tracer = Telemetry.getTracer();
Tracer tracer = Tracing.getTracer();
Span span =
tracer
.spanBuilder("tool_response [" + tool.name() + "]")
Expand Down Expand Up @@ -615,7 +615,7 @@ private static Event buildResponseEvent(
.build()))
.actions(toolContext.eventActions())
.build();
Telemetry.traceToolResponse(invocationContext, event.id(), event);
Tracing.traceToolResponse(invocationContext, event.id(), event);
return event;
} finally {
span.end();
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/google/adk/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.adk.runner;

import com.google.adk.Telemetry;
import com.google.adk.agents.ActiveStreamingTool;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.InvocationContext;
Expand All @@ -39,6 +38,7 @@
import com.google.adk.summarizer.EventsCompactionConfig;
import com.google.adk.summarizer.LlmEventSummarizer;
import com.google.adk.summarizer.SlidingWindowEventCompactor;
import com.google.adk.telemetry.Tracing;
import com.google.adk.tools.BaseTool;
import com.google.adk.tools.FunctionTool;
import com.google.adk.utils.CollectionUtils;
Expand Down Expand Up @@ -450,7 +450,7 @@ public Flowable<Event> runAsync(
RunConfig runConfig,
@Nullable Map<String, Object> stateDelta) {
Span span =
Telemetry.getTracer().spanBuilder("invocation").setParent(Context.current()).startSpan();
Tracing.getTracer().spanBuilder("invocation").setParent(Context.current()).startSpan();
Context spanContext = Context.current().with(span);

try {
Expand All @@ -465,7 +465,7 @@ public Flowable<Event> runAsync(
.userContent(newMessage)
.build();

return Telemetry.traceFlowable(
return Tracing.traceFlowable(
spanContext,
span,
() ->
Expand Down Expand Up @@ -645,7 +645,7 @@ private InvocationContext.Builder newInvocationContextBuilder(Session session) {
public Flowable<Event> runLive(
Session session, LiveRequestQueue liveRequestQueue, RunConfig runConfig) {
Span span =
Telemetry.getTracer().spanBuilder("invocation").setParent(Context.current()).startSpan();
Tracing.getTracer().spanBuilder("invocation").setParent(Context.current()).startSpan();
Context spanContext = Context.current().with(span);

try {
Expand All @@ -668,7 +668,7 @@ public Flowable<Event> runLive(

return invocationContextSingle.flatMapPublisher(
updatedInvocationContext ->
Telemetry.traceFlowable(
Tracing.traceFlowable(
spanContext,
span,
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

package com.google.adk;
package com.google.adk.telemetry;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.adk.JsonBaseModel;
import com.google.adk.agents.InvocationContext;
import com.google.adk.events.Event;
import com.google.adk.models.LlmRequest;
Expand Down Expand Up @@ -47,18 +48,22 @@
* LLM interactions, and data handling. It leverages OpenTelemetry for tracing and logging for
* detailed information. These traces can then be exported through the ADK Dev Server UI.
*/
public class Telemetry {
public class Tracing {

private static final Logger log = LoggerFactory.getLogger(Telemetry.class);
private static final Logger log = LoggerFactory.getLogger(Tracing.class);

@SuppressWarnings("NonFinalStaticField")
private static Tracer tracer = GlobalOpenTelemetry.getTracer("gcp.vertex.agent");
private static Tracer tracer = GlobalOpenTelemetry.getTracer("com.google.adk");

private Telemetry() {}
private static final boolean CAPTURE_MESSAGE_CONTENT_IN_SPANS =
Boolean.parseBoolean(
System.getenv().getOrDefault("ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS", "true"));

private Tracing() {}

/** Sets the OpenTelemetry instance to be used for tracing. This is for testing purposes only. */
public static void setTracerForTesting(Tracer tracer) {
Telemetry.tracer = tracer;
Tracing.tracer = tracer;
}

/**
Expand All @@ -73,10 +78,9 @@ public static void traceToolCall(Map<String, Object> args) {
return;
}

span.setAttribute("gen_ai.system", "gcp.vertex.agent");
span.setAttribute("gen_ai.system", "com.google.adk");
try {
span.setAttribute(
"gcp.vertex.agent.tool_call_args", JsonBaseModel.getMapper().writeValueAsString(args));
span.setAttribute("adk.tool_call_args", JsonBaseModel.getMapper().writeValueAsString(args));
} catch (JsonProcessingException e) {
log.warn("traceToolCall: Failed to serialize tool call args to JSON", e);
}
Expand All @@ -97,16 +101,16 @@ public static void traceToolResponse(
return;
}

span.setAttribute("gen_ai.system", "gcp.vertex.agent");
span.setAttribute("gcp.vertex.agent.invocation_id", invocationContext.invocationId());
span.setAttribute("gcp.vertex.agent.event_id", eventId);
span.setAttribute("gcp.vertex.agent.tool_response", functionResponseEvent.toJson());
span.setAttribute("gen_ai.system", "com.google.adk");
span.setAttribute("adk.invocation_id", invocationContext.invocationId());
span.setAttribute("adk.event_id", eventId);
span.setAttribute("adk.tool_response", functionResponseEvent.toJson());

// Setting empty llm request and response (as the AdkDevServer UI expects these)
span.setAttribute("gcp.vertex.agent.llm_request", "{}");
span.setAttribute("gcp.vertex.agent.llm_response", "{}");
span.setAttribute("adk.llm_request", "{}");
span.setAttribute("adk.llm_response", "{}");
if (invocationContext.session() != null && invocationContext.session().id() != null) {
span.setAttribute("gcp.vertex.agent.session_id", invocationContext.session().id());
span.setAttribute("adk.session_id", invocationContext.session().id());
}
}

Expand Down Expand Up @@ -158,26 +162,31 @@ public static void traceCallLlm(
return;
}

span.setAttribute("gen_ai.system", "gcp.vertex.agent");
span.setAttribute("gen_ai.system", "com.google.adk");
llmRequest.model().ifPresent(modelName -> span.setAttribute("gen_ai.request.model", modelName));
span.setAttribute("gcp.vertex.agent.invocation_id", invocationContext.invocationId());
span.setAttribute("gcp.vertex.agent.event_id", eventId);
span.setAttribute("adk.invocation_id", invocationContext.invocationId());
span.setAttribute("adk.event_id", eventId);

if (invocationContext.session() != null && invocationContext.session().id() != null) {
span.setAttribute("gcp.vertex.agent.session_id", invocationContext.session().id());
span.setAttribute("adk.session_id", invocationContext.session().id());
} else {
log.trace(
"traceCallLlm: InvocationContext session or session ID is null, cannot set"
+ " gcp.vertex.agent.session_id");
+ " adk.session_id");
}

try {
span.setAttribute(
"gcp.vertex.agent.llm_request",
JsonBaseModel.getMapper().writeValueAsString(buildLlmRequestForTrace(llmRequest)));
span.setAttribute("gcp.vertex.agent.llm_response", llmResponse.toJson());
} catch (JsonProcessingException e) {
log.warn("traceCallLlm: Failed to serialize LlmRequest or LlmResponse to JSON", e);
if (CAPTURE_MESSAGE_CONTENT_IN_SPANS) {
try {
span.setAttribute(
"adk.llm_request",
JsonBaseModel.getMapper().writeValueAsString(buildLlmRequestForTrace(llmRequest)));
span.setAttribute("adk.llm_response", llmResponse.toJson());
} catch (JsonProcessingException e) {
log.warn("traceCallLlm: Failed to serialize LlmRequest or LlmResponse to JSON", e);
}
} else {
span.setAttribute("adk.llm_request", "{}");
span.setAttribute("adk.llm_response", "{}");
}
}

Expand All @@ -196,13 +205,13 @@ public static void traceSendData(
return;
}

span.setAttribute("gcp.vertex.agent.invocation_id", invocationContext.invocationId());
span.setAttribute("adk.invocation_id", invocationContext.invocationId());
if (eventId != null && !eventId.isEmpty()) {
span.setAttribute("gcp.vertex.agent.event_id", eventId);
span.setAttribute("adk.event_id", eventId);
}

if (invocationContext.session() != null && invocationContext.session().id() != null) {
span.setAttribute("gcp.vertex.agent.session_id", invocationContext.session().id());
span.setAttribute("adk.session_id", invocationContext.session().id());
}

try {
Expand All @@ -216,7 +225,7 @@ public static void traceSendData(
}
}
}
span.setAttribute("gcp.vertex.agent.data", JsonBaseModel.toJsonString(dataList));
span.setAttribute("adk.data", JsonBaseModel.toJsonString(dataList));
} catch (IllegalStateException e) {
log.warn("traceSendData: Failed to serialize data to JSON", e);
}
Expand Down
Loading