Skip to content

Guard Activity.Current restore with null check in OpenTelemetry streaming clients#7443

Merged
tarekgh merged 2 commits intomainfrom
copilot/fix-opentelemetrychatclient-streaming
Mar 31, 2026
Merged

Guard Activity.Current restore with null check in OpenTelemetry streaming clients#7443
tarekgh merged 2 commits intomainfrom
copilot/fix-opentelemetrychatclient-streaming

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

OpenTelemetryChatClient.GetStreamingResponseAsync unconditionally sets Activity.Current = activity after each yield return as a workaround for dotnet/runtime#47802. When _activitySource.HasListeners() is false, activity is null, so this destroys Activity.Current for all downstream middleware on every streaming chunk. Same class of bug fixed for FunctionInvokingChatClient in #7321.

Changes

  • Guard the workaround with if (activity is not null) in all four affected streaming methods:
    • OpenTelemetryChatClient
    • OpenTelemetrySpeechToTextClient
    • OpenTelemetryTextToSpeechClient
    • OpenTelemetryHostedFileClient
  • Add test Streaming_NoListeners_PreservesActivityCurrent verifying Activity.Current survives streaming when no listener is attached
// Before
yield return update;
Activity.Current = activity;

// After
yield return update;
if (activity is not null)
{
    Activity.Current = activity;
}

…ng trace context

When ActivitySource has no listeners, CreateAndConfigureActivity returns null.
The workaround for dotnet/runtime#47802 was unconditionally setting
Activity.Current = activity (null), destroying trace context for all
downstream middleware in the streaming pipeline.

This applies the same fix from PR #7321 (FunctionInvokingChatClient) to:
- OpenTelemetryChatClient
- OpenTelemetrySpeechToTextClient
- OpenTelemetryTextToSpeechClient
- OpenTelemetryHostedFileClient

Agent-Logs-Url: https://github.com/dotnet/extensions/sessions/53ad8d92-99ca-4075-a7ee-fd3a1551817e

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix OpenTelemetryChatClient streaming null activity issue Guard Activity.Current restore with null check in OpenTelemetry streaming clients Mar 31, 2026
Copilot AI requested a review from stephentoub March 31, 2026 14:31
@stephentoub stephentoub marked this pull request as ready for review March 31, 2026 14:45
Copilot AI review requested due to automatic review settings March 31, 2026 14:45
@stephentoub stephentoub requested a review from a team as a code owner March 31, 2026 14:45
@stephentoub stephentoub added area-ai Microsoft.Extensions.AI libraries and removed area-telemetry labels Mar 31, 2026
@stephentoub stephentoub requested a review from tarekgh March 31, 2026 14:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an OpenTelemetry streaming regression where Activity.Current could be unintentionally cleared (set to null) on each streamed chunk when no ActivitySource listeners are attached.

Changes:

  • Guard Activity.Current = activity restoration with if (activity is not null) in streaming loops for multiple OpenTelemetry client wrappers.
  • Add a unit test asserting Activity.Current is preserved during streaming when the OpenTelemetry ActivitySource has no listeners.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs Adds streaming test to ensure Activity.Current is preserved when no OTel listeners exist.
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs Prevents clearing Activity.Current during streaming when no activity was created.
src/Libraries/Microsoft.Extensions.AI/SpeechToText/OpenTelemetrySpeechToTextClient.cs Same null-guard for streaming activity restoration in speech-to-text.
src/Libraries/Microsoft.Extensions.AI/TextToSpeech/OpenTelemetryTextToSpeechClient.cs Same null-guard for streaming activity restoration in text-to-speech.
src/Libraries/Microsoft.Extensions.AI/Files/OpenTelemetryHostedFileClient.cs Same null-guard for activity restoration in async enumeration of listed files.

@tarekgh tarekgh merged commit 50d05f8 into main Mar 31, 2026
10 checks passed
@tarekgh tarekgh deleted the copilot/fix-opentelemetrychatclient-streaming branch March 31, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenTelemetryChatClient streaming sets Activity.Current = null when ActivitySource has no listeners

4 participants