Skip to content
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
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Notes](../../RELEASENOTES.md).

## Unreleased

* Fixed an exception when downscaling empty exponential histogram buckets after
a reset.
([#8064](https://github.com/open-telemetry/opentelemetry-dotnet/pull/8064))

## 1.21.0-rc.1

Released 2026-Oct-28
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/CircularBufferBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void ScaleDown(int level = 1)
{
Debug.Assert(level > 0, "The scale down level must be a positive integer.");

if (this.trait == null)
if (this.trait == null || this.end < this.Offset)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,40 @@ public void ZeroHandling()
Assert.Equal(2, histogram.ZeroCount);
}

[Theory]
[InlineData(-1, 4, -1)]
[InlineData(1, 4, -1)]
[InlineData(-1, 16, -2)]
[InlineData(1, 16, -2)]
public void RecordAfterResetScalesEmptyOppositeSignBuckets(int sign, double value, int expectedScale)
{
var histogram = new Base2ExponentialBucketHistogram(maxBuckets: 2, scale: 0);
histogram.Record(-sign);
histogram.Reset(isMinMax: false);

histogram.Record(sign);
histogram.Record(sign * value);

var recordedBuckets = sign > 0 ? histogram.PositiveBuckets : histogram.NegativeBuckets;
var emptyBuckets = sign > 0 ? histogram.NegativeBuckets : histogram.PositiveBuckets;

Assert.Equal(expectedScale, histogram.Scale);
Assert.Equal(-1, recordedBuckets.Offset);
Assert.Equal(2, recordedBuckets.Size);
Assert.Equal(1, recordedBuckets[-1]);
Assert.Equal(1, recordedBuckets[0]);
Assert.Equal(0, emptyBuckets.Offset);
Assert.Equal(0, emptyBuckets.Size);

histogram.Record(-sign);

Assert.Equal(-1, emptyBuckets.Offset);
Assert.Equal(1, emptyBuckets.Size);
Assert.Equal(1, emptyBuckets[-1]);
Assert.Equal(1, recordedBuckets[-1]);
Assert.Equal(1, recordedBuckets[0]);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
Expand Down
38 changes: 38 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/CircularBufferBucketsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,44 @@ public void ScaleDownCapacity2()
Assert.Equal(1, buckets[buckets.Offset + 1]);
}

[Theory]
[InlineData(2, false, 1)]
[InlineData(2, false, 3)]
[InlineData(2, true, 1)]
[InlineData(2, true, 3)]
[InlineData(3, false, 1)]
[InlineData(3, false, 3)]
[InlineData(3, true, 1)]
[InlineData(3, true, 3)]
public void ScaleDownEmptyBuckets(int capacity, bool reset, int level)
{
var buckets = new CircularBufferBuckets(capacity);

if (reset)
{
Assert.Equal(0, buckets.TryIncrement(1, 2));
Assert.Equal(0, buckets.TryIncrement(0, 3));
buckets.Reset();
}

buckets.ScaleDown(level);

Assert.Equal(0, buckets.Offset);
Assert.Equal(0, buckets.Size);

var copy = new long[capacity];
buckets.Copy(copy);
Assert.Equal(new long[capacity], copy);

Assert.Equal(0, buckets.TryIncrement(-4, 7));
Assert.Equal(0, buckets.TryIncrement(-3, 5));
buckets.ScaleDown();

Assert.Equal(-2, buckets.Offset);
Assert.Equal(1, buckets.Size);
Assert.Equal(12, buckets[-2]);
}

[Fact]
public void ScaleDownCapacity3()
{
Expand Down
Loading