|
62 | 62 | import java.util.List; |
63 | 63 | import java.util.Map; |
64 | 64 | import java.util.Optional; |
| 65 | +import java.util.concurrent.atomic.AtomicReference; |
65 | 66 | import org.junit.After; |
66 | 67 | import org.junit.Before; |
67 | 68 | import org.junit.Rule; |
@@ -215,6 +216,70 @@ public void testTraceTransformer() throws InterruptedException { |
215 | 216 | assertTrue(transformerSpanData.hasEnded()); |
216 | 217 | } |
217 | 218 |
|
| 219 | + @Test |
| 220 | + public void testTraceTransformerStartsSpanBeforeSubscribingToDeferredUpstream() |
| 221 | + throws InterruptedException { |
| 222 | + Span parentSpan = tracer.spanBuilder("parent").startSpan(); |
| 223 | + AtomicReference<String> flowableSpanId = new AtomicReference<>(); |
| 224 | + AtomicReference<String> singleSpanId = new AtomicReference<>(); |
| 225 | + AtomicReference<String> maybeSpanId = new AtomicReference<>(); |
| 226 | + AtomicReference<String> completableSpanId = new AtomicReference<>(); |
| 227 | + |
| 228 | + try (Scope s = parentSpan.makeCurrent()) { |
| 229 | + Flowable.defer( |
| 230 | + () -> { |
| 231 | + flowableSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 232 | + return Flowable.just(1); |
| 233 | + }) |
| 234 | + .compose(Tracing.trace("flowable-transformer")) |
| 235 | + .test() |
| 236 | + .await() |
| 237 | + .assertComplete(); |
| 238 | + |
| 239 | + Single.defer( |
| 240 | + () -> { |
| 241 | + singleSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 242 | + return Single.just(1); |
| 243 | + }) |
| 244 | + .compose(Tracing.trace("single-transformer")) |
| 245 | + .test() |
| 246 | + .await() |
| 247 | + .assertComplete(); |
| 248 | + |
| 249 | + Maybe.defer( |
| 250 | + () -> { |
| 251 | + maybeSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 252 | + return Maybe.just(1); |
| 253 | + }) |
| 254 | + .compose(Tracing.trace("maybe-transformer")) |
| 255 | + .test() |
| 256 | + .await() |
| 257 | + .assertComplete(); |
| 258 | + |
| 259 | + Completable.defer( |
| 260 | + () -> { |
| 261 | + completableSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 262 | + return Completable.complete(); |
| 263 | + }) |
| 264 | + .compose(Tracing.trace("completable-transformer")) |
| 265 | + .test() |
| 266 | + .await() |
| 267 | + .assertComplete(); |
| 268 | + } finally { |
| 269 | + parentSpan.end(); |
| 270 | + } |
| 271 | + |
| 272 | + SpanData parentSpanData = findSpanByName("parent"); |
| 273 | + assertDeferredUpstreamSawTransformerSpan( |
| 274 | + parentSpanData, findSpanByName("flowable-transformer"), flowableSpanId); |
| 275 | + assertDeferredUpstreamSawTransformerSpan( |
| 276 | + parentSpanData, findSpanByName("single-transformer"), singleSpanId); |
| 277 | + assertDeferredUpstreamSawTransformerSpan( |
| 278 | + parentSpanData, findSpanByName("maybe-transformer"), maybeSpanId); |
| 279 | + assertDeferredUpstreamSawTransformerSpan( |
| 280 | + parentSpanData, findSpanByName("completable-transformer"), completableSpanId); |
| 281 | + } |
| 282 | + |
218 | 283 | @Test |
219 | 284 | public void testTraceAgentInvocation() { |
220 | 285 | Span span = tracer.spanBuilder("test").startSpan(); |
@@ -504,6 +569,38 @@ public void runnerRunLive_propagatesContext() throws InterruptedException { |
504 | 569 | assertParent(invocation, agentSpan); |
505 | 570 | } |
506 | 571 |
|
| 572 | + @Test |
| 573 | + public void testModelCallbacksObserveCallLlmSpan() throws InterruptedException { |
| 574 | + TestLlm testLlm = |
| 575 | + TestUtils.createTestLlm( |
| 576 | + TestUtils.createLlmResponse(Content.fromParts(Part.fromText("response")))); |
| 577 | + AtomicReference<String> beforeModelSpanId = new AtomicReference<>(); |
| 578 | + AtomicReference<String> afterModelSpanId = new AtomicReference<>(); |
| 579 | + |
| 580 | + LlmAgent agentWithCallbacks = |
| 581 | + LlmAgent.builder() |
| 582 | + .name("test_agent") |
| 583 | + .description("description") |
| 584 | + .model(testLlm) |
| 585 | + .beforeModelCallback( |
| 586 | + (callbackContext, llmRequest) -> { |
| 587 | + beforeModelSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 588 | + return Maybe.empty(); |
| 589 | + }) |
| 590 | + .afterModelCallback( |
| 591 | + (callbackContext, llmResponse) -> { |
| 592 | + afterModelSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 593 | + return Maybe.empty(); |
| 594 | + }) |
| 595 | + .build(); |
| 596 | + |
| 597 | + runAgent(agentWithCallbacks); |
| 598 | + |
| 599 | + SpanData callLlm = findSpanByName("call_llm"); |
| 600 | + assertEquals(callLlm.getSpanContext().getSpanId(), beforeModelSpanId.get()); |
| 601 | + assertEquals(callLlm.getSpanContext().getSpanId(), afterModelSpanId.get()); |
| 602 | + } |
| 603 | + |
507 | 604 | @Test |
508 | 605 | public void testAgentWithToolCallTraceHierarchy() throws InterruptedException { |
509 | 606 | // This test verifies the trace hierarchy created when an agent calls an LLM, |
@@ -634,9 +731,9 @@ public void testNestedAgentTraceHierarchy() throws InterruptedException { |
634 | 731 | assertParent(agentASpan, agentACallLlm1); |
635 | 732 | // ├── execute_tool transfer_to_agent |
636 | 733 | assertParent(agentACallLlm1, executeTool); |
637 | | - // └── invoke_agent AgentB |
638 | | - assertParent(agentACallLlm1, agentBSpan); |
639 | | - // └── call_llm 2 |
| 734 | + // └── invoke_agent AgentB |
| 735 | + assertParent(agentASpan, agentBSpan); |
| 736 | + // └── call_llm 2 |
640 | 737 | assertParent(agentBSpan, agentBCallLlm); |
641 | 738 | } |
642 | 739 |
|
@@ -685,6 +782,13 @@ private void assertParent(SpanData parent, SpanData child) { |
685 | 782 | assertEquals(parent.getSpanContext().getSpanId(), child.getParentSpanContext().getSpanId()); |
686 | 783 | } |
687 | 784 |
|
| 785 | + private void assertDeferredUpstreamSawTransformerSpan( |
| 786 | + SpanData parent, SpanData transformer, AtomicReference<String> observedSpanId) { |
| 787 | + assertParent(parent, transformer); |
| 788 | + assertTrue(transformer.hasEnded()); |
| 789 | + assertEquals(transformer.getSpanContext().getSpanId(), observedSpanId.get()); |
| 790 | + } |
| 791 | + |
688 | 792 | /** |
689 | 793 | * Finds a span by name, polling multiple times. |
690 | 794 | * |
|
0 commit comments