Skip to content

.NET: Fix Activity.Current lost after streaming + tool calls (#4074) - #4109

Closed
alliscode wants to merge 2 commits into
microsoft:mainfrom
alliscode:rescue/issue-4074-clean
Closed

.NET: Fix Activity.Current lost after streaming + tool calls (#4074)#4109
alliscode wants to merge 2 commits into
microsoft:mainfrom
alliscode:rescue/issue-4074-clean

Conversation

@alliscode

Copy link
Copy Markdown
Member

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.Current is 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:

  • Updated OpenTelemetryAgent.GetStreamingResponseAsync to capture and restore Activity.Current after each yield, ensuring the correct activity context is maintained throughout streaming and after completion. The call to UpdateCurrentActivity is now deferred until streaming completes to avoid interfering with other clients' activity management.

Testing and validation:

  • Added two comprehensive unit tests in OpenTelemetryAgentTests.cs:
  • Introduced a callback-based CallbackChatClient test double to facilitate flexible streaming response simulation in tests.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

alliscode 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.
Copilot AI review requested due to automatic review settings February 19, 2026 23:07
@markwallace-microsoft markwallace-microsoft added the .NET Usage: [Issues, PRs], Target: .Net label Feb 19, 2026
@github-actions github-actions Bot changed the title Fix Activity.Current lost after streaming + tool calls (#4074) .NET: Fix Activity.Current lost after streaming + tool calls (#4074) Feb 19, 2026
@alliscode
alliscode requested review from stephentoub and removed request for Copilot February 19, 2026 23:08
@stephentoub

Copy link
Copy Markdown
Member

This shouldn't be necessary once dotnet/extensions#7321 merges. Is this blocking for AF?

@alliscode

Copy link
Copy Markdown
Member Author

This shouldn't be necessary once dotnet/extensions#7321 merges. Is this blocking for AF?

Not blocking. I'll keep an eye on that PR. Thanks.

@alliscode alliscode closed this Feb 20, 2026
@filipw

filipw commented Mar 9, 2026

Copy link
Copy Markdown

AF OTEL is currently broken, does this not meet some definition of "blocking"?
It would be great to get M.E.AI patch out there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants