Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 23, 2025

Summary

This PR implements support for the OpenTelemetry semantic conventions update from open-telemetry/semantic-conventions#2944 by adding a new nullable Name property to OtelMessage that stores the value from ChatMessage.AuthorName.

Changes

Core Implementation

OpenTelemetryChatClient.cs:

  • Added Name property to the private OtelMessage class as a nullable string
  • Updated SerializeChatMessages method to populate OtelMessage.Name from ChatMessage.AuthorName

The property automatically participates in JSON serialization with snake_case naming (name) and respects the existing JsonIgnoreCondition.WhenWritingNull setting, ensuring null values are not included in the serialized output.

Testing

OpenTelemetryChatClientTests.cs:

  • Updated the existing ExpectedInformationLogged_Async test to include AuthorName on two messages
  • Added corresponding expected output validation to verify the name field is correctly serialized in telemetry data
  • Ensures messages without an AuthorName don't include the field (backward compatibility)

Example

When a ChatMessage has an AuthorName set, it will now be included in the telemetry data:

var message = new ChatMessage(ChatRole.User, "Hello!") 
{ 
    AuthorName = "Alice" 
};

Serializes to:

{
  "role": "user",
  "name": "alice",
  "parts": [
    {
      "type": "text",
      "content": "Hello!"
    }
  ]
}

Messages without AuthorName continue to work as before, with no name field in the output.

Testing

✅ All 5 OpenTelemetryChatClient tests pass
✅ Backward compatibility maintained with existing tests
✅ Build successful across all target frameworks (net8.0, net9.0, net462, netstandard2.0)

> [!WARNING]
>
>

Original prompt

In adherence to the otel addition in open-telemetry/semantic-conventions#2944, please update OpenTelemetryChatClient to store ChatMessage.AuthorName as a new nullable string OtelMessage.Name.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Microsoft Reviewers: Open in CodeFlow

…Name

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Update OpenTelemetryChatClient to store ChatMessage.AuthorName Add Name property to OtelMessage to store ChatMessage.AuthorName per OpenTelemetry semantic conventions Oct 23, 2025
Copilot finished work on behalf of stephentoub October 23, 2025 15:50
Copilot AI requested a review from stephentoub October 23, 2025 15:50
…Logged_Async test

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI requested a review from stephentoub October 23, 2025 16:06
Copilot finished work on behalf of stephentoub October 23, 2025 16:06
@stephentoub stephentoub marked this pull request as ready for review October 23, 2025 16:09
@stephentoub stephentoub requested a review from a team as a code owner October 23, 2025 16:09
Copilot AI review requested due to automatic review settings October 23, 2025 16:09
Copy link
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 adds support for OpenTelemetry semantic conventions update by introducing a Name property to store ChatMessage.AuthorName in telemetry data. The change enables tracking of message author names in OpenTelemetry traces when present.

Key changes:

  • Added nullable Name property to OtelMessage class to capture author information
  • Updated message serialization logic to populate the new property from ChatMessage.AuthorName
  • Enhanced test coverage to validate the name field appears correctly in telemetry output

Reviewed Changes

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

File Description
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs Added Name property to OtelMessage class and populated it from ChatMessage.AuthorName during serialization
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs Updated test to set AuthorName on sample messages and verify the name field is correctly included in telemetry output

@stephentoub stephentoub requested a review from JamesNK October 23, 2025 16:09
@stephentoub stephentoub enabled auto-merge (squash) October 23, 2025 16:10
@stephentoub stephentoub added the area-ai Microsoft.Extensions.AI libraries label Oct 24, 2025
@stephentoub stephentoub merged commit 42ae4e2 into main Oct 27, 2025
6 checks passed
@stephentoub stephentoub deleted the copilot/update-otelmessage-name-storage branch October 27, 2025 15:08
jeffhandley pushed a commit to jeffhandley/extensions that referenced this pull request Nov 1, 2025
…OpenTelemetry semantic conventions (dotnet#6953)

* Initial plan

* Add Name property to OtelMessage and populate from ChatMessage.AuthorName

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Refactor: integrate AuthorName test into existing ExpectedInformationLogged_Async test

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
jeffhandley pushed a commit that referenced this pull request Nov 2, 2025
…OpenTelemetry semantic conventions (#6953)

* Initial plan

* Add Name property to OtelMessage and populate from ChatMessage.AuthorName

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Refactor: integrate AuthorName test into existing ExpectedInformationLogged_Async test

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This was referenced Nov 12, 2025
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.

3 participants