Browse Source

rename lastSum -> lastCount && enrich test case with expectBucketCount and expectSchema

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
pull/13129/head
Ziqi Zhao 1 year ago
parent
commit
10ebeb0a62
  1. 8
      scrape/target.go
  2. 50
      scrape/target_test.go

8
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) { func (app *bucketLimitAppender) AppendHistogram(ref storage.SeriesRef, lset labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) (storage.SeriesRef, error) {
if h != nil { if h != nil {
for len(h.PositiveBuckets)+len(h.NegativeBuckets) > app.limit { 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) 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 return 0, errBucketLimit
} }
} }
} }
if fh != nil { if fh != nil {
for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > app.limit { 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) 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 return 0, errBucketLimit
} }
} }

50
scrape/target_test.go

@ -508,9 +508,11 @@ func TestBucketLimitAppender(t *testing.T) {
} }
cases := []struct { cases := []struct {
h histogram.Histogram h histogram.Histogram
limit int limit int
expectError bool expectError bool
expectBucketCount int
expectSchema int32
}{ }{
{ {
h: example, h: example,
@ -518,14 +520,18 @@ func TestBucketLimitAppender(t *testing.T) {
expectError: true, expectError: true,
}, },
{ {
h: example, h: example,
limit: 4, limit: 4,
expectError: false, expectError: false,
expectBucketCount: 4,
expectSchema: -1,
}, },
{ {
h: example, h: example,
limit: 10, limit: 10,
expectError: false, 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) { t.Run(fmt.Sprintf("floatHistogram=%t", floatHisto), func(t *testing.T) {
app := &bucketLimitAppender{Appender: resApp, limit: c.limit} app := &bucketLimitAppender{Appender: resApp, limit: c.limit}
ts := int64(10 * time.Minute / time.Millisecond) ts := int64(10 * time.Minute / time.Millisecond)
h := c.h
lbls := labels.FromStrings("__name__", "sparse_histogram_series") lbls := labels.FromStrings("__name__", "sparse_histogram_series")
var err error var err error
if floatHisto { if floatHisto {
_, err = app.AppendHistogram(0, lbls, ts, nil, h.Copy().ToFloat()) fh := c.h.Copy().ToFloat()
} else { _, err = app.AppendHistogram(0, lbls, ts, nil, fh)
_, err = app.AppendHistogram(0, lbls, ts, h.Copy(), nil) if c.expectError {
} require.Error(t, err)
if c.expectError { } else {
require.Error(t, err) require.Equal(t, c.expectSchema, fh.Schema)
require.Equal(t, c.expectBucketCount, len(fh.NegativeBuckets)+len(fh.PositiveBuckets))
require.NoError(t, err)
}
} else { } 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()) require.NoError(t, app.Commit())
}) })

Loading…
Cancel
Save