Skip to content

Commit

Permalink
[main-1.3.1] Patch Histogram synchronization fix (#4031)
Browse files Browse the repository at this point in the history
* Patch Histogram synchronization fix
  • Loading branch information
utpilla authored Dec 20, 2022
1 parent c9052ef commit 401a6a5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-packages-1.0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
os: [windows-latest]
branches: [main-1.3.0]
branches: [main-1.3.1]

steps:
- uses: actions/checkout@v3
Expand Down
9 changes: 9 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Unreleased

* Support for metrics was originally released in version 1.3.0. A
synchronization issue with regards to the histogram aggregation was
discovered and fixed in
[#3534](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3534).
This fix has been back-ported to OpenTelemetry .NET 1.3.2. If you
are currently running OpenTelemetry .NET version 1.3.x, it is recommended
that you upgrade to 1.3.2.
([#4031](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4031))

## 1.3.1

Released 2022-Sep-06
Expand Down
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 @@ -339,14 +339,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 @@ -363,13 +365,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 @@ -495,44 +499,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

0 comments on commit 401a6a5

Please sign in to comment.