Skip to content

Commit

Permalink
Publish Aggregated Values For Counter and UpDownCounter For Syste…
Browse files Browse the repository at this point in the history
…m.Diagnostics.Metrics (#84846)
  • Loading branch information
kkeirstead authored Apr 18, 2023
1 parent 634cc1e commit e210d90
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="System\Diagnostics\Metrics\ObservableGauge.cs" />
<Compile Include="System\Diagnostics\Metrics\ObservableInstrument.cs" />
<Compile Include="System\Diagnostics\Metrics\ObservableUpDownCounter.cs" />
<Compile Include="System\Diagnostics\Metrics\RateAggregator.cs" />
<Compile Include="System\Diagnostics\Metrics\CounterAggregator.cs" />
<Compile Include="System\Diagnostics\Metrics\StringSequence.cs" />
<Compile Include="System\Diagnostics\Metrics\TagList.cs" />
<Compile Include="System\Diagnostics\Metrics\UpDownCounter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void RemoveInstrumentState(Instrument instrument)
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: true) : null;
return CheckTimeSeriesAllowed() ? new CounterAggregator(isMonotonic: true) : null;
}
};
}
Expand All @@ -285,7 +285,7 @@ private void RemoveInstrumentState(Instrument instrument)
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: true) : null;
return CheckTimeSeriesAllowed() ? new ObservableCounterAggregator(isMonotonic: true) : null;
}
};
}
Expand Down Expand Up @@ -318,7 +318,7 @@ private void RemoveInstrumentState(Instrument instrument)
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: false) : null;
return CheckTimeSeriesAllowed() ? new CounterAggregator(isMonotonic: false) : null;
}
};
}
Expand All @@ -328,7 +328,7 @@ private void RemoveInstrumentState(Instrument instrument)
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: false) : null;
return CheckTimeSeriesAllowed() ? new ObservableCounterAggregator(isMonotonic: false) : null;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

namespace System.Diagnostics.Metrics
{
internal sealed class RateSumAggregator : Aggregator
internal sealed class CounterAggregator : Aggregator
{
private readonly bool _isMonotonic;
private double _sum;
private double _delta;
private double _aggregatedValue;

public RateSumAggregator(bool isMonotonic)
public CounterAggregator(bool isMonotonic)
{
_isMonotonic = isMonotonic;
}
Expand All @@ -17,28 +18,29 @@ public override void Update(double value)
{
lock (this)
{
_sum += value;
_delta += value;
}
}

public override IAggregationStatistics Collect()
{
lock (this)
{
RateStatistics? stats = new RateStatistics(_sum, _isMonotonic);
_sum = 0;
_aggregatedValue += _delta;
CounterStatistics? stats = new CounterStatistics(_delta, _isMonotonic, _aggregatedValue);
_delta = 0;
return stats;
}
}
}

internal sealed class RateAggregator : Aggregator
internal sealed class ObservableCounterAggregator : Aggregator
{
private readonly bool _isMonotonic;
private double? _prevValue;
private double _value;
private double _currValue;

public RateAggregator(bool isMonotonic)
public ObservableCounterAggregator(bool isMonotonic)
{
_isMonotonic = isMonotonic;
}
Expand All @@ -47,7 +49,7 @@ public override void Update(double value)
{
lock (this)
{
_value = value;
_currValue = value;
}
}

Expand All @@ -58,25 +60,29 @@ public override IAggregationStatistics Collect()
double? delta = null;
if (_prevValue.HasValue)
{
delta = _value - _prevValue.Value;
delta = _currValue - _prevValue.Value;
}
RateStatistics stats = new RateStatistics(delta, _isMonotonic);
_prevValue = _value;

CounterStatistics stats = new CounterStatistics(delta, _isMonotonic, _currValue);
_prevValue = _currValue;
return stats;
}
}
}

internal sealed class RateStatistics : IAggregationStatistics
internal sealed class CounterStatistics : IAggregationStatistics
{
public RateStatistics(double? delta, bool isMonotonic)
public CounterStatistics(double? delta, bool isMonotonic, double value)
{
Delta = delta;
IsMonotonic = isMonotonic;
Value = value;
}

public double? Delta { get; }

public bool IsMonotonic { get; }

public double Value { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public void CollectionStop(string sessionId, DateTime intervalStartTime, DateTim
WriteEvent(3, sessionId, intervalStartTime, intervalEndTime);
}

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

[Event(5, Keywords = Keywords.TimeSeriesValues)]
Expand Down Expand Up @@ -191,12 +191,12 @@ public void MultipleSessionsNotSupportedError(string runningSessionId)
WriteEvent(15, runningSessionId);
}

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

/// <summary>
Expand Down Expand Up @@ -413,17 +413,19 @@ private void ParseSpecs(string? metricsSpecs)

private static void TransmitMetricValue(Instrument instrument, LabeledAggregationStatistics stats, string sessionId)
{
if (stats.AggregationStatistics is RateStatistics rateStats)
if (stats.AggregationStatistics is CounterStatistics rateStats)
{
if (rateStats.IsMonotonic)
{
Log.CounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels),
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "");
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "",
rateStats.Value.ToString(CultureInfo.InvariantCulture));
}
else
{
Log.UpDownCounterRateValuePublished(sessionId, instrument.Meter.Name, instrument.Meter.Version, instrument.Name, instrument.Unit, FormatTags(stats.Labels),
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "");
rateStats.Delta.HasValue ? rateStats.Delta.Value.ToString(CultureInfo.InvariantCulture) : "",
rateStats.Value.ToString(CultureInfo.InvariantCulture));
}
}
else if (stats.AggregationStatistics is LastValueStatistics lastValueStats)
Expand Down
Loading

0 comments on commit e210d90

Please sign in to comment.