Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Histogram synchronization issue #3534

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ internal HistogramBuckets(double[] explicitBounds)
this.SnapshotBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : new long[0];
}

internal object LockObject => this.SnapshotBucketCounts;

public Enumerator GetEnumerator() => new(this);

internal HistogramBuckets Copy()
Expand Down
70 changes: 48 additions & 22 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,16 @@ internal void Update(double number)
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
// Lock acquired
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

Expand All @@ -365,13 +367,15 @@ internal void Update(double number)
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
// Lock acquired
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

Expand Down Expand Up @@ -497,44 +501,66 @@ internal void TakeSnapshot(bool outputDelta)

case AggregationType.Histogram:
{
lock (this.histogramBuckets.LockObject)
var sw = default(SpinWait);
while (true)
{
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

for (int i = 0; i < this.histogramBuckets.RunningBucketCounts.Length; i++)
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
this.histogramBuckets.SnapshotBucketCounts[i] = this.histogramBuckets.RunningBucketCounts[i];
// Lock acquired
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.histogramBuckets.RunningBucketCounts[i] = 0;
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

for (int i = 0; i < this.histogramBuckets.RunningBucketCounts.Length; i++)
{
this.histogramBuckets.SnapshotBucketCounts[i] = this.histogramBuckets.RunningBucketCounts[i];
if (outputDelta)
{
this.histogramBuckets.RunningBucketCounts[i] = 0;
}
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
sw.SpinOnce();
}

break;
}

case AggregationType.HistogramSumCount:
{
lock (this.histogramBuckets.LockObject)
var sw = default(SpinWait);
while (true)
{
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
// Lock acquired
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
sw.SpinOnce();
}

break;
Expand Down