.NET: Fix Activity.Current lost after streaming + tool calls (#4074) - #4109
Closed
alliscode wants to merge 2 commits into
Closed
.NET: Fix Activity.Current lost after streaming + tool calls (#4074)#4109alliscode wants to merge 2 commits into
alliscode wants to merge 2 commits into
Conversation
added 2 commits
February 19, 2026 14:59
Defer UpdateCurrentActivity until after streaming completes and restore Activity.Current after each yield to work around dotnet/runtime#47802. This prevents FunctionInvokingChatClient from nullifying the trace context during multi-round streaming with tool calls.
alliscode
temporarily deployed
to
integration
February 19, 2026 23:07 — with
GitHub Actions
Inactive
alliscode
temporarily deployed
to
integration
February 19, 2026 23:07 — with
GitHub Actions
Inactive
Member
|
This shouldn't be necessary once dotnet/extensions#7321 merges. Is this blocking for AF? |
Member
Author
Not blocking. I'll keep an eye on that PR. Thanks. |
|
AF OTEL is currently broken, does this not meet some definition of "blocking"? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves the reliability of activity tracking and tracing in streaming chat agent responses, especially when tool calls are involved. The main change is a fix to how
Activity.Currentis preserved and restored during streaming, ensuring all spans remain within the same trace. Additionally, new unit tests are added to verify this behavior at both the agent and chat client pipeline levels.Activity tracking and context preservation:
OpenTelemetryAgent.GetStreamingResponseAsyncto capture and restoreActivity.Currentafter each yield, ensuring the correct activity context is maintained throughout streaming and after completion. The call toUpdateCurrentActivityis now deferred until streaming completes to avoid interfering with other clients' activity management.Testing and validation:
OpenTelemetryAgentTests.cs:StreamingWithToolCalls_PreservesActivityCurrentAsync: Verifies thatActivity.Currentis preserved during streaming responses with tool calls and that all spans remain within the same trace at the agent layer.StreamingWithToolCalls_ChatClientPipeline_PreservesActivityCurrentAsync: EnsuresActivity.Currentpreservation at the chat client pipeline level (includingFunctionInvokingChatClientandOpenTelemetryChatClient), independent of the agent.CallbackChatClienttest double to facilitate flexible streaming response simulation in tests.Contribution Checklist