@@ -164,7 +164,10 @@ protected Flowable<Event> postprocess(
164164 * callbacks. Callbacks should not rely on its ID if they create their own separate events.
165165 */
166166 private Flowable <LlmResponse > callLlm (
167- InvocationContext context , LlmRequest llmRequest , Event eventForCallbackUsage ) {
167+ Context spanContext ,
168+ InvocationContext context ,
169+ LlmRequest llmRequest ,
170+ Event eventForCallbackUsage ) {
168171 LlmAgent agent = (LlmAgent ) context .agent ();
169172
170173 LlmRequest .Builder llmRequestBuilder = llmRequest .toBuilder ();
@@ -200,7 +203,7 @@ private Flowable<LlmResponse> callLlm(
200203 span .setStatus (StatusCode .ERROR , error .getMessage ());
201204 span .recordException (error );
202205 })
203- .compose (Tracing .trace ("call_llm" ))
206+ .compose (Tracing .< LlmResponse > trace ("call_llm" ). setParent ( spanContext ))
204207 .concatMap (
205208 llmResp ->
206209 handleAfterModelCallback (context , llmResp , eventForCallbackUsage )
@@ -319,7 +322,7 @@ private Single<LlmResponse> handleAfterModelCallback(
319322 * @throws LlmCallsLimitExceededException if the agent exceeds allowed LLM invocations.
320323 * @throws IllegalStateException if a transfer agent is specified but not found.
321324 */
322- private Flowable <Event > runOneStep (InvocationContext context ) {
325+ private Flowable <Event > runOneStep (Context spanContext , InvocationContext context ) {
323326 AtomicReference <LlmRequest > llmRequestRef = new AtomicReference <>(LlmRequest .builder ().build ());
324327
325328 return Flowable .defer (
@@ -351,7 +354,11 @@ private Flowable<Event> runOneStep(InvocationContext context) {
351354 .build ();
352355 mutableEventTemplate .setTimestamp (0L );
353356
354- return callLlm (context , llmRequestAfterPreprocess , mutableEventTemplate )
357+ return callLlm (
358+ spanContext ,
359+ context ,
360+ llmRequestAfterPreprocess ,
361+ mutableEventTemplate )
355362 .concatMap (
356363 llmResponse -> {
357364 try (Scope postScope = currentContext .makeCurrent ()) {
@@ -403,11 +410,12 @@ private Flowable<Event> runOneStep(InvocationContext context) {
403410 */
404411 @ Override
405412 public Flowable <Event > run (InvocationContext invocationContext ) {
406- return run (invocationContext , 0 );
413+ return run (Context . current (), invocationContext , 0 );
407414 }
408415
409- private Flowable <Event > run (InvocationContext invocationContext , int stepsCompleted ) {
410- Flowable <Event > currentStepEvents = runOneStep (invocationContext ).cache ();
416+ private Flowable <Event > run (
417+ Context spanContext , InvocationContext invocationContext , int stepsCompleted ) {
418+ Flowable <Event > currentStepEvents = runOneStep (spanContext , invocationContext ).cache ();
411419 if (stepsCompleted + 1 >= maxSteps ) {
412420 logger .debug ("Ending flow execution because max steps reached." );
413421 return currentStepEvents ;
@@ -427,7 +435,7 @@ private Flowable<Event> run(InvocationContext invocationContext, int stepsComple
427435 return Flowable .empty ();
428436 } else {
429437 logger .debug ("Continuing to next step of the flow." );
430- return run (invocationContext , stepsCompleted + 1 );
438+ return run (spanContext , invocationContext , stepsCompleted + 1 );
431439 }
432440 }));
433441 }
@@ -444,6 +452,7 @@ private Flowable<Event> run(InvocationContext invocationContext, int stepsComple
444452 public Flowable <Event > runLive (InvocationContext invocationContext ) {
445453 AtomicReference <LlmRequest > llmRequestRef = new AtomicReference <>(LlmRequest .builder ().build ());
446454 Flowable <Event > preprocessEvents = preprocess (invocationContext , llmRequestRef );
455+ Context spanContext = Context .current ();
447456
448457 return preprocessEvents .concatWith (
449458 Flowable .defer (
@@ -481,7 +490,7 @@ public Flowable<Event> runLive(InvocationContext invocationContext) {
481490 eventIdForSendData ,
482491 llmRequestAfterPreprocess .contents ());
483492 })
484- .compose (Tracing .trace ("send_data" ));
493+ .compose (Tracing .< Event > trace ("send_data" ). setParent ( spanContext ));
485494
486495 Flowable <LiveRequest > liveRequests =
487496 invocationContext
0 commit comments