From 10ebeb0a629e7c4880347be18ae4e2d1850bdf2b Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Thu, 16 Nov 2023 13:00:11 +0800 Subject: [PATCH] rename lastSum -> lastCount && enrich test case with expectBucketCount and expectSchema Signed-off-by: Ziqi Zhao --- scrape/target.go | 8 +++---- scrape/target_test.go | 50 ++++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/scrape/target.go b/scrape/target.go index ed4c598b7..72b91a41e 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -367,18 +367,18 @@ type bucketLimitAppender struct { func (app *bucketLimitAppender) AppendHistogram(ref storage.SeriesRef, lset labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) (storage.SeriesRef, error) { if h != nil { for len(h.PositiveBuckets)+len(h.NegativeBuckets) > app.limit { - lastSum := len(h.PositiveBuckets) + len(h.NegativeBuckets) + lastCount := len(h.PositiveBuckets) + len(h.NegativeBuckets) h = h.ReduceResolution(h.Schema - 1) - if len(h.PositiveBuckets)+len(h.NegativeBuckets) == lastSum { + if len(h.PositiveBuckets)+len(h.NegativeBuckets) == lastCount { return 0, errBucketLimit } } } if fh != nil { for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > app.limit { - lastSum := len(fh.PositiveBuckets) + len(fh.NegativeBuckets) + lastCount := len(fh.PositiveBuckets) + len(fh.NegativeBuckets) fh = fh.ReduceResolution(fh.Schema - 1) - if len(fh.PositiveBuckets)+len(fh.NegativeBuckets) == lastSum { + if len(fh.PositiveBuckets)+len(fh.NegativeBuckets) == lastCount { return 0, errBucketLimit } } diff --git a/scrape/target_test.go b/scrape/target_test.go index f676b85c0..604cfa995 100644 --- a/scrape/target_test.go +++ b/scrape/target_test.go @@ -508,9 +508,11 @@ func TestBucketLimitAppender(t *testing.T) { } cases := []struct { - h histogram.Histogram - limit int - expectError bool + h histogram.Histogram + limit int + expectError bool + expectBucketCount int + expectSchema int32 }{ { h: example, @@ -518,14 +520,18 @@ func TestBucketLimitAppender(t *testing.T) { expectError: true, }, { - h: example, - limit: 4, - expectError: false, + h: example, + limit: 4, + expectError: false, + expectBucketCount: 4, + expectSchema: -1, }, { - h: example, - limit: 10, - expectError: false, + h: example, + limit: 10, + expectError: false, + expectBucketCount: 6, + expectSchema: 0, }, } @@ -536,18 +542,28 @@ func TestBucketLimitAppender(t *testing.T) { t.Run(fmt.Sprintf("floatHistogram=%t", floatHisto), func(t *testing.T) { app := &bucketLimitAppender{Appender: resApp, limit: c.limit} ts := int64(10 * time.Minute / time.Millisecond) - h := c.h lbls := labels.FromStrings("__name__", "sparse_histogram_series") var err error if floatHisto { - _, err = app.AppendHistogram(0, lbls, ts, nil, h.Copy().ToFloat()) + fh := c.h.Copy().ToFloat() + _, err = app.AppendHistogram(0, lbls, ts, nil, fh) + if c.expectError { + require.Error(t, err) + } else { + require.Equal(t, c.expectSchema, fh.Schema) + require.Equal(t, c.expectBucketCount, len(fh.NegativeBuckets)+len(fh.PositiveBuckets)) + require.NoError(t, err) + } } else { - _, err = app.AppendHistogram(0, lbls, ts, h.Copy(), nil) - } - if c.expectError { - require.Error(t, err) - } else { - require.NoError(t, err) + h := c.h.Copy() + _, err = app.AppendHistogram(0, lbls, ts, h, nil) + if c.expectError { + require.Error(t, err) + } else { + require.Equal(t, c.expectSchema, h.Schema) + require.Equal(t, c.expectBucketCount, len(h.NegativeBuckets)+len(h.PositiveBuckets)) + require.NoError(t, err) + } } require.NoError(t, app.Commit()) })