From ba57bf8b2f91332d7b0d57609979d3e5b1d54e83 Mon Sep 17 00:00:00 2001 From: unknown <3676547+alanwest@users.noreply.github.com> Date: Wed, 15 Mar 2023 13:11:20 -0700 Subject: [PATCH 1/2] Fix min/max snapshot for explicit bounds histogram --- src/OpenTelemetry/Metrics/HistogramBuckets.cs | 2 ++ .../Metrics/MetricSnapshotTests.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/OpenTelemetry/Metrics/HistogramBuckets.cs b/src/OpenTelemetry/Metrics/HistogramBuckets.cs index f0cf3938299..fbe17269950 100644 --- a/src/OpenTelemetry/Metrics/HistogramBuckets.cs +++ b/src/OpenTelemetry/Metrics/HistogramBuckets.cs @@ -87,6 +87,8 @@ internal HistogramBuckets Copy() Array.Copy(this.SnapshotBucketCounts, copy.SnapshotBucketCounts, this.SnapshotBucketCounts.Length); copy.SnapshotSum = this.SnapshotSum; + copy.SnapshotMin = this.SnapshotMin; + copy.SnapshotMax = this.SnapshotMax; return copy; } diff --git a/test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs b/test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs index 9ea34769685..81df155b901 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs @@ -117,6 +117,9 @@ public void VerifySnapshot_Histogram() ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current; Assert.Equal(1, metricPoint1.GetHistogramCount()); Assert.Equal(10, metricPoint1.GetHistogramSum()); + metricPoint1.TryGetHistogramMinMaxValues(out var min, out var max); + Assert.Equal(10, min); + Assert.Equal(10, max); // Verify Snapshot 1 Assert.Single(exportedSnapshots); @@ -124,6 +127,9 @@ public void VerifySnapshot_Histogram() Assert.Single(snapshot1.MetricPoints); Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount()); Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum()); + snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max); + Assert.Equal(10, min); + Assert.Equal(10, max); // Verify Metric == Snapshot Assert.Equal(metric1.Name, snapshot1.Name); @@ -141,6 +147,9 @@ public void VerifySnapshot_Histogram() // This value is expected to be updated. Assert.Equal(2, metricPoint1.GetHistogramCount()); Assert.Equal(15, metricPoint1.GetHistogramSum()); + metricPoint1.TryGetHistogramMinMaxValues(out min, out max); + Assert.Equal(5, min); + Assert.Equal(10, max); // Verify Metric 2 Assert.Equal(2, exportedMetrics.Count); @@ -150,11 +159,17 @@ public void VerifySnapshot_Histogram() ref readonly var metricPoint2 = ref metricPoints2Enumerator.Current; Assert.Equal(2, metricPoint2.GetHistogramCount()); Assert.Equal(15, metricPoint2.GetHistogramSum()); + metricPoint2.TryGetHistogramMinMaxValues(out min, out max); + Assert.Equal(5, min); + Assert.Equal(10, max); // Verify Snapshot 1 after second export // This value is expected to be unchanged. Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount()); Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum()); + snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max); + Assert.Equal(10, min); + Assert.Equal(10, max); // Verify Snapshot 2 Assert.Equal(2, exportedSnapshots.Count); @@ -162,6 +177,9 @@ public void VerifySnapshot_Histogram() Assert.Single(snapshot2.MetricPoints); Assert.Equal(2, snapshot2.MetricPoints[0].GetHistogramCount()); Assert.Equal(15, snapshot2.MetricPoints[0].GetHistogramSum()); + snapshot2.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max); + Assert.Equal(5, min); + Assert.Equal(10, max); } } } From daa464346390a467b1ec4d896881371bfe8dec3a Mon Sep 17 00:00:00 2001 From: unknown <3676547+alanwest@users.noreply.github.com> Date: Wed, 15 Mar 2023 13:19:21 -0700 Subject: [PATCH 2/2] Update changelog --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index d8101b31014..4d6a10e2ca4 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Fixed issue where the `MetricSnapshot` of a histogram did not capture the min + and max values. + ([#4306](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4306)) + ## 1.5.0-alpha.1 Released 2023-Mar-07