From cbd01fc296a48f0512d8c071b70c24dac5e21c0d Mon Sep 17 00:00:00 2001 From: Linas Medziunas Date: Mon, 25 Sep 2023 16:03:55 +0300 Subject: [PATCH] Fix NaN sum check in [Float]Histogram.Equals method Signed-off-by: Linas Medziunas --- model/histogram/float_histogram.go | 3 ++- model/histogram/histogram.go | 3 ++- model/histogram/histogram_test.go | 12 ++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index 41873278c..6272b6683 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -313,7 +313,8 @@ func (h *FloatHistogram) Equals(h2 *FloatHistogram) bool { } if h.Schema != h2.Schema || h.ZeroThreshold != h2.ZeroThreshold || - h.ZeroCount != h2.ZeroCount || h.Count != h2.Count || h.Sum != h2.Sum { + h.ZeroCount != h2.ZeroCount || h.Count != h2.Count || + math.Float64bits(h.Sum) != math.Float64bits(h2.Sum) { return false } diff --git a/model/histogram/histogram.go b/model/histogram/histogram.go index 6d425307c..31c3ce9fe 100644 --- a/model/histogram/histogram.go +++ b/model/histogram/histogram.go @@ -178,7 +178,8 @@ func (h *Histogram) Equals(h2 *Histogram) bool { } if h.Schema != h2.Schema || h.ZeroThreshold != h2.ZeroThreshold || - h.ZeroCount != h2.ZeroCount || h.Count != h2.Count || h.Sum != h2.Sum { + h.ZeroCount != h2.ZeroCount || h.Count != h2.Count || + math.Float64bits(h.Sum) != math.Float64bits(h2.Sum) { return false } diff --git a/model/histogram/histogram_test.go b/model/histogram/histogram_test.go index 3975353d2..2af90501e 100644 --- a/model/histogram/histogram_test.go +++ b/model/histogram/histogram_test.go @@ -19,6 +19,8 @@ import ( "testing" "github.com/stretchr/testify/require" + + "github.com/prometheus/prometheus/model/value" ) func TestHistogramString(t *testing.T) { @@ -411,8 +413,8 @@ func TestHistogramToFloat(t *testing.T) { require.Equal(t, h.String(), fh.String()) } -// TestHistogramMatches tests both Histogram and FloatHistogram. -func TestHistogramMatches(t *testing.T) { +// TestHistogramEquals tests both Histogram and FloatHistogram. +func TestHistogramEquals(t *testing.T) { h1 := Histogram{ Schema: 3, Count: 61, @@ -537,6 +539,12 @@ func TestHistogramMatches(t *testing.T) { }) h2.NegativeBuckets = append(h2.NegativeBuckets, 1) notEquals(h1, *h2) + + // StaleNaN. + h2 = h1.Copy() + h2.Sum = math.Float64frombits(value.StaleNaN) + notEquals(h1, *h2) + equals(*h2, *h2) } func TestHistogramCompact(t *testing.T) {