From 60ad864667aa42f6cf23d6864b49e7700a419757 Mon Sep 17 00:00:00 2001 From: Justin Lei Date: Thu, 9 Mar 2023 11:03:30 -0800 Subject: [PATCH] Remove hacky promql.Test native histogram thing Signed-off-by: Justin Lei --- promql/test.go | 31 ++++++----------------------- storage/remote/read_handler_test.go | 27 +++++++++++++++++-------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/promql/test.go b/promql/test.go index f47269aec..78cc1e9fb 100644 --- a/promql/test.go +++ b/promql/test.go @@ -33,7 +33,6 @@ import ( "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb" - "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/util/teststorage" "github.com/prometheus/prometheus/util/testutil" ) @@ -303,26 +302,14 @@ func (cmd loadCmd) String() string { func (cmd *loadCmd) set(m labels.Labels, vals ...parser.SequenceValue) { h := m.Hash() - metricName := m.Get("__name__") - isHistogram := strings.Contains(metricName, "_histogram_") - samples := make([]Point, 0, len(vals)) ts := testStartTime for _, v := range vals { if !v.Omitted { - t := ts.UnixNano() / int64(time.Millisecond/time.Nanosecond) - - if isHistogram { - samples = append(samples, Point{ - T: t, - H: tsdbutil.GenerateTestFloatHistogram(int(v.Value)), - }) - } else { - samples = append(samples, Point{ - T: t, - V: v.Value, - }) - } + samples = append(samples, Point{ + T: ts.UnixNano() / int64(time.Millisecond/time.Nanosecond), + V: v.Value, + }) } ts = ts.Add(cmd.gap) } @@ -336,14 +323,8 @@ func (cmd *loadCmd) append(a storage.Appender) error { m := cmd.metrics[h] for _, s := range smpls { - if s.H == nil { - if _, err := a.Append(0, m, s.T, s.V); err != nil { - return err - } - } else { - if _, err := a.AppendHistogram(0, m, s.T, nil, s.H); err != nil { - return err - } + if _, err := a.Append(0, m, s.T, s.V); err != nil { + return err } } } diff --git a/storage/remote/read_handler_test.go b/storage/remote/read_handler_test.go index 2ac9cc32b..261c28e21 100644 --- a/storage/remote/read_handler_test.go +++ b/storage/remote/read_handler_test.go @@ -15,11 +15,13 @@ package remote import ( "bytes" + "context" "errors" "io" "net/http" "net/http/httptest" "testing" + "time" "github.com/gogo/protobuf/proto" "github.com/golang/snappy" @@ -37,12 +39,12 @@ func TestSampledReadEndpoint(t *testing.T) { suite, err := promql.NewTest(t, ` load 1m test_metric1{foo="bar",baz="qux"} 1 - test_histogram_metric1{foo="bar",baz="qux"} 1 `) require.NoError(t, err) - defer suite.Close() + addNativeHistogramsToTestSuite(t, suite, 1) + err = suite.Run() require.NoError(t, err) @@ -123,10 +125,9 @@ func TestSampledReadEndpoint(t *testing.T) { {Name: "b", Value: "c"}, {Name: "baz", Value: "qux"}, {Name: "d", Value: "e"}, - {Name: "foo", Value: "bar"}, }, Histograms: []prompb.Histogram{ - FloatHistogramToHistogramProto(0, tsdbutil.GenerateTestFloatHistogram(1)), + FloatHistogramToHistogramProto(0, tsdbutil.GenerateTestFloatHistogram(0)), }, }, }, @@ -139,7 +140,7 @@ func BenchmarkStreamReadEndpoint(b *testing.B) { test_metric1{foo="bar1",baz="qux"} 0+100x119 test_metric1{foo="bar2",baz="qux"} 0+100x120 test_metric1{foo="bar3",baz="qux"} 0+100x240 -`) + `) require.NoError(b, err) defer suite.Close() @@ -210,12 +211,12 @@ func TestStreamReadEndpoint(t *testing.T) { test_metric1{foo="bar1",baz="qux"} 0+100x119 test_metric1{foo="bar2",baz="qux"} 0+100x120 test_metric1{foo="bar3",baz="qux"} 0+100x240 - test_histogram_metric1{foo="bar4",baz="qux"} 0+1x119 `) require.NoError(t, err) - defer suite.Close() + addNativeHistogramsToTestSuite(t, suite, 120) + require.NoError(t, suite.Run()) api := NewReadHandler(nil, nil, suite.Storage(), func() config.Config { @@ -424,7 +425,6 @@ func TestStreamReadEndpoint(t *testing.T) { {Name: "b", Value: "c"}, {Name: "baz", Value: "qux"}, {Name: "d", Value: "e"}, - {Name: "foo", Value: "bar4"}, }, Chunks: []prompb.Chunk{ { @@ -439,3 +439,14 @@ func TestStreamReadEndpoint(t *testing.T) { }, }, results) } + +func addNativeHistogramsToTestSuite(t *testing.T, pqlTest *promql.Test, n int) { + lbls := labels.FromStrings("__name__", "test_histogram_metric1", "baz", "qux") + + app := pqlTest.Storage().Appender(context.TODO()) + for i, fh := range tsdbutil.GenerateTestFloatHistograms(n) { + _, err := app.AppendHistogram(0, lbls, int64(i)*int64(60*time.Second/time.Millisecond), nil, fh) + require.NoError(t, err) + } + require.NoError(t, app.Commit()) +}