diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index a11f9108e92..8ac2f28309d 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -6,6 +6,10 @@ Notes](../../RELEASENOTES.md). ## Unreleased +* Fixed an issue where the Base2 Exponential Bucket Histogram did not reset its + scale to 20 after each collection cycle when using delta aggregation temporality. + ([#6557](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6557)) + ## 1.13.0 Released 2025-Oct-01 diff --git a/src/OpenTelemetry/Metrics/MetricPoint/Base2ExponentialBucketHistogram.cs b/src/OpenTelemetry/Metrics/MetricPoint/Base2ExponentialBucketHistogram.cs index 1d63d898443..9e744f2dd8c 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint/Base2ExponentialBucketHistogram.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint/Base2ExponentialBucketHistogram.cs @@ -197,10 +197,16 @@ public void Record(double value) Debug.Assert(n == 0, "Increment should always succeed after scale down."); } - internal void Reset() + internal void Reset(bool isMinMax) { - // TODO: Determine if this is sufficient for delta temporality. - // I'm not sure we should be resetting the scale. + if (isMinMax) + { + this.RunningMin = double.PositiveInfinity; + this.RunningMax = double.NegativeInfinity; + } + + this.Scale = Metric.DefaultExponentialHistogramMaxScale; + this.RunningSum = 0; this.ZeroCount = 0; this.PositiveBuckets.Reset(); this.NegativeBuckets.Reset(); diff --git a/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs index 8c0ad5951fc..75821b5454e 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs @@ -820,8 +820,7 @@ internal void TakeSnapshot(bool outputDelta) if (outputDelta) { this.runningValue.AsLong = 0; - histogram.RunningSum = 0; - histogram.Reset(); + histogram.Reset(isMinMax: false); } this.MetricPointStatus = MetricPointStatus.NoCollectPending; @@ -848,10 +847,7 @@ internal void TakeSnapshot(bool outputDelta) if (outputDelta) { this.runningValue.AsLong = 0; - histogram.RunningSum = 0; - histogram.Reset(); - histogram.RunningMin = double.PositiveInfinity; - histogram.RunningMax = double.NegativeInfinity; + histogram.Reset(isMinMax: true); } this.MetricPointStatus = MetricPointStatus.NoCollectPending; diff --git a/test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs b/test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs index 19bf1097f4b..76a906c602d 100644 --- a/test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs @@ -352,10 +352,12 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega var count = metricPoint.GetHistogramCount(); var sum = metricPoint.GetHistogramSum(); var hasMinMax = metricPoint.TryGetHistogramMinMaxValues(out var min, out var max); + var firstScale = metricPoint.GetExponentialHistogramData().Scale; AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData()); Assert.Equal(50, sum); Assert.Equal(6, count); + Assert.True(firstScale <= Metric.DefaultExponentialHistogramMaxScale, $"The first scale value, {firstScale}, is greater than Metric.DefaultExponentialHistogramMaxScale."); if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax) { @@ -373,12 +375,14 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega count = metricPoint.GetHistogramCount(); sum = metricPoint.GetHistogramSum(); hasMinMax = metricPoint.TryGetHistogramMinMaxValues(out min, out max); + var secondScale = metricPoint.GetExponentialHistogramData().Scale; if (aggregationTemporality == AggregationTemporality.Cumulative) { AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData()); Assert.Equal(50, sum); Assert.Equal(6, count); + Assert.Equal(firstScale, secondScale); if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax) { @@ -393,10 +397,11 @@ internal void ExponentialHistogramTests(AggregationType aggregationType, Aggrega } else { - expectedHistogram.Reset(); + expectedHistogram.Reset(isMinMax: aggregationType == AggregationType.HistogramWithMinMax); AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData()); Assert.Equal(0, sum); Assert.Equal(0, count); + Assert.Equal(Metric.DefaultExponentialHistogramMaxScale, secondScale); if (aggregationType == AggregationType.Base2ExponentialHistogramWithMinMax) {