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) {
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
}
}

50
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())
} else {
_, err = app.AppendHistogram(0, lbls, ts, h.Copy(), nil)
}
if c.expectError {
require.Error(t, err)
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 {
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())
})

Loading…
Cancel
Save