-
Notifications
You must be signed in to change notification settings - Fork 359
fix: Fix RxJava tracing context propagation #1253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BaseLlmFlow#callLlm — Create the call_llm span before beforeModelCallback, so beforeModelCallback, model execution, afterModelCallback, and model error callbacks share the same call_llm span. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,74 +218,87 @@ private Flowable<Event> callLlm( | |
| Event eventForCallbackUsage) { | ||
| LlmRequest.Builder llmRequestBuilder = llmRequest.toBuilder(); | ||
|
|
||
| return handleBeforeModelCallback(context, llmRequestBuilder, eventForCallbackUsage) | ||
| .toFlowable() | ||
| .concatMap( | ||
| llmResp -> | ||
| postprocess( | ||
| context, | ||
| eventForCallbackUsage, | ||
| llmRequestBuilder.build(), | ||
| llmResp, | ||
| spanContext)) | ||
| .switchIfEmpty( | ||
| Flowable.defer( | ||
| () -> { | ||
| LlmAgent agent = (LlmAgent) context.agent(); | ||
| BaseLlm llm = | ||
| agent.resolvedModel().model().isPresent() | ||
| ? agent.resolvedModel().model().get() | ||
| : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); | ||
| LlmRequest finalLlmRequest = llmRequestBuilder.build(); | ||
|
|
||
| Span span = | ||
| Tracing.getTracer() | ||
| .spanBuilder("call_llm") | ||
| .setParent(spanContext) | ||
| .startSpan(); | ||
| Context callLlmContext = spanContext.with(span); | ||
|
|
||
| Flowable<Event> flowable = | ||
| llm.generateContent( | ||
| finalLlmRequest, | ||
| context.runConfig().streamingMode() == StreamingMode.SSE) | ||
| .onErrorResumeNext( | ||
| exception -> | ||
| handleOnModelErrorCallback( | ||
| context, | ||
| llmRequestBuilder, | ||
| eventForCallbackUsage, | ||
| exception) | ||
| .switchIfEmpty(Single.error(exception)) | ||
| .toFlowable()) | ||
| .doOnError( | ||
| error -> { | ||
| span.setStatus(StatusCode.ERROR, error.getMessage()); | ||
| span.recordException(error); | ||
| }) | ||
| return Flowable.defer( | ||
| () -> { | ||
| Span span = | ||
| Tracing.getTracer().spanBuilder("call_llm").setParent(spanContext).startSpan(); | ||
| Context callLlmContext = spanContext.with(span); | ||
|
|
||
| return Tracing.traceFlowable( | ||
| callLlmContext, | ||
| span, | ||
| () -> | ||
| handleBeforeModelCallback(context, llmRequestBuilder, eventForCallbackUsage) | ||
| .toFlowable() | ||
| .concatMap( | ||
| llmResp -> | ||
| handleAfterModelCallback(context, llmResp, eventForCallbackUsage) | ||
| .toFlowable()) | ||
| .flatMap( | ||
| llmResp -> | ||
| postprocess( | ||
| context, | ||
| eventForCallbackUsage, | ||
| finalLlmRequest, | ||
| llmRequestBuilder.build(), | ||
| llmResp, | ||
| callLlmContext) | ||
| .doOnSubscribe( | ||
| s -> | ||
| subscription -> | ||
| traceCallLlm( | ||
| span, | ||
| context, | ||
| eventForCallbackUsage.id(), | ||
| finalLlmRequest, | ||
| llmResp))); | ||
|
|
||
| return Tracing.traceFlowable(callLlmContext, span, () -> flowable); | ||
| })); | ||
| llmRequestBuilder.build(), | ||
| llmResp))) | ||
| .switchIfEmpty( | ||
| Flowable.defer( | ||
| () -> { | ||
| LlmAgent agent = (LlmAgent) context.agent(); | ||
| BaseLlm llm = | ||
| agent.resolvedModel().model().isPresent() | ||
| ? agent.resolvedModel().model().get() | ||
| : LlmRegistry.getLlm( | ||
| agent.resolvedModel().modelName().get()); | ||
| LlmRequest finalLlmRequest = llmRequestBuilder.build(); | ||
|
|
||
| return llm.generateContent( | ||
| finalLlmRequest, | ||
| context.runConfig().streamingMode() | ||
| == StreamingMode.SSE) | ||
| .onErrorResumeNext( | ||
| exception -> | ||
| handleOnModelErrorCallback( | ||
| context, | ||
| llmRequestBuilder, | ||
| eventForCallbackUsage, | ||
| exception) | ||
| .switchIfEmpty(Single.error(exception)) | ||
| .toFlowable()) | ||
| .doOnError( | ||
| error -> { | ||
| span.setStatus(StatusCode.ERROR, error.getMessage()); | ||
| span.recordException(error); | ||
| }) | ||
| .concatMap( | ||
| llmResp -> | ||
| handleAfterModelCallback( | ||
| context, llmResp, eventForCallbackUsage) | ||
| .toFlowable()) | ||
| .flatMap( | ||
| llmResp -> | ||
| postprocess( | ||
| context, | ||
| eventForCallbackUsage, | ||
| finalLlmRequest, | ||
| llmResp, | ||
| callLlmContext) | ||
| .doOnSubscribe( | ||
| subscription -> | ||
| traceCallLlm( | ||
| span, | ||
| context, | ||
| eventForCallbackUsage.id(), | ||
| finalLlmRequest, | ||
| llmResp))); | ||
| }))) | ||
| .compose(Tracing.withContext(spanContext)); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -667,10 +680,12 @@ public void onError(Throwable e) { | |
| "Agent not found: " + event.actions().transferToAgent().get()); | ||
| } | ||
| Flowable<Event> nextAgentEvents = | ||
| nextAgent | ||
| .get() | ||
| .runLive(invocationContext) | ||
| .compose(Tracing.withContext(spanContext)); | ||
| Flowable.defer( | ||
| () -> { | ||
| try (Scope scope = spanContext.makeCurrent()) { | ||
| return nextAgent.get().runLive(invocationContext); | ||
| } | ||
| }); | ||
| events = Flowable.concat(events, nextAgentEvents); | ||
| } | ||
| return events; | ||
|
|
@@ -693,11 +708,12 @@ public void onError(Throwable e) { | |
| }); | ||
|
|
||
| return Tracing.traceFlowable( | ||
| callLlmContext, | ||
| span, | ||
| () -> | ||
| receiveFlow.takeWhile( | ||
| event -> !event.actions().endInvocation().orElse(false))); | ||
| callLlmContext, | ||
| span, | ||
| () -> | ||
| receiveFlow.takeWhile( | ||
| event -> !event.actions().endInvocation().orElse(false))) | ||
| .compose(Tracing.withContext(spanContext)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BaseLlmFlow#callLlm — Re-scope emissions back to the parent context after the LLM segment, preventing follow-up tool/agent/LLM work from inheriting the previous call_llm context. |
||
| })); | ||
| } | ||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runner#runAsyncImpl — Capture Context.current() inside Flowable.defer(...) so the runner captures execution-time context, not stale assembly-time context. |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracing.TracerProvider — Start spans before subscribing to upstream streams, instead of in This ensures deferred RxJava sources see the tracing span as Span.current(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseAgent#run — Capture the parent context inside deferred execution so invoke_agent is correctly parented under the active invocation span.