Skip to content

.NET: Propagate EnableSensitiveData to auto-wired inner OpenTelemetryChatClient#6096

Open
Copilot wants to merge 1 commit into
mainfrom
copilot/red-green-unit-tests-issue-5873
Open

.NET: Propagate EnableSensitiveData to auto-wired inner OpenTelemetryChatClient#6096
Copilot wants to merge 1 commit into
mainfrom
copilot/red-green-unit-tests-issue-5873

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 26, 2026

Motivation and Context

When EnableSensitiveData = true is set on OpenTelemetryAgent and auto-wiring is active, the inner OpenTelemetryChatClient created by GetRunOptionsWithChatClientWiring was always instantiated with the default EnableSensitiveData = false. This caused the inner chat span to silently omit gen_ai.input.messages / gen_ai.output.messages even when the caller explicitly opted in to sensitive data capture — violating the OpenTelemetry GenAI semantic conventions contract described in the issue.

Description

Root cause: WrapIfNeeded was a zero-parameter static local that called UseOpenTelemetry(sourceName: sourceName) with no configure callback, so EnableSensitiveData was never forwarded to the auto-wired client.

Fix (OpenTelemetryAgent.cs):

// Before
static IChatClient WrapIfNeeded(IChatClient cc, string sourceName) =>
    cc.GetService(typeof(OpenTelemetryChatClient)) is not null
        ? cc
        : cc.AsBuilder().UseOpenTelemetry(sourceName: sourceName).Build();

// After
bool enableSensitiveData = this.EnableSensitiveData;
static IChatClient WrapIfNeeded(IChatClient cc, string sourceName, bool enableSensitiveData) =>
    cc.GetService(typeof(OpenTelemetryChatClient)) is not null
        ? cc
        : cc.AsBuilder()
            .UseOpenTelemetry(sourceName: sourceName, configure: o => o.EnableSensitiveData = enableSensitiveData)
            .Build();

The value is read once at GetRunOptionsWithChatClientWiring call-time and captured in the factory closure, consistent with how sourceName is already handled. Both the ChatClientAgentRunOptions clone path and the plain AgentRunOptions → new-options path are updated.

Test (OpenTelemetryAgentTests.cs): Added AutoWireChatClient_EnableSensitiveData_PropagatedToInnerChatClient_Async — a 4-variant theory (enableSensitiveData × streaming) written red-first to replicate the bug, then green after the fix. Asserts that the inner chat span includes/excludes gen_ai.input.messages and gen_ai.output.messages in lock-step with the outer agent's EnableSensitiveData flag.

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.

…hatClient

When _autoWireChatClient=true and the caller sets EnableSensitiveData=true on
the outer OpenTelemetryAgent, the auto-wired inner OpenTelemetryChatClient now
also has EnableSensitiveData propagated, so the inner chat span correctly
captures message content (gen_ai.input.messages / gen_ai.output.messages).

Red test added first to reproduce the bug, then the fix applied (green).

Fixes #5873

Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/fda69dd4-9576-4f3f-b954-514321652ea9

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Copilot AI self-assigned this May 26, 2026
Copilot AI review requested due to automatic review settings May 26, 2026 20:11
Copilot AI review requested due to automatic review settings May 26, 2026 20:11
@rogerbarreto rogerbarreto changed the title fix(.NET): Propagate EnableSensitiveData to auto-wired inner OpenTelemetryChatClient .NET: Propagate EnableSensitiveData to auto-wired inner OpenTelemetryChatClient May 26, 2026
@moonbox3 moonbox3 added the .NET label May 26, 2026
@rogerbarreto rogerbarreto marked this pull request as ready for review May 26, 2026 20:13
Copilot AI review requested due to automatic review settings May 26, 2026 20:13
@rogerbarreto rogerbarreto enabled auto-merge May 26, 2026 20:13
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

This PR fixes a telemetry configuration propagation bug in the .NET OpenTelemetryAgent auto-wiring path: when the agent is configured with EnableSensitiveData = true, the implicitly-created inner OpenTelemetryChatClient is now configured the same way, ensuring GenAI message-content tags are emitted consistently.

Changes:

  • Propagate OpenTelemetryAgent.EnableSensitiveData into the auto-wired inner OpenTelemetryChatClient created by GetRunOptionsWithChatClientWiring.
  • Add a regression theory test covering EnableSensitiveData × (streaming vs non-streaming) and asserting gen_ai.input.messages / gen_ai.output.messages presence in the inner chat span.

Reviewed changes

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

File Description
dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs Forwards EnableSensitiveData to the auto-wired inner OpenTelemetryChatClient via the UseOpenTelemetry(..., configure: ...) callback.
dotnet/tests/Microsoft.Agents.AI.UnitTests/OpenTelemetryAgentTests.cs Adds a regression theory verifying sensitive-data capture tags are present/absent on the inner chat span as expected.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Use OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to control sensitive data capture in telemetry

4 participants