Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 4 additions & 8 deletions src/OpenTelemetry/Metrics/ExponentialHistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace OpenTelemetry.Metrics;

public sealed class ExponentialHistogramBuckets
{
private int size;
private long[] buckets = Array.Empty<long>();

internal ExponentialHistogramBuckets()
Expand All @@ -29,7 +28,7 @@ internal ExponentialHistogramBuckets()

public int Offset { get; private set; }

public Enumerator GetEnumerator() => new(this.size, this.buckets);
public Enumerator GetEnumerator() => new(this.buckets);

internal void SnapshotBuckets(CircularBufferBuckets buckets)
{
Expand All @@ -39,7 +38,6 @@ internal void SnapshotBuckets(CircularBufferBuckets buckets)
}

this.Offset = buckets.Offset;
this.size = buckets.Size;
buckets.Copy(this.buckets);
}

Expand All @@ -58,14 +56,12 @@ internal ExponentialHistogramBuckets Copy()
// Note: Does not implement IEnumerator<> to prevent accidental boxing.
public struct Enumerator
{
private readonly int size;
private readonly long[] buckets;
private int index;

internal Enumerator(int size, long[] buckets)
internal Enumerator(long[] buckets)
{
this.index = size;
this.size = size;
this.index = 0;
this.buckets = buckets;
this.Current = default;
}
Expand All @@ -85,7 +81,7 @@ internal Enumerator(int size, long[] buckets)
/// collection.</returns>
public bool MoveNext()
{
if (this.index < this.size)
if (this.index < this.buckets.Length)
Copy link
Contributor

@utpilla utpilla Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be size? We are now always going enumerate over the full length of the CircularBufferBuckets instead of just enumerating from CircularBufferBuckets.begin to CircularBufferBuckets.end.

{
this.Current = this.buckets[this.index++];
return true;
Expand Down
27 changes: 23 additions & 4 deletions test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,36 @@ internal static void AssertExponentialBucketsAreCorrect(Base2ExponentialBucketHi
Assert.Equal(expectedHistogram.PositiveBuckets.Offset, data.PositiveBuckets.Offset);
Assert.Equal(expectedHistogram.NegativeBuckets.Offset, data.NegativeBuckets.Offset);

var index = expectedHistogram.PositiveBuckets.Offset;
expectedHistogram.Snapshot();
var expectedData = expectedHistogram.GetExponentialHistogramData();

var actual = new List<long>();
foreach (var bucketCount in data.PositiveBuckets)
{
Assert.Equal(expectedHistogram.PositiveBuckets[index++], bucketCount);
actual.Add(bucketCount);
}

index = expectedHistogram.NegativeBuckets.Offset;
var expected = new List<long>();
foreach (var bucketCount in expectedData.PositiveBuckets)
{
expected.Add(bucketCount);
}

Assert.Equal(expected, actual);

actual = new List<long>();
foreach (var bucketCount in data.NegativeBuckets)
{
Assert.Equal(expectedHistogram.PositiveBuckets[index++], bucketCount);
actual.Add(bucketCount);
}

expected = new List<long>();
foreach (var bucketCount in expectedData.NegativeBuckets)
{
expected.Add(bucketCount);
}

Assert.Equal(expected, actual);
}

[Theory]
Expand Down