|
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(); |
@@ -464,6 +529,38 @@ public void runnerRunLive_propagatesContext() throws InterruptedException { |
464 | 529 | assertParent(invocation, agentSpan); |
465 | 530 | } |
466 | 531 |
|
| 532 | + @Test |
| 533 | + public void testModelCallbacksObserveCallLlmSpan() throws InterruptedException { |
| 534 | + TestLlm testLlm = |
| 535 | + TestUtils.createTestLlm( |
| 536 | + TestUtils.createLlmResponse(Content.fromParts(Part.fromText("response")))); |
| 537 | + AtomicReference<String> beforeModelSpanId = new AtomicReference<>(); |
| 538 | + AtomicReference<String> afterModelSpanId = new AtomicReference<>(); |
| 539 | + |
| 540 | + LlmAgent agentWithCallbacks = |
| 541 | + LlmAgent.builder() |
| 542 | + .name("test_agent") |
| 543 | + .description("description") |
| 544 | + .model(testLlm) |
| 545 | + .beforeModelCallback( |
| 546 | + (callbackContext, llmRequest) -> { |
| 547 | + beforeModelSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 548 | + return Maybe.empty(); |
| 549 | + }) |
| 550 | + .afterModelCallback( |
| 551 | + (callbackContext, llmResponse) -> { |
| 552 | + afterModelSpanId.set(Span.current().getSpanContext().getSpanId()); |
| 553 | + return Maybe.empty(); |
| 554 | + }) |
| 555 | + .build(); |
| 556 | + |
| 557 | + runAgent(agentWithCallbacks); |
| 558 | + |
| 559 | + SpanData callLlm = findSpanByName("call_llm"); |
| 560 | + assertEquals(callLlm.getSpanContext().getSpanId(), beforeModelSpanId.get()); |
| 561 | + assertEquals(callLlm.getSpanContext().getSpanId(), afterModelSpanId.get()); |
| 562 | + } |
| 563 | + |
467 | 564 | @Test |
468 | 565 | public void testAgentWithToolCallTraceHierarchy() throws InterruptedException { |
469 | 566 | // This test verifies the trace hierarchy created when an agent calls an LLM, |
@@ -594,9 +691,9 @@ public void testNestedAgentTraceHierarchy() throws InterruptedException { |
594 | 691 | assertParent(agentASpan, agentACallLlm1); |
595 | 692 | // ├── execute_tool transfer_to_agent |
596 | 693 | assertParent(agentACallLlm1, executeTool); |
597 | | - // └── invoke_agent AgentB |
598 | | - assertParent(agentACallLlm1, agentBSpan); |
599 | | - // └── call_llm 2 |
| 694 | + // └── invoke_agent AgentB |
| 695 | + assertParent(agentASpan, agentBSpan); |
| 696 | + // └── call_llm 2 |
600 | 697 | assertParent(agentBSpan, agentBCallLlm); |
601 | 698 | } |
602 | 699 |
|
@@ -645,6 +742,13 @@ private void assertParent(SpanData parent, SpanData child) { |
645 | 742 | assertEquals(parent.getSpanContext().getSpanId(), child.getParentSpanContext().getSpanId()); |
646 | 743 | } |
647 | 744 |
|
| 745 | + private void assertDeferredUpstreamSawTransformerSpan( |
| 746 | + SpanData parent, SpanData transformer, AtomicReference<String> observedSpanId) { |
| 747 | + assertParent(parent, transformer); |
| 748 | + assertTrue(transformer.hasEnded()); |
| 749 | + assertEquals(transformer.getSpanContext().getSpanId(), observedSpanId.get()); |
| 750 | + } |
| 751 | + |
648 | 752 | /** |
649 | 753 | * Finds a span by name, polling multiple times. |
650 | 754 | * |
|
0 commit comments