Skip to content

Commit

Permalink
Publish UpDownCounter and ObservableUpDownCounter Events for Syst…
Browse files Browse the repository at this point in the history
…em.Diagnostics.Metrics (#81041)

Add support for publishing UpDownCounter (and Observable...) - also added in the missing testing for this.
  • Loading branch information
kkeirstead committed Jan 29, 2023
1 parent 84a7b8e commit dfe1076
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 21 deletions.
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() : null;
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: true) : null;
}
};
}
Expand All @@ -285,7 +285,7 @@ private void RemoveInstrumentState(Instrument instrument)
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateAggregator() : null;
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: true) : null;
}
};
}
Expand All @@ -312,6 +312,26 @@ private void RemoveInstrumentState(Instrument instrument)
}
};
}
else if (genericDefType == typeof(UpDownCounter<>))
{
return () =>
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateSumAggregator(isMonotonic: false) : null;
}
};
}
else if (genericDefType == typeof(ObservableUpDownCounter<>))
{
return () =>
{
lock (this)
{
return CheckTimeSeriesAllowed() ? new RateAggregator(isMonotonic: false) : null;
}
};
}
else
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ public void MultipleSessionsNotSupportedError(string runningSessionId)
WriteEvent(15, runningSessionId);
}

[Event(16, Keywords = Keywords.TimeSeriesValues)]
[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)
{
WriteEvent(16, sessionId, meterName, meterVersion ?? "", instrumentName, unit ?? "", tags, rate);
}

/// <summary>
/// Called when the EventSource gets a command from a EventListener or ETW.
/// </summary>
Expand Down Expand Up @@ -407,8 +415,16 @@ private static void TransmitMetricValue(Instrument instrument, LabeledAggregatio
{
if (stats.AggregationStatistics is RateStatistics rateStats)
{
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) : "");
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) : "");
}
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) : "");
}
}
else if (stats.AggregationStatistics is LastValueStatistics lastValueStats)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace System.Diagnostics.Metrics
internal sealed class RateSumAggregator : Aggregator
{
private double _sum;
private bool _isMonotonic;

public RateSumAggregator(bool isMonotonic)
{
_isMonotonic = isMonotonic;
}

public override void Update(double value)
{
Expand All @@ -19,7 +25,7 @@ public override IAggregationStatistics Collect()
{
lock (this)
{
RateStatistics? stats = new RateStatistics(_sum);
RateStatistics? stats = new RateStatistics(_sum, _isMonotonic);
_sum = 0;
return stats;
}
Expand All @@ -30,6 +36,12 @@ internal sealed class RateAggregator : Aggregator
{
private double? _prevValue;
private double _value;
private bool _isMonotonic;

public RateAggregator(bool isMonotonic)
{
_isMonotonic = isMonotonic;
}

public override void Update(double value)
{
Expand All @@ -48,7 +60,7 @@ public override IAggregationStatistics Collect()
{
delta = _value - _prevValue.Value;
}
RateStatistics stats = new RateStatistics(delta);
RateStatistics stats = new RateStatistics(delta, _isMonotonic);
_prevValue = _value;
return stats;
}
Expand All @@ -57,11 +69,14 @@ public override IAggregationStatistics Collect()

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

public double? Delta { get; }

public bool IsMonotonic { get; }
}
}
Loading

0 comments on commit dfe1076

Please sign in to comment.