Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[System.Diagnostics.DiagnosticSource] Implement metrics advice API #102524

Merged
merged 13 commits into from
Jun 20, 2024
Next Next commit
Prototype HistogramAdvice API.
  • Loading branch information
CodeBlanch committed Mar 28, 2024
commit bed3ff7d9d5d9e2c55f3c298706a39ea7fc4500c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ internal UpDownCounter(Meter meter, string name, string? unit, string? descripti
}
public sealed class Histogram<T> : Instrument<T> where T : struct
{
public HistogramAdvice<T>? Advice { get { throw null; } }
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
internal Histogram(Meter meter, string name, string? unit, string? description) : base(meter, name, unit, description) { throw null; }
public void Record(T value) { throw null; }
public void Record(T value, System.Collections.Generic.KeyValuePair<string, object?> tag) { throw null; }
Expand Down Expand Up @@ -410,7 +411,8 @@ public class Meter : IDisposable
public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>> tags) where T : struct { throw null; }
public Histogram<T> CreateHistogram<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>> tags) where T : struct { throw null; }
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags) where T : struct { throw null; }
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags, HistogramAdvice<T>? advice) where T : struct { throw null; }
public ObservableCounter<T> CreateObservableCounter<T>(
string name,
Func<T> observeValue,
Expand Down Expand Up @@ -567,4 +569,8 @@ public abstract class ObservableInstrument<T> : Instrument where T : struct
protected ObservableInstrument(Meter meter, string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>> tags) : base(meter, name, unit, description) { throw null; }
protected abstract System.Collections.Generic.IEnumerable<Measurement<T>> Observe();
}
public sealed class HistogramAdvice<T> where T : struct
{
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
public System.Collections.Generic.IReadOnlyList<T>? ExplicitBucketBoundaries { get { throw null; } init { throw null; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\Counter.cs" />
<Compile Include="System\Diagnostics\Metrics\ExponentialHistogramAggregator.cs" />
<Compile Include="System\Diagnostics\Metrics\Histogram.cs" />
<Compile Include="System\Diagnostics\Metrics\HistogramAdvice.cs" />
<Compile Include="System\Diagnostics\Metrics\IMeterFactory.cs" />
<Compile Include="System\Diagnostics\Metrics\Instrument.cs" />
<Compile Include="System\Diagnostics\Metrics\Instrument.common.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ namespace System.Diagnostics.Metrics
/// </remarks>
public sealed class Histogram<T> : Instrument<T> where T : struct
{
internal Histogram(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, tags: null)
public HistogramAdvice<T>? Advice { get; }

internal Histogram(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, tags: null, advice: null)
{
}

internal Histogram(Meter meter, string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags) : base(meter, name, unit, description, tags)
internal Histogram(Meter meter, string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags, HistogramAdvice<T>? advice) : base(meter, name, unit, description, tags)
{
Advice = advice;

Publish();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace System.Diagnostics.Metrics
{
public sealed class HistogramAdvice<T> where T : struct
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
public IReadOnlyList<T>? ExplicitBucketBoundaries { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public Counter<T> CreateCounter<T>(string name, string? unit, string? descriptio
/// <remarks>
/// Example uses for Histogram: the request duration and the size of the response payload.
/// </remarks>
public Histogram<T> CreateHistogram<T>(string name, string? unit = null, string? description = null) where T : struct => CreateHistogram<T>(name, unit, description, tags: null);
public Histogram<T> CreateHistogram<T>(string name, string? unit = null, string? description = null) where T : struct
=> CreateHistogram<T>(name, unit, description, tags: null, advice: null);

/// <summary>
/// Histogram is an Instrument which can be used to report arbitrary values that are likely to be statistically meaningful. It is intended for statistics such as histograms, summaries, and percentile.
Expand All @@ -165,7 +166,21 @@ public Counter<T> CreateCounter<T>(string name, string? unit, string? descriptio
/// Example uses for Histogram: the request duration and the size of the response payload.
/// </remarks>
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags) where T : struct
=> (Histogram<T>)GetOrCreateInstrument<T>(typeof(Histogram<T>), name, unit, description, tags, () => new Histogram<T>(this, name, unit, description, tags));
=> CreateHistogram<T>(name, unit, description, tags, advice: null);

/// <summary>
/// Histogram is an Instrument which can be used to report arbitrary values that are likely to be statistically meaningful. It is intended for statistics such as histograms, summaries, and percentile.
/// </summary>
/// <param name="name">The instrument name. cannot be null.</param>
/// <param name="unit">Optional instrument unit of measurements.</param>
/// <param name="description">Optional instrument description.</param>
/// <param name="tags">tags to attach to the counter.</param>
/// <param name="advice"><see cref="HistogramAdvice{T}"/> to attach to the counter.</param>
/// <remarks>
/// Example uses for Histogram: the request duration and the size of the response payload.
/// </remarks>
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags, HistogramAdvice<T>? advice) where T : struct
=> (Histogram<T>)GetOrCreateInstrument<T>(typeof(Histogram<T>), name, unit, description, tags, () => new Histogram<T>(this, name, unit, description, tags, advice));

/// <summary>
/// Create a metrics UpDownCounter object.
Expand Down