Skip to content

Commit c529580

Browse files
tarekghcijothomas
andauthored
Add Gauge Metrics Instrument (#103543)
* Add Gauge Metrics Instrument * Addressing the feedback * Apply suggestions from code review Co-authored-by: Cijo Thomas <[email protected]> --------- Co-authored-by: Cijo Thomas <[email protected]>
1 parent eb14da9 commit c529580

File tree

7 files changed

+424
-59
lines changed

7 files changed

+424
-59
lines changed

src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ public sealed class Counter<T> : Instrument<T> where T : struct
350350
internal Counter(Meter meter, string name, string? unit, string? description) :
351351
base(meter, name, unit, description) { throw null; }
352352
}
353+
public sealed class Gauge<T> : Instrument<T> where T : struct
354+
{
355+
public void Record(T value) { throw null; }
356+
public void Record(T value, System.Collections.Generic.KeyValuePair<string, object?> tag) { throw null; }
357+
public void Record(T value, System.Collections.Generic.KeyValuePair<string, object?> tag1, System.Collections.Generic.KeyValuePair<string, object?> tag2) { throw null; }
358+
public void Record(T value, System.Collections.Generic.KeyValuePair<string, object?> tag1, System.Collections.Generic.KeyValuePair<string, object?> tag2, System.Collections.Generic.KeyValuePair<string, object?> tag3) { throw null; }
359+
public void Record(T value, params System.ReadOnlySpan<System.Collections.Generic.KeyValuePair<string, object?>> tags) { throw null; }
360+
public void Record(T value, params System.Collections.Generic.KeyValuePair<string, object?>[] tags) { throw null; }
361+
public void Record(T value, in TagList tagList) { throw null; }
362+
internal Gauge(Meter meter, string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>>? tags) :
363+
base(meter, name, unit, description, tags) { throw null; }
364+
}
353365
public sealed class UpDownCounter<T> : Instrument<T> where T : struct
354366
{
355367
public void Add(T delta) { throw null; }
@@ -413,10 +425,12 @@ public abstract class Instrument<T> : Instrument where T : struct
413425
public delegate void MeasurementCallback<T>(Instrument instrument, T measurement, ReadOnlySpan<System.Collections.Generic.KeyValuePair<string, object?>> tags, object? state) where T : struct;
414426
public class Meter : IDisposable
415427
{
416-
public Counter<T> CreateCounter<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
417-
public Counter<T> CreateCounter<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>> tags) where T : struct { throw null; }
418-
public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
419-
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; }
428+
public Counter<T> CreateCounter<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
429+
public Counter<T> CreateCounter<T>(string name, string? unit, string? description, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object?>> tags) where T : struct { throw null; }
430+
public Gauge<T> CreateGauge<T>(string name) where T : struct { throw null; }
431+
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; }
432+
public UpDownCounter<T> CreateUpDownCounter<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
433+
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; }
420434
public Histogram<T> CreateHistogram<T>(string name, string? unit = null, string? description = null) where T : struct { throw null; }
421435
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; }
422436
public ObservableCounter<T> CreateObservableCounter<T>(

src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
5353
<Compile Include="System\Diagnostics\Metrics\AggregatorStore.cs" />
5454
<Compile Include="System\Diagnostics\Metrics\Counter.cs" />
5555
<Compile Include="System\Diagnostics\Metrics\ExponentialHistogramAggregator.cs" />
56+
<Compile Include="System\Diagnostics\Metrics\Gauge.cs" />
5657
<Compile Include="System\Diagnostics\Metrics\Histogram.cs" />
5758
<Compile Include="System\Diagnostics\Metrics\IMeterFactory.cs" />
5859
<Compile Include="System\Diagnostics\Metrics\Instrument.cs" />

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/AggregationManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private void RemoveInstrumentState(Instrument instrument)
316316
}
317317
};
318318
}
319-
else if (genericDefType == typeof(ObservableGauge<>))
319+
else if (genericDefType == typeof(ObservableGauge<>) || genericDefType == typeof(Gauge<>))
320320
{
321321
return () =>
322322
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
6+
namespace System.Diagnostics.Metrics
7+
{
8+
/// <summary>
9+
/// The Gauge is an instrument used to record non-additive values whenever changes occur. For example, record the room background noise level value when changes occur.
10+
/// </summary>
11+
/// <remarks>
12+
/// 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" />
13+
/// </remarks>
14+
public sealed class Gauge<T> : Instrument<T> where T : struct
15+
{
16+
internal Gauge(Meter meter, string name, string? unit, string? description) : this(meter, name, unit, description, null)
17+
{
18+
}
19+
20+
internal Gauge(Meter meter, string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags) : base(meter, name, unit, description, tags)
21+
{
22+
Publish();
23+
}
24+
25+
/// <summary>
26+
/// Record the Gauge current value.
27+
/// </summary>
28+
/// <param name="value">The Gauge current value.</param>
29+
public void Record(T value) => RecordMeasurement(value);
30+
31+
/// <summary>
32+
/// Record the Gauge current value.
33+
/// </summary>
34+
/// <param name="value">The Gauge current value.</param>
35+
/// <param name="tag">A key-value pair tag associated with the measurement.</param>
36+
public void Record(T value, KeyValuePair<string, object?> tag) => RecordMeasurement(value, tag);
37+
38+
/// <summary>
39+
/// Record the Gauge current value.
40+
/// </summary>
41+
/// <param name="value">The Gauge current value.</param>
42+
/// <param name="tag1">A first key-value pair tag associated with the measurement.</param>
43+
/// <param name="tag2">A second key-value pair tag associated with the measurement.</param>
44+
public void Record(T value, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2) => RecordMeasurement(value, tag1, tag2);
45+
46+
/// <summary>
47+
/// Record the Gauge current value.
48+
/// </summary>
49+
/// <param name="value">The Gauge current value.</param>
50+
/// <param name="tag1">A first key-value pair tag associated with the measurement.</param>
51+
/// <param name="tag2">A second key-value pair tag associated with the measurement.</param>
52+
/// <param name="tag3">A third key-value pair tag associated with the measurement.</param>
53+
public void Record(T value, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2, KeyValuePair<string, object?> tag3) => RecordMeasurement(value, tag1, tag2, tag3);
54+
55+
/// <summary>
56+
/// Record the Gauge current value.
57+
/// </summary>
58+
/// <param name="value">The Gauge current value.</param>
59+
/// <param name="tags">A span of key-value pair tags associated with the measurement.</param>
60+
public void Record(T value, params ReadOnlySpan<KeyValuePair<string, object?>> tags) => RecordMeasurement(value, tags);
61+
62+
/// <summary>
63+
/// Record the Gauge current value.
64+
/// </summary>
65+
/// <param name="value">The Gauge current value.</param>
66+
/// <param name="tags">A list of key-value pair tags associated with the measurement.</param>
67+
public void Record(T value, params KeyValuePair<string, object?>[] tags) => RecordMeasurement(value, tags.AsSpan());
68+
69+
/// <summary>
70+
/// Record the Gauge current value.
71+
/// </summary>
72+
/// <param name="value">The Gauge current value.</param>
73+
/// <param name="tagList">A <see cref="T:System.Diagnostics.TagList" /> of tags associated with the measurement.</param>
74+
public void Record(T value, in TagList tagList) => RecordMeasurement(value, in tagList);
75+
}
76+
}

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Meter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ private void Initialize(string name, string? version, IEnumerable<KeyValuePair<s
143143
public Counter<T> CreateCounter<T>(string name, string? unit, string? description, IEnumerable<KeyValuePair<string, object?>>? tags) where T : struct
144144
=> (Counter<T>)GetOrCreateInstrument<T>(typeof(Counter<T>), name, unit, description, tags, () => new Counter<T>(this, name, unit, description, tags));
145145

146+
/// <summary>
147+
/// Creates a Gauge instrument, which can be used to record non-additive values.
148+
/// </summary>
149+
/// <param name="name">The instrument name. cannot be null.</param>
150+
/// <remarks>
151+
/// Gauge is an Instrument which used to record non-additive values.
152+
/// Example uses for Gauge: record the room background noise level value when changes occur.
153+
/// </remarks>
154+
public Gauge<T> CreateGauge<T>(string name) where T : struct => CreateGauge<T>(name, unit: null, description: null, tags: null);
155+
156+
/// <summary>
157+
/// Create a metrics Gauge object.
158+
/// </summary>
159+
/// <param name="name">The instrument name. cannot be null.</param>
160+
/// <param name="unit">Optional instrument unit of measurements.</param>
161+
/// <param name="description">Optional instrument description.</param>
162+
/// <param name="tags">tags to attach to the Gauge.</param>
163+
/// <remarks>
164+
/// Gauge is an Instrument which used to record non-additive values.
165+
/// Example uses for Gauge: record the room background noise level value when changes occur.
166+
/// </remarks>
167+
public Gauge<T> CreateGauge<T>(string name, string? unit = null, string? description = null, IEnumerable<KeyValuePair<string, object?>>? tags = null) where T : struct
168+
=> (Gauge<T>)GetOrCreateInstrument<T>(typeof(Gauge<T>), name, unit, description, tags, () => new Gauge<T>(this, name, unit, description, tags));
169+
146170
/// <summary>
147171
/// 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.
148172
/// </summary>

0 commit comments

Comments
 (0)