Skip to content

Commit e210d90

Browse files
authored
Publish Aggregated Values For Counter and UpDownCounter For System.Diagnostics.Metrics (#84846)
1 parent 634cc1e commit e210d90

File tree

5 files changed

+108
-98
lines changed

5 files changed

+108
-98
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
6262
<Compile Include="System\Diagnostics\Metrics\ObservableGauge.cs" />
6363
<Compile Include="System\Diagnostics\Metrics\ObservableInstrument.cs" />
6464
<Compile Include="System\Diagnostics\Metrics\ObservableUpDownCounter.cs" />
65-
<Compile Include="System\Diagnostics\Metrics\RateAggregator.cs" />
65+
<Compile Include="System\Diagnostics\Metrics\CounterAggregator.cs" />
6666
<Compile Include="System\Diagnostics\Metrics\StringSequence.cs" />
6767
<Compile Include="System\Diagnostics\Metrics\TagList.cs" />
6868
<Compile Include="System\Diagnostics\Metrics\UpDownCounter.cs" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private void RemoveInstrumentState(Instrument instrument)
275275
{
276276
lock (this)
277277
{
278-
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: true) : null;
278+
return CheckTimeSeriesAllowed() ? new CounterAggregator(isMonotonic: true) : null;
279279
}
280280
};
281281
}
@@ -285,7 +285,7 @@ private void RemoveInstrumentState(Instrument instrument)
285285
{
286286
lock (this)
287287
{
288-
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: true) : null;
288+
return CheckTimeSeriesAllowed() ? new ObservableCounterAggregator(isMonotonic: true) : null;
289289
}
290290
};
291291
}
@@ -318,7 +318,7 @@ private void RemoveInstrumentState(Instrument instrument)
318318
{
319319
lock (this)
320320
{
321-
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: false) : null;
321+
return CheckTimeSeriesAllowed() ? new CounterAggregator(isMonotonic: false) : null;
322322
}
323323
};
324324
}
@@ -328,7 +328,7 @@ private void RemoveInstrumentState(Instrument instrument)
328328
{
329329
lock (this)
330330
{
331-
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: false) : null;
331+
return CheckTimeSeriesAllowed() ? new ObservableCounterAggregator(isMonotonic: false) : null;
332332
}
333333
};
334334
}

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/RateAggregator.cs renamed to src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/CounterAggregator.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
namespace System.Diagnostics.Metrics
55
{
6-
internal sealed class RateSumAggregator : Aggregator
6+
internal sealed class CounterAggregator : Aggregator
77
{
88
private readonly bool _isMonotonic;
9-
private double _sum;
9+
private double _delta;
10+
private double _aggregatedValue;
1011

11-
public RateSumAggregator(bool isMonotonic)
12+
public CounterAggregator(bool isMonotonic)
1213
{
1314
_isMonotonic = isMonotonic;
1415
}
@@ -17,28 +18,29 @@ public override void Update(double value)
1718
{
1819
lock (this)
1920
{
20-
_sum += value;
21+
_delta += value;
2122
}
2223
}
2324

2425
public override IAggregationStatistics Collect()
2526
{
2627
lock (this)
2728
{
28-
RateStatistics? stats = new RateStatistics(_sum, _isMonotonic);
29-
_sum = 0;
29+
_aggregatedValue += _delta;
30+
CounterStatistics? stats = new CounterStatistics(_delta, _isMonotonic, _aggregatedValue);
31+
_delta = 0;
3032
return stats;
3133
}
3234
}
3335
}
3436

35-
internal sealed class RateAggregator : Aggregator
37+
internal sealed class ObservableCounterAggregator : Aggregator
3638
{
3739
private readonly bool _isMonotonic;
3840
private double? _prevValue;
39-
private double _value;
41+
private double _currValue;
4042

41-
public RateAggregator(bool isMonotonic)
43+
public ObservableCounterAggregator(bool isMonotonic)
4244
{
4345
_isMonotonic = isMonotonic;
4446
}
@@ -47,7 +49,7 @@ public override void Update(double value)
4749
{
4850
lock (this)
4951
{
50-
_value = value;
52+
_currValue = value;
5153
}
5254
}
5355

@@ -58,25 +60,29 @@ public override IAggregationStatistics Collect()
5860
double? delta = null;
5961
if (_prevValue.HasValue)
6062
{
61-
delta = _value - _prevValue.Value;
63+
delta = _currValue - _prevValue.Value;
6264
}
63-
RateStatistics stats = new RateStatistics(delta, _isMonotonic);
64-
_prevValue = _value;
65+
66+
CounterStatistics stats = new CounterStatistics(delta, _isMonotonic, _currValue);
67+
_prevValue = _currValue;
6568
return stats;
6669
}
6770
}
6871
}
6972

70-
internal sealed class RateStatistics : IAggregationStatistics
73+
internal sealed class CounterStatistics : IAggregationStatistics
7174
{
72-
public RateStatistics(double? delta, bool isMonotonic)
75+
public CounterStatistics(double? delta, bool isMonotonic, double value)
7376
{
7477
Delta = delta;
7578
IsMonotonic = isMonotonic;
79+
Value = value;
7680
}
7781

7882
public double? Delta { get; }
7983

8084
public bool IsMonotonic { get; }
85+
86+
public double Value { get; }
8187
}
8288
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public void CollectionStop(string sessionId, DateTime intervalStartTime, DateTim
102102
WriteEvent(3, sessionId, intervalStartTime, intervalEndTime);
103103
}
104104

105-
[Event(4, Keywords = Keywords.TimeSeriesValues)]
105+
[Event(4, Keywords = Keywords.TimeSeriesValues, Version=1)]
106106
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
107107
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
108-
public void CounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate)
108+
public void CounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate, string value)
109109
{
110-
WriteEvent(4, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate);
110+
WriteEvent(4, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate, value);
111111
}
112112

113113
[Event(5, Keywords = Keywords.TimeSeriesValues)]
@@ -191,12 +191,12 @@ public void MultipleSessionsNotSupportedError(string runningSessionId)
191191
WriteEvent(15, runningSessionId);
192192
}
193193

194-
[Event(16, Keywords = Keywords.TimeSeriesValues)]
194+
[Event(16, Keywords = Keywords.TimeSeriesValues, Version=1)]
195195
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
196196
Justification = "This calls WriteEvent with all primitive arguments which is safe. Primitives are always serialized properly.")]
197-
public void UpDownCounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate)
197+
public void UpDownCounterRateValuePublished(string sessionId, string meterName, string? meterVersion, string instrumentName, string? unit, string tags, string rate, string value)
198198
{
199-
WriteEvent(16, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate);
199+
WriteEvent(16, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate, value);
200200
}
201201

202202
/// <summary>
@@ -413,17 +413,19 @@ private void ParseSpecs(string? metricsSpecs)
413413

414414
private static void TransmitMetricValue(Instrument instrument, LabeledAggregationStatistics stats, string sessionId)
415415
{
416-
if (stats.AggregationStatistics is RateStatistics rateStats)
416+
if (stats.AggregationStatistics is CounterStatistics rateStats)
417417
{
418418
if (rateStats.IsMonotonic)
419419
{
420420
Log.CounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels),
421-
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "");
421+
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "",
422+
rateStats.Value.ToString(CultureInfo.InvariantCulture));
422423
}
423424
else
424425
{
425426
Log.UpDownCounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels),
426-
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "");
427+
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "",
428+
rateStats.Value.ToString(CultureInfo.InvariantCulture));
427429
}
428430
}
429431
else if (stats.AggregationStatistics is LastValueStatistics lastValueStats)

0 commit comments

Comments
 (0)