Skip to content

Commit

Permalink
[System.Diagnostics.DiagnosticSource] Implement metrics advice API (d…
Browse files Browse the repository at this point in the history
…otnet#102524)

* Prototype HistogramAdvice API.

* Updates.

* Add tests.

* Code review.

* Code review.

* Tweaks.

* Code review.

* Revisions.

* Tweaks.

* Code review.

* Add InstrumentAdvice ctor to ref.
  • Loading branch information
CodeBlanch authored Jun 20, 2024
1 parent d9b9676 commit 7377eda
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ public abstract class Instrument
{
public string? Description { get {throw null;} }
public bool Enabled { get {throw null; } }
protected Instrument(Meter meter, string name, string? unit, string? description) {throw null;}
protected Instrument(Meter meter, string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags) {throw null;}
protected Instrument(Meter meter, string name) : this(meter, name, unit: null, description: null, tags: null) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
protected Instrument(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, tags: null) { throw null; }
protected Instrument(Meter meter, string name, string? unit = default, string? description = default, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags = default) { throw null; }
public virtual bool IsObservable { get {throw null; } }
public Meter Meter { get {throw null;} }
public string Name { get {throw null;} }
Expand All @@ -404,15 +406,31 @@ public abstract class Instrument
}
public abstract class Instrument<T> : Instrument where T : struct
{
protected Instrument(Meter meter, string name, string? unit, string? description) : base(meter, name, unit, description) { throw null; }
protected Instrument(Meter meter, string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags) : base(meter, name, unit, description, tags) {throw null;}
public InstrumentAdvice<T>? Advice { get { throw null; } }
protected Instrument(Meter meter, string name) : this(meter, name, unit: null, description: null, tags: null, advice: null) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
protected Instrument(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, tags: null, advice: null) { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
protected Instrument(Meter meter, string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags) : this(meter, name, unit, description, tags, advice: null) { throw null; }
protected Instrument(
Meter meter,
string name,
string? unit = default,
string? description = default,
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags = default,
InstrumentAdvice<T>? advice = default) : base(meter, name, unit, description, tags) { throw null; }
protected void RecordMeasurement(T measurement) { throw null; }
protected void RecordMeasurement(T measurement, System.Collections.Generic.KeyValuePair<string, object?> tag) { throw null; }
protected void RecordMeasurement(T measurement, System.Collections.Generic.KeyValuePair<string, object?> tag1, System.Collections.Generic.KeyValuePair<string, object?> tag2) { throw null; }
protected void RecordMeasurement(T measurement, System.Collections.Generic.KeyValuePair<string, object?> tag1, System.Collections.Generic.KeyValuePair<string, object?> tag2, System.Collections.Generic.KeyValuePair<string, object?> tag3) { throw null; }
protected void RecordMeasurement(T measurement, in TagList tagList) { throw null; }
protected void RecordMeasurement(T measurement, ReadOnlySpan<System.Collections.Generic.KeyValuePair<string, object?>> tags) { throw null; }
}
public sealed class InstrumentAdvice<T> where T : struct
{
public InstrumentAdvice() { throw null; }
public System.Collections.Generic.IReadOnlyList<T>? HistogramBucketBoundaries { get { throw null; } init { throw null; } }
}
public readonly struct Measurement<T> where T : struct
{
public Measurement(T value) { throw null; }
Expand All @@ -431,8 +449,12 @@ public class Meter : IDisposable
public Gauge<T> CreateGauge<T>(string name, string? unit = null, string? description = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags = null) where T : struct { throw null; }
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) where T : struct { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public Histogram<T> CreateHistogram<T>(string name, string? unit, string? description) where T : struct { throw null; }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
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 = default, string? description = default, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags = default, InstrumentAdvice<T>? advice = default) where T : struct { throw null; }
public ObservableCounter<T> CreateObservableCounter<T>(
string name,
Func<T> observeValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -174,4 +174,7 @@
<data name="InvalidInstrumentType" xml:space="preserve">
<value>The instrument is of different generic type.</value>
</data>
<data name="InvalidHistogramExplicitBucketBoundaries" xml:space="preserve">
<value>Histogram explicit bucket boundaries must be specified in ascending order and cannot contain duplicate values.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\ExponentialHistogramAggregator.cs" />
<Compile Include="System\Diagnostics\Metrics\Gauge.cs" />
<Compile Include="System\Diagnostics\Metrics\Histogram.cs" />
<Compile Include="System\Diagnostics\Metrics\InstrumentAdvice.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 Expand Up @@ -93,10 +94,8 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\ObjectSequence.netcore.cs" />
<Compile Include="System\Diagnostics\Metrics\StringSequence.netcore.cs" />
<Compile Include="System\Diagnostics\System.Diagnostics.DiagnosticSource.Typeforwards.netcoreapp.cs" />
<Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs"
Link="Common\System\LocalAppContextSwitches.Common.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs" Link="Common\System\LocalAppContextSwitches.Common.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs" Link="Common\System\Text\ValueStringBuilder.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ 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)
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, InstrumentAdvice<T>? advice)
: base(meter, name, unit, description, tags, advice)
{
Publish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.ComponentModel;

namespace System.Diagnostics.Metrics
{
/// <summary>
/// Instrument{T} is the base class for all non-observable instruments.
/// <see cref="Instrument{T}"/> is the base class for all non-observable instruments.
/// </summary>
/// <remarks>
/// This class supports only the following generic parameter types: <see cref="byte" />, <see cref="short" />, <see cref="int" />, <see cref="long" />, <see cref="float" />, <see cref="double" />, and <see cref="decimal" />
Expand All @@ -15,28 +16,67 @@ namespace System.Diagnostics.Metrics
public abstract partial class Instrument<T> : Instrument where T : struct
{
/// <summary>
/// Create the metrics instrument using the properties meter, name, description, and unit.
/// All classes extending Instrument{T} need to call this constructor when constructing object of the extended class.
/// Gets the <see cref="InstrumentAdvice{T}"/> associated with the instrument.
/// </summary>
/// <param name="meter">The meter that created the instrument.</param>
/// <param name="name">The instrument name. cannot be null.</param>
public InstrumentAdvice<T>? Advice { get; }

/// <summary>
/// Constructs a new instance of <see cref="Instrument{T}"/>.
/// </summary>
/// <param name="meter">The meter that created the instrument. Cannot be null.</param>
/// <param name="name">The instrument name. Cannot be null.</param>
protected Instrument(Meter meter, string name)
: this(meter, name, unit: null, description: null, tags: null, advice: null)
{
}

/// <summary>
/// Constructs a new instance of <see cref="Instrument{T}"/>.
/// </summary>
/// <param name="meter">The meter that created the instrument. Cannot be null.</param>
/// <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>
protected Instrument(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, tags: null)
[EditorBrowsable(EditorBrowsableState.Never)]
protected Instrument(Meter meter, string name, string? unit, string? description)
: this(meter, name, unit, description, tags: null, advice: null)
{
}

/// <summary>
/// Create the metrics instrument using the properties meter, name, description, and unit.
/// All classes extending Instrument{T} need to call this constructor when constructing object of the extended class.
/// Constructs a new instance of <see cref="Instrument{T}"/>.
/// </summary>
/// <param name="meter">The meter that created the instrument.</param>
/// <param name="name">The instrument name. cannot be null.</param>
/// <param name="meter">The meter that created the instrument. Cannot be null.</param>
/// <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">Optional instrument tags.</param>
protected Instrument(Meter meter, string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags) : base(meter, name, unit, description, tags)
[EditorBrowsable(EditorBrowsableState.Never)]
protected Instrument(Meter meter, string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags)
: this(meter, name, unit, description, tags, advice: null)
{
}

/// <summary>
/// Constructs a new instance of <see cref="Instrument{T}"/>.
/// </summary>
/// <param name="meter">The meter that created the instrument. Cannot be null.</param>
/// <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">Optional instrument tags.</param>
/// <param name="advice">Optional <see cref="InstrumentAdvice{T}"/>.</param>
protected Instrument(
Meter meter,
string name,
string? unit = default,
string? description = default,
IEnumerable<KeyValuePair<string, object?>>? tags = default,
InstrumentAdvice<T>? advice = default)
: base(meter, name, unit, description, tags)
{
Advice = advice;

ValidateTypeParameter<T>();
}

Expand Down
Loading

0 comments on commit 7377eda

Please sign in to comment.