Skip to content

Implement time_to_first_chunk and time_per_output_chunk streaming metrics in OpenTelemetryChatClient#7325

Merged
stephentoub merged 7 commits intomainfrom
copilot/add-new-metrics-open-telemetry
Feb 22, 2026
Merged

Implement time_to_first_chunk and time_per_output_chunk streaming metrics in OpenTelemetryChatClient#7325
stephentoub merged 7 commits intomainfrom
copilot/add-new-metrics-open-telemetry

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

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 received
  • gen_ai.client.operation.time_per_output_chunk — Time between consecutive chunks, recorded for each chunk after the first

Both are Histogram<double> in seconds, streaming-only, using the same bucket boundaries as gen_ai.client.operation.duration. Metrics are recorded inline as each chunk arrives for accurate timestamps.

Changes

  • OpenTelemetryConsts.cs — Added TimeToFirstChunk and TimePerOutputChunk metric constant classes under GenAI.Client
  • OpenTelemetryChatClient.cs
    • Two new histogram fields initialized in constructor
    • GetStreamingResponseAsync records chunk timing metrics inline as each chunk is received via elapsed deltas on the existing Stopwatch
    • Metric tags are populated once before the loop; gen_ai.response.model is captured from the first ChatResponseUpdate with a non-null ModelId
    • Promoted AddMetricTags from local function to private method for reuse from both TraceResponse and the streaming path
  • OpenTelemetryChatClientTests.cs — Two new tests: one verifying correct metric counts, tags (including gen_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

…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
Copilot AI requested a review from stephentoub February 20, 2026 13:39
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Feb 20, 2026
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]>
- 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]>
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]>
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]>
Copilot AI requested a review from stephentoub February 22, 2026 03:48
@stephentoub stephentoub marked this pull request as ready for review February 22, 2026 03:52
@stephentoub stephentoub requested a review from a team as a code owner February 22, 2026 03:52
Copilot AI review requested due to automatic review settings February 22, 2026 03:52
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

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_chunk and gen_ai.client.operation.time_per_output_chunk constants (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.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM!

@stephentoub stephentoub merged commit 4939788 into main Feb 22, 2026
6 checks passed
@stephentoub stephentoub deleted the copilot/add-new-metrics-open-telemetry branch February 22, 2026 16:49
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.

4 participants