Skip to content

Add HistogramBucketBoundaries to the histogram attributes - #7947

Open
dhhoang wants to merge 1 commit into
dotnet:mainfrom
dhhoang:histogram-bucket-boundaries
Open

dhhoang wants to merge 1 commit into
dotnet:mainfrom
dhhoang:histogram-bucket-boundaries

Conversation

@dhhoang

@dhhoang dhhoang commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #6766

This adds a HistogramBucketBoundaries property to HistogramAttribute<T> and HistogramAttribute, and the metrics source generator now passes it to Meter.CreateHistogram as InstrumentAdvice<T>. Right now the only way to get sensible buckets for a source-generated histogram is a View in every app that collects it. With this, the library that defines the histogram can ship the advice itself.

[Histogram<double>("route", Unit = "s", HistogramBucketBoundaries = [0.01, 0.05, 0.1, 0.5, 1, 5, 10])]
public static partial RequestDuration CreateRequestDuration(Meter meter);

generates

var instrument = _meter.CreateHistogram<double>(@"RequestDuration", @"s", null, tags: null,
    advice: new global::System.Diagnostics.Metrics.InstrumentAdvice<double> { HistogramBucketBoundaries = new double[] { 0.01D, 0.05D, 0.1D, 0.5D, 1D, 5D, 10D } });

A few things worth calling out:

  • The property only exists when targeting .NET 10 or later.
  • It's [Experimental] (EXTEXP0003), like Description.
  • Rejects NaN since OpenTelemetry's own View API refuses it .

Testing: the generator unit tests, the generated-code tests on net10.0 and net11.0, and the attribute tests pass locally on Linux. net8.0 and net9.0 only build here, and the net472 tests need Windows, so CI will be the first to run those.

Fixes dotnet#6766

HistogramAttribute and HistogramAttribute<T> get an experimental
HistogramBucketBoundaries property. The metrics source generator passes
the values to Meter.CreateHistogram as InstrumentAdvice<T>, so
collectors such as the OpenTelemetry SDK use them as the histogram's
default explicit bucket boundaries.

The property is only available when targeting .NET 10 or later, where
InstrumentAdvice<T> always exists. The generator reports METGEN020 when
the boundaries aren't distinct and increasing, or contain NaN, rather
than letting InstrumentAdvice<T> throw when the instrument is created.
@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.AI Line 89 87.81 🔻
Microsoft.Extensions.AI Branch 89 79.23 🔻
Microsoft.Extensions.AI.OpenAI Line 75 68.8 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 60.3 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0.75 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.06 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.28 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Branch 99 92.86 🔻
Microsoft.Extensions.Diagnostics.Testing Line 99 98.22 🔻
Microsoft.Extensions.Diagnostics.Testing Branch 99 93.66 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 58.1 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 64.41 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 41.14 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 45.1 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 60.22 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Branch 75 73.96 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 60 🔻
Microsoft.Extensions.Telemetry Line 93 90.47 🔻
Microsoft.Extensions.Telemetry Branch 93 86.83 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 32.15 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 26.31 🔻
Microsoft.Gen.MetricsReports Branch 67 66.86 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.AI.Evaluation.NLP 0 74
Microsoft.Extensions.Caching.Hybrid 81 83
Microsoft.Extensions.DataIngestion 75 77
Microsoft.Extensions.DataIngestion.Markdig 75 88
Microsoft.Extensions.DocumentExtraction 80 94
Microsoft.Extensions.DocumentExtraction.Abstractions 80 100
Microsoft.Extensions.Documents.Abstractions 80 98
Microsoft.Gen.MetadataExtractor 57 71

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1680013&view=codecoverage-tab

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: support setting the advice on Histogram metrics when using source-generated metrics

2 participants