optimize the logic of break the loop of reducing resolution

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
pull/13129/head
Ziqi Zhao 2023-11-21 16:56:56 +08:00
parent 10ebeb0a62
commit 8fe9250f7d
2 changed files with 27 additions and 6 deletions

View File

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

View File

@ -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)