Skip to content

Commit

Permalink
Fix min/max snapshot for explicit bounds histogram (#4306)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Mar 16, 2023
1 parent 3d6656f commit d924663
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,19 @@ 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);
var snapshot1 = exportedSnapshots[0];
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);
Expand All @@ -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);
Expand All @@ -150,18 +159,27 @@ 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);
var snapshot2 = exportedSnapshots[1];
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);
}

[Fact]
Expand Down

0 comments on commit d924663

Please sign in to comment.