Skip to content

Commit ac8d45e

Browse files
Reset scale of exponential histogram (#6557)
Co-authored-by: Rajkumar Rangaraj <[email protected]>
1 parent 8b3d8fa commit ac8d45e

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/OpenTelemetry/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Notes](../../RELEASENOTES.md).
66

77
## Unreleased
88

9+
* Fixed an issue where the Base2 Exponential Bucket Histogram did not reset its
10+
scale to 20 after each collection cycle when using delta aggregation temporality.
11+
([#6557](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6557))
12+
913
## 1.13.0
1014

1115
Released 2025-Oct-01

src/OpenTelemetry/Metrics/MetricPoint/Base2ExponentialBucketHistogram.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,16 @@ public void Record(double value)
197197
Debug.Assert(n == 0, "Increment should always succeed after scale down.");
198198
}
199199

200-
internal void Reset()
200+
internal void Reset(bool isMinMax)
201201
{
202-
// TODO: Determine if this is sufficient for delta temporality.
203-
// I'm not sure we should be resetting the scale.
202+
if (isMinMax)
203+
{
204+
this.RunningMin = double.PositiveInfinity;
205+
this.RunningMax = double.NegativeInfinity;
206+
}
207+
208+
this.Scale = Metric.DefaultExponentialHistogramMaxScale;
209+
this.RunningSum = 0;
204210
this.ZeroCount = 0;
205211
this.PositiveBuckets.Reset();
206212
this.NegativeBuckets.Reset();

src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,7 @@ internal void TakeSnapshot(bool outputDelta)
820820
if (outputDelta)
821821
{
822822
this.runningValue.AsLong = 0;
823-
histogram.RunningSum = 0;
824-
histogram.Reset();
823+
histogram.Reset(isMinMax: false);
825824
}
826825

827826
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@@ -848,10 +847,7 @@ internal void TakeSnapshot(bool outputDelta)
848847
if (outputDelta)
849848
{
850849
this.runningValue.AsLong = 0;
851-
histogram.RunningSum = 0;
852-
histogram.Reset();
853-
histogram.RunningMin = double.PositiveInfinity;
854-
histogram.RunningMax = double.NegativeInfinity;
850+
histogram.Reset(isMinMax: true);
855851
}
856852

857853
this.MetricPointStatus = MetricPointStatus.NoCollectPending;

test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,12 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega
352352
var count = metricPoint.GetHistogramCount();
353353
var sum = metricPoint.GetHistogramSum();
354354
var hasMinMax = metricPoint.TryGetHistogramMinMaxValues(out var min, out var max);
355+
var firstScale = metricPoint.GetExponentialHistogramData().Scale;
355356

356357
AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData());
357358
Assert.Equal(50, sum);
358359
Assert.Equal(6, count);
360+
Assert.True(firstScale <= Metric.DefaultExponentialHistogramMaxScale, $"The first scale value, {firstScale}, is greater than Metric.DefaultExponentialHistogramMaxScale.");
359361

360362
if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax)
361363
{
@@ -373,12 +375,14 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega
373375
count = metricPoint.GetHistogramCount();
374376
sum = metricPoint.GetHistogramSum();
375377
hasMinMax = metricPoint.TryGetHistogramMinMaxValues(out min, out max);
378+
var secondScale = metricPoint.GetExponentialHistogramData().Scale;
376379

377380
if (aggregationTemporality == AggregationTemporality.Cumulative)
378381
{
379382
AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData());
380383
Assert.Equal(50, sum);
381384
Assert.Equal(6, count);
385+
Assert.Equal(firstScale, secondScale);
382386

383387
if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax)
384388
{
@@ -393,10 +397,11 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega
393397
}
394398
else
395399
{
396-
expectedHistogram.Reset();
400+
expectedHistogram.Reset(isMinMax: aggregationType == AggregationType.HistogramWithMinMax);
397401
AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData());
398402
Assert.Equal(0, sum);
399403
Assert.Equal(0, count);
404+
Assert.Equal(Metric.DefaultExponentialHistogramMaxScale, secondScale);
400405

401406
if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax)
402407
{

0 commit comments

Comments
 (0)