Skip to content

Add "hints" in Metric API to provide things like histogram bounds #63650

@tarekgh

Description

@tarekgh

Metrics currently supports the Histogram instrument, which is used to publish measurement data. The data aggregator (such as the OpenTelemetry SDK) divides the full interval of possible values into consecutive, non-overlapping ranges called buckets. The OpenTelemetry SDK currently uses some defaults to define the bucket boundaries, but it also provides a way to customize or configure these boundaries. OpenTelemetry has introduced a new specification that allows creators of the Histogram instrument to specify advised bucket boundary values, which will be the recommended values for Histogram data aggregation.

Explicit bucket boundaries OpenTelemetry specs.

Proposal

namespace System.Diagnostics.Metrics;

+public sealed class HistogramAdvice<T> where T : struct
+{
+    public HistogramAdvice(IEnumerable<T> explicitBucketBoundaries) { ... }
+    public IEnumerable<T> ExplicitBucketBoundaries { get; }
+}

public class Meter
{
    public System.Diagnostics.Metrics.Histogram<T> CreateHistogram<T> (
        string name,
        string? unit = default,
        string? description = default) where T : struct;

    public Histogram<T> CreateHistogram<T>(
        string name,
        string? unit,
        string? description,
        IEnumerable<KeyValuePair<string, object?>>? tags) where T : struct;

+   public System.Diagnostics.Metrics.Histogram<T> CreateHistogram<T> (
+       string name,
+       string? unit,
+       string? description,
+       IEnumerable<KeyValuePair<string, object?>>? tags,
+       HistogramAdvice<T> advice) where T : struct;
}

+public sealed class Histogram<T> : System.Diagnostics.Metrics.Instrument<T> where T : struct
+{
+    public HistogramAdvice<T>? Advice { get; }
+}

Usage Example

private static readonly Meter MyMeter = new("MyApplicationMeter");​

...

Histogram<long> requestLatencyHistogram = MyMeter.CreateHistogram<long>(
                                           name: "RequestLatency", 
                                           unit: "ms", 
                                           description: "Request Latency", 
                                           tags: null, 
                                           new HistogramAdvice<long>(new long[] { 0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000 }));requestLatencyHistogram.Record(150)

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Diagnostics.MetricblockingMarks issues that we want to fast track in order to unblock other important workfeature-requestin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions