Skip to content

Commit

Permalink
Reset IncrementingPollingCounter statistics on update
Browse files Browse the repository at this point in the history
  • Loading branch information
eterekhin committed Jul 28, 2024
1 parent 3471de7 commit d871345
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private void ResetCounters()
}
else if (counter is IncrementingPollingCounter ipCounter)
{
ipCounter.UpdateMetric();
ipCounter.ResetStatistics();
}
else if (counter is EventCounter eCounter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Threading;

namespace System.Diagnostics.Tracing
{
Expand Down Expand Up @@ -43,19 +44,37 @@ public IncrementingPollingCounter(string name, EventSource eventSource, Func<dou
private double _increment;
private double _prevIncrement;
private readonly Func<double> _totalValueProvider;
private bool _resetStatistics;

internal void ResetStatistics()
{
_resetStatistics = true;
}

/// <summary>
/// Calls "_totalValueProvider" to enqueue the counter value to the queue.
/// </summary>
internal void UpdateMetric()
private void UpdateMetric()
{
try
lock (this)
{
lock (this)
if (_resetStatistics)
{
_prevIncrement = _increment;
_increment = _totalValueProvider();
UpdateMetricInternal();
_resetStatistics = false;
}

UpdateMetricInternal();
}
}

private void UpdateMetricInternal()
{
Debug.Assert(Monitor.IsEntered(this));
try
{
_prevIncrement = _increment;
_increment = _totalValueProvider();
}
catch (Exception ex)
{
Expand Down

0 comments on commit d871345

Please sign in to comment.