Skip to content

Commit 1ecab47

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Updating the Tracing implementation and updating BaseAgent.runLive
This update should make the java tracing consistent with Python ADK: 1. **`BaseAgent.java`**: * The `runAsync` and `runLive` methods have been modified to create the `InvocationContext` before starting the tracing span. * The span name for agent invocations has been changed from `"agent_run [<agent name>]"` to `"invoke_agent <agent name>"`. * A new `Tracing.traceAgentInvocation` method is now called to add initial tracing attributes for the agent invocation. * In `runLive`, the `runLiveImpl` execution is now wrapped with calls to `beforeAgentCallback` and `afterAgentCallback` to ensure proper tracing of these lifecycle events. 2. **`Tracing.java`**: * The OpenTelemetry tracer name has been updated from `"com.google.adk"` to `"gcp.vertex.agent"`. * A new `traceAgentInvocation` method has been added to set standard attributes for agent invocation spans, including `gen_ai.operation.name`, `gen_ai.agent.description`, `gen_ai.agent.name`, and `gen_ai.conversation.id`. * Attribute keys used in `traceToolCall`, `traceToolResponse`, `traceCallLlm`, and `traceSendData` have been updated to use the `"gcp.vertex.agent."` prefix instead of `"adk."` or `"com.google.adk"`. * The serialization of message content (like tool call arguments, tool responses, and data) is now guarded by the `CAPTURE_MESSAGE_CONTENT_IN_SPANS` flag. When disabled, empty JSON objects are recorded instead. * `traceToolResponse` now includes logic to extract and trace the `tool_call.id` and the tool response content from `FunctionResponse` objects. * `traceCallLlm` now captures additional LLM request and response details, such as `gen_ai.request.top_p`, `gen_ai.request.max_tokens`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and `gen_ai.response.finish_reasons`. PiperOrigin-RevId: 867888690
1 parent ee459b3 commit 1ecab47

File tree

6 files changed

+744
-154
lines changed

6 files changed

+744
-154
lines changed

core/src/main/java/com/google/adk/agents/BaseAgent.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,12 @@ public Flowable<Event> runAsync(InvocationContext parentContext) {
304304
Tracer tracer = Tracing.getTracer();
305305
return Flowable.defer(
306306
() -> {
307+
InvocationContext invocationContext = createInvocationContext(parentContext);
307308
Span span =
308-
tracer
309-
.spanBuilder("agent_run [" + name() + "]")
310-
.setParent(Context.current())
311-
.startSpan();
309+
tracer.spanBuilder("invoke_agent " + name()).setParent(Context.current()).startSpan();
310+
Tracing.traceAgentInvocation(span, name(), description(), invocationContext);
312311
Context spanContext = Context.current().with(span);
313312

314-
InvocationContext invocationContext = createInvocationContext(parentContext);
315-
316313
return Tracing.traceFlowable(
317314
spanContext,
318315
span,
@@ -443,16 +440,41 @@ public Flowable<Event> runLive(InvocationContext parentContext) {
443440
Tracer tracer = Tracing.getTracer();
444441
return Flowable.defer(
445442
() -> {
443+
InvocationContext invocationContext = createInvocationContext(parentContext);
446444
Span span =
447-
tracer
448-
.spanBuilder("agent_run [" + name() + "]")
449-
.setParent(Context.current())
450-
.startSpan();
445+
tracer.spanBuilder("invoke_agent " + name()).setParent(Context.current()).startSpan();
446+
Tracing.traceAgentInvocation(span, name(), description(), invocationContext);
451447
Context spanContext = Context.current().with(span);
452448

453-
InvocationContext invocationContext = createInvocationContext(parentContext);
449+
return Tracing.traceFlowable(
450+
spanContext,
451+
span,
452+
() ->
453+
callCallback(
454+
beforeCallbacksToFunctions(
455+
invocationContext.pluginManager(), beforeAgentCallback),
456+
invocationContext)
457+
.flatMapPublisher(
458+
beforeEventOpt -> {
459+
if (invocationContext.endInvocation()) {
460+
return Flowable.fromOptional(beforeEventOpt);
461+
}
462+
463+
Flowable<Event> beforeEvents = Flowable.fromOptional(beforeEventOpt);
464+
Flowable<Event> mainEvents =
465+
Flowable.defer(() -> runLiveImpl(invocationContext));
466+
Flowable<Event> afterEvents =
467+
Flowable.defer(
468+
() ->
469+
callCallback(
470+
afterCallbacksToFunctions(
471+
invocationContext.pluginManager(),
472+
afterAgentCallback),
473+
invocationContext)
474+
.flatMapPublisher(Flowable::fromOptional));
454475

455-
return Tracing.traceFlowable(spanContext, span, () -> runLiveImpl(invocationContext));
476+
return Flowable.concat(beforeEvents, mainEvents, afterEvents);
477+
}));
456478
});
457479
}
458480

core/src/main/java/com/google/adk/flows/llmflows/Functions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static Maybe<Event> handleFunctionCalls(
181181
Span mergedSpan =
182182
tracer.spanBuilder("tool_response").setParent(Context.current()).startSpan();
183183
try (Scope scope = mergedSpan.makeCurrent()) {
184-
Tracing.traceToolResponse(invocationContext, mergedEvent.id(), mergedEvent);
184+
Tracing.traceToolResponse(mergedEvent.id(), mergedEvent);
185185
} finally {
186186
mergedSpan.end();
187187
}
@@ -571,7 +571,8 @@ private static Maybe<Map<String, Object>> callTool(
571571
.setParent(parentContext)
572572
.startSpan();
573573
try (Scope scope = span.makeCurrent()) {
574-
Tracing.traceToolCall(args);
574+
Tracing.traceToolCall(
575+
tool.name(), tool.description(), tool.getClass().getSimpleName(), args);
575576
return tool.runAsync(args, toolContext)
576577
.toMaybe()
577578
.doOnError(span::recordException)
@@ -620,7 +621,7 @@ private static Event buildResponseEvent(
620621
.content(Content.builder().role("user").parts(partFunctionResponse).build())
621622
.actions(toolContext.eventActions())
622623
.build();
623-
Tracing.traceToolResponse(invocationContext, event.id(), event);
624+
Tracing.traceToolResponse(event.id(), event);
624625
return event;
625626
} finally {
626627
span.end();

0 commit comments

Comments
 (0)