Implement time_to_first_chunk and time_per_output_chunk streaming metrics in OpenTelemetryChatClient#7325
Merged
stephentoub merged 7 commits intomainfrom Feb 22, 2026
Conversation
…hunk metrics Implement the two new streaming metrics from OpenTelemetry semantic conventions: - gen_ai.client.operation.time_to_first_chunk: Time from request to first chunk - gen_ai.client.operation.time_per_output_chunk: Time between consecutive chunks These metrics are only recorded for streaming calls per the spec. Co-authored-by: stephentoub <[email protected]>
Copilot
AI
changed the title
[WIP] Add new metrics to OpenTelemetryChatClient
Implement time_to_first_chunk and time_per_output_chunk streaming metrics in OpenTelemetryChatClient
Feb 20, 2026
stephentoub
reviewed
Feb 20, 2026
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
Address review feedback: - Record time_per_output_chunk and time_to_first_chunk metrics as each chunk arrives, giving better timestamp accuracy and avoiding list allocation - Remove RecordStreamingChunkMetrics method and interChunkTimes list Co-authored-by: stephentoub <[email protected]>
stephentoub
reviewed
Feb 22, 2026
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
- Simplify condition to just `if (trackChunkTimes)` with Debug.Assert - Move TagList creation outside the loop since tags don't change per chunk - Remove AddMetricTags wrapper, call PopulateMetricTags directly Co-authored-by: stephentoub <[email protected]>
stephentoub
reviewed
Feb 22, 2026
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Outdated
Show resolved
Hide resolved
The original local function was just promoted to a private method to be reusable from the streaming path. No rename was needed. Co-authored-by: stephentoub <[email protected]>
stephentoub
reviewed
Feb 22, 2026
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs
Show resolved
Hide resolved
The loop now captures the first non-null ModelId from ChatResponseUpdate objects and adds it to the chunk metric tags, so that gen_ai.response.model is included in time_to_first_chunk and time_per_output_chunk metrics during streaming. Co-authored-by: stephentoub <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds OpenTelemetry GenAI streaming-only chunk timing metrics to OpenTelemetryChatClient, aligning the client’s metric surface with the newer semantic conventions for streaming latency.
Changes:
- Introduces
gen_ai.client.operation.time_to_first_chunkandgen_ai.client.operation.time_per_output_chunkconstants (including bucket boundaries aligned to operation duration). - Adds two new
Histogram<double>instruments and records per-chunk timing measurements during streaming enumeration. - Adds unit tests validating streaming metrics are emitted (and non-streaming calls do not emit them).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs |
Adds metric name/description/bucket boundary constants for the two new streaming histograms. |
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs |
Creates and records the new histograms during GetStreamingResponseAsync; refactors metric tag helper for reuse. |
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs |
Adds tests using MetricCollector<double> to validate emitted measurement counts/tags for streaming and absence for non-streaming. |
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs
Show resolved
Hide resolved
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.
Implements the two new streaming metrics from the OpenTelemetry GenAI semantic conventions:
gen_ai.client.operation.time_to_first_chunk— Time from request issuance to first chunk receivedgen_ai.client.operation.time_per_output_chunk— Time between consecutive chunks, recorded for each chunk after the firstBoth are
Histogram<double>in seconds, streaming-only, using the same bucket boundaries asgen_ai.client.operation.duration. Metrics are recorded inline as each chunk arrives for accurate timestamps.Changes
OpenTelemetryConsts.cs— AddedTimeToFirstChunkandTimePerOutputChunkmetric constant classes underGenAI.ClientOpenTelemetryChatClient.csGetStreamingResponseAsyncrecords chunk timing metrics inline as each chunk is received via elapsed deltas on the existingStopwatchgen_ai.response.modelis captured from the firstChatResponseUpdatewith a non-nullModelIdAddMetricTagsfrom local function to private method for reuse from bothTraceResponseand the streaming pathOpenTelemetryChatClientTests.cs— Two new tests: one verifying correct metric counts, tags (includinggen_ai.response.model), and values for streaming; one verifying no metrics emitted for non-streaming calls🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.
Microsoft Reviewers: Open in CodeFlow