|
22 | 22 | import static com.google.adk.testing.TestUtils.createTestAgent; |
23 | 23 | import static com.google.adk.testing.TestUtils.createTestAgentBuilder; |
24 | 24 | import static com.google.adk.testing.TestUtils.createTestLlm; |
| 25 | +import static com.google.adk.testing.TestUtils.createTextLlmResponse; |
25 | 26 | import static com.google.common.collect.Iterables.getOnlyElement; |
26 | 27 | import static com.google.common.truth.Truth.assertThat; |
| 28 | +import static org.junit.Assert.assertEquals; |
27 | 29 | import static org.junit.Assert.assertThrows; |
28 | 30 |
|
29 | 31 | import com.google.adk.agents.Callbacks.AfterModelCallback; |
|
39 | 41 | import com.google.adk.models.Model; |
40 | 42 | import com.google.adk.sessions.InMemorySessionService; |
41 | 43 | import com.google.adk.sessions.Session; |
| 44 | +import com.google.adk.telemetry.Tracing; |
42 | 45 | import com.google.adk.testing.TestLlm; |
43 | 46 | import com.google.adk.testing.TestUtils.EchoTool; |
44 | 47 | import com.google.adk.tools.BaseTool; |
45 | 48 | import com.google.adk.tools.BaseToolset; |
46 | 49 | import com.google.common.collect.ImmutableList; |
47 | 50 | import com.google.common.collect.ImmutableMap; |
| 51 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
48 | 52 | import com.google.genai.types.Content; |
49 | 53 | import com.google.genai.types.FunctionDeclaration; |
50 | 54 | import com.google.genai.types.Part; |
51 | 55 | import com.google.genai.types.Schema; |
| 56 | +import io.opentelemetry.api.trace.Span; |
| 57 | +import io.opentelemetry.api.trace.Tracer; |
| 58 | +import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; |
| 59 | +import io.opentelemetry.sdk.trace.data.SpanData; |
52 | 60 | import io.reactivex.rxjava3.core.Flowable; |
53 | 61 | import io.reactivex.rxjava3.core.Maybe; |
54 | 62 | import io.reactivex.rxjava3.core.Single; |
55 | 63 | import java.util.List; |
56 | 64 | import java.util.Optional; |
57 | 65 | import java.util.concurrent.ConcurrentHashMap; |
58 | 66 | import java.util.concurrent.atomic.AtomicBoolean; |
| 67 | +import org.junit.After; |
| 68 | +import org.junit.Before; |
| 69 | +import org.junit.Rule; |
59 | 70 | import org.junit.Test; |
60 | 71 | import org.junit.runner.RunWith; |
61 | 72 | import org.junit.runners.JUnit4; |
62 | 73 |
|
63 | 74 | /** Unit tests for {@link LlmAgent}. */ |
64 | 75 | @RunWith(JUnit4.class) |
65 | 76 | public final class LlmAgentTest { |
| 77 | + @Rule public final OpenTelemetryRule openTelemetryRule = OpenTelemetryRule.create(); |
| 78 | + |
| 79 | + private Tracer originalTracer; |
| 80 | + |
| 81 | + @Before |
| 82 | + public void setup() { |
| 83 | + this.originalTracer = Tracing.getTracer(); |
| 84 | + Tracing.setTracerForTesting(openTelemetryRule.getOpenTelemetry().getTracer("gcp.vertex.agent")); |
| 85 | + } |
| 86 | + |
| 87 | + @After |
| 88 | + public void tearDown() { |
| 89 | + Tracing.setTracerForTesting(originalTracer); |
| 90 | + } |
66 | 91 |
|
67 | 92 | private static class ClosableToolset implements BaseToolset { |
68 | 93 | final AtomicBoolean closed = new AtomicBoolean(false); |
@@ -496,4 +521,121 @@ public void close() { |
496 | 521 | assertThat(toolset1.closed.get()).isTrue(); |
497 | 522 | assertThat(toolset2.closed.get()).isTrue(); |
498 | 523 | } |
| 524 | + |
| 525 | + @Test |
| 526 | + public void runAsync_createsInvokeAgentSpan() throws InterruptedException { |
| 527 | + Content modelContent = Content.fromParts(Part.fromText("response")); |
| 528 | + TestLlm testLlm = createTestLlm(createLlmResponse(modelContent)); |
| 529 | + LlmAgent agent = createTestAgent(testLlm); |
| 530 | + InvocationContext invocationContext = createInvocationContext(agent); |
| 531 | + |
| 532 | + agent.runAsync(invocationContext).test().await().assertComplete(); |
| 533 | + |
| 534 | + List<SpanData> spans = openTelemetryRule.getSpans(); |
| 535 | + assertThat(spans.stream().anyMatch(s -> s.getName().equals("invoke_agent test agent"))) |
| 536 | + .isTrue(); |
| 537 | + } |
| 538 | + |
| 539 | + @Test |
| 540 | + public void runAsync_withTools_createsToolSpans() throws InterruptedException { |
| 541 | + ImmutableMap<String, Object> echoArgs = ImmutableMap.of("arg", "value"); |
| 542 | + Content contentWithFunctionCall = |
| 543 | + Content.fromParts(Part.fromText("text"), Part.fromFunctionCall("echo_tool", echoArgs)); |
| 544 | + Content finalResponse = Content.fromParts(Part.fromText("finished")); |
| 545 | + TestLlm testLlm = |
| 546 | + createTestLlm(createLlmResponse(contentWithFunctionCall), createLlmResponse(finalResponse)); |
| 547 | + LlmAgent agent = createTestAgentBuilder(testLlm).tools(new EchoTool()).build(); |
| 548 | + InvocationContext invocationContext = createInvocationContext(agent); |
| 549 | + |
| 550 | + agent.runAsync(invocationContext).test().await().assertComplete(); |
| 551 | + |
| 552 | + List<SpanData> spans = openTelemetryRule.getSpans(); |
| 553 | + SpanData agentSpan = findSpanByName(spans, "invoke_agent test agent"); |
| 554 | + List<SpanData> llmSpans = findSpansByName(spans, "call_llm"); |
| 555 | + List<SpanData> toolCallSpans = findSpansByName(spans, "tool_call [echo_tool]"); |
| 556 | + List<SpanData> toolResponseSpans = findSpansByName(spans, "tool_response [echo_tool]"); |
| 557 | + |
| 558 | + assertThat(llmSpans).hasSize(2); |
| 559 | + assertThat(toolCallSpans).hasSize(1); |
| 560 | + assertThat(toolResponseSpans).hasSize(1); |
| 561 | + |
| 562 | + String agentSpanId = agentSpan.getSpanContext().getSpanId(); |
| 563 | + llmSpans.forEach(s -> assertEquals(agentSpanId, s.getParentSpanContext().getSpanId())); |
| 564 | + toolCallSpans.forEach(s -> assertEquals(agentSpanId, s.getParentSpanContext().getSpanId())); |
| 565 | + toolResponseSpans.forEach(s -> assertEquals(agentSpanId, s.getParentSpanContext().getSpanId())); |
| 566 | + } |
| 567 | + |
| 568 | + @Test |
| 569 | + public void runAsync_afterToolCallback_propagatesContext() throws InterruptedException { |
| 570 | + ImmutableMap<String, Object> echoArgs = ImmutableMap.of("arg", "value"); |
| 571 | + Content contentWithFunctionCall = |
| 572 | + Content.fromParts(Part.fromText("text"), Part.fromFunctionCall("echo_tool", echoArgs)); |
| 573 | + Content finalResponse = Content.fromParts(Part.fromText("finished")); |
| 574 | + TestLlm testLlm = |
| 575 | + createTestLlm(createLlmResponse(contentWithFunctionCall), createLlmResponse(finalResponse)); |
| 576 | + |
| 577 | + AfterToolCallback afterToolCallback = |
| 578 | + (invCtx, tool, input, toolCtx, response) -> { |
| 579 | + // Verify that the OpenTelemetry context is correctly propagated to the callback. |
| 580 | + assertThat(Span.current().getSpanContext().isValid()).isTrue(); |
| 581 | + return Maybe.empty(); |
| 582 | + }; |
| 583 | + |
| 584 | + LlmAgent agent = |
| 585 | + createTestAgentBuilder(testLlm) |
| 586 | + .tools(new EchoTool()) |
| 587 | + .afterToolCallback(ImmutableList.of(afterToolCallback)) |
| 588 | + .build(); |
| 589 | + InvocationContext invocationContext = createInvocationContext(agent); |
| 590 | + |
| 591 | + agent.runAsync(invocationContext).test().await().assertComplete(); |
| 592 | + |
| 593 | + List<SpanData> spans = openTelemetryRule.getSpans(); |
| 594 | + findSpanByName(spans, "invoke_agent test agent"); |
| 595 | + } |
| 596 | + |
| 597 | + @Test |
| 598 | + public void runAsync_withSubAgents_createsSpans() throws InterruptedException { |
| 599 | + LlmAgent subAgent = |
| 600 | + createTestAgentBuilder(createTestLlm(createTextLlmResponse("sub response"))) |
| 601 | + .name("sub-agent") |
| 602 | + .build(); |
| 603 | + |
| 604 | + // Force a transfer to sub-agent using a callback |
| 605 | + AfterModelCallback transferCallback = |
| 606 | + (ctx, response) -> { |
| 607 | + ctx.eventActions().setTransferToAgent(subAgent.name()); |
| 608 | + return Maybe.empty(); |
| 609 | + }; |
| 610 | + |
| 611 | + TestLlm testLlm = createTestLlm(createTextLlmResponse("initial")); |
| 612 | + LlmAgent agent = |
| 613 | + createTestAgentBuilder(testLlm) |
| 614 | + .subAgents(subAgent) |
| 615 | + .afterModelCallback(ImmutableList.of(transferCallback)) |
| 616 | + .build(); |
| 617 | + InvocationContext invocationContext = createInvocationContext(agent); |
| 618 | + |
| 619 | + agent.runAsync(invocationContext).test().await().assertComplete(); |
| 620 | + |
| 621 | + List<SpanData> spans = openTelemetryRule.getSpans(); |
| 622 | + assertThat(spans.stream().anyMatch(s -> s.getName().equals("invoke_agent test agent"))) |
| 623 | + .isTrue(); |
| 624 | + assertThat(spans.stream().anyMatch(s -> s.getName().equals("invoke_agent sub-agent"))).isTrue(); |
| 625 | + |
| 626 | + List<SpanData> llmSpans = findSpansByName(spans, "call_llm"); |
| 627 | + assertThat(llmSpans).hasSize(2); // One for main agent, one for sub agent |
| 628 | + } |
| 629 | + |
| 630 | + private List<SpanData> findSpansByName(List<SpanData> spans, String name) { |
| 631 | + return spans.stream().filter(s -> s.getName().equals(name)).toList(); |
| 632 | + } |
| 633 | + |
| 634 | + @CanIgnoreReturnValue |
| 635 | + private SpanData findSpanByName(List<SpanData> spans, String name) { |
| 636 | + return spans.stream() |
| 637 | + .filter(s -> s.getName().equals(name)) |
| 638 | + .findFirst() |
| 639 | + .orElseThrow(() -> new AssertionError("Span not found: " + name)); |
| 640 | + } |
499 | 641 | } |
0 commit comments