From 8fe9250f7d236bcb5a2b21e7357abad2e5dcb31a Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Tue, 21 Nov 2023 16:56:56 +0800 Subject: [PATCH] optimize the logic of break the loop of reducing resolution Signed-off-by: Ziqi Zhao --- scrape/scrape_test.go | 23 +++++++++++++++++++++++ scrape/target.go | 10 ++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index a2e0d00c6..93e163101 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -1866,6 +1866,29 @@ func TestScrapeLoop_HistogramBucketLimit(t *testing.T) { msg, err = MetricFamilyToProtobuf(histogramMetricFamily) require.NoError(t, err) + now = time.Now() + total, added, seriesAdded, err = sl.append(app, msg, "application/vnd.google.protobuf", now) + require.NoError(t, err) + require.Equal(t, 3, total) + require.Equal(t, 3, added) + require.Equal(t, 3, seriesAdded) + + err = sl.metrics.targetScrapeNativeHistogramBucketLimit.Write(&metric) + require.NoError(t, err) + metricValue = metric.GetCounter().GetValue() + require.Equal(t, beforeMetricValue, metricValue) + beforeMetricValue = metricValue + + nativeHistogram.WithLabelValues("L").Observe(100000.0) // in different bucket since > 10*1.1 + + gathered, err = registry.Gather() + require.NoError(t, err) + require.NotEmpty(t, gathered) + + histogramMetricFamily = gathered[0] + msg, err = MetricFamilyToProtobuf(histogramMetricFamily) + require.NoError(t, err) + now = time.Now() total, added, seriesAdded, err = sl.append(app, msg, "application/vnd.google.protobuf", now) if !errors.Is(err, errBucketLimit) { diff --git a/scrape/target.go b/scrape/target.go index 72b91a41e..62f662693 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -367,20 +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 { - lastCount := len(h.PositiveBuckets) + len(h.NegativeBuckets) - h = h.ReduceResolution(h.Schema - 1) - if len(h.PositiveBuckets)+len(h.NegativeBuckets) == lastCount { + if h.Schema == -4 { return 0, errBucketLimit } + h = h.ReduceResolution(h.Schema - 1) } } if fh != nil { for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > app.limit { - lastCount := len(fh.PositiveBuckets) + len(fh.NegativeBuckets) - fh = fh.ReduceResolution(fh.Schema - 1) - if len(fh.PositiveBuckets)+len(fh.NegativeBuckets) == lastCount { + if fh.Schema == -4 { return 0, errBucketLimit } + fh = fh.ReduceResolution(fh.Schema - 1) } } ref, err := app.Appender.AppendHistogram(ref, lset, t, h, fh)