Skip to content

Commit fe788be

Browse files
authored
cleanup native histogram validation const (#6114)
Signed-off-by: Ben Ye <[email protected]>
1 parent 01f4d9d commit fe788be

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* [BUGFIX] Querier: Enforce max query length check for `/api/v1/series` API even though `ignoreMaxQueryLength` is set to true. #6018
3737
* [BUGFIX] Ingester: Fix issue with the minimize token generator where it was not taking in consideration the current ownerhip of an instance when generating extra tokens. #6062
3838
* [BUGFIX] Scheduler: Fix user queue in scheduler that was not thread-safe. #6077
39+
* [BUGFIX] Ingester: Include out-of-order head compaction when compacting TSDB head. #6108
3940

4041
## 1.17.1 2024-05-20
4142

pkg/cortexpb/histograms.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ package cortexpb
1515

1616
import "github.com/prometheus/prometheus/model/histogram"
1717

18-
const (
19-
ExponentialSchemaMax int32 = 8
20-
ExponentialSchemaMin int32 = -4
21-
)
22-
2318
func (h Histogram) IsFloatHistogram() bool {
2419
_, ok := h.GetCount().(*Histogram_CountFloat)
2520
return ok

pkg/util/validation/validate.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/go-kit/log/level"
1212
"github.com/prometheus/client_golang/prometheus"
1313
"github.com/prometheus/common/model"
14+
"github.com/prometheus/prometheus/model/histogram"
1415
"github.com/weaveworks/common/httpgrpc"
1516

1617
"github.com/cortexproject/cortex/pkg/cortexpb"
@@ -265,57 +266,57 @@ func ValidateMetadata(validateMetrics *ValidateMetrics, cfg *Limits, userID stri
265266
return nil
266267
}
267268

268-
func ValidateNativeHistogram(validateMetrics *ValidateMetrics, limits *Limits, userID string, ls []cortexpb.LabelAdapter, histogram cortexpb.Histogram) (cortexpb.Histogram, error) {
269+
func ValidateNativeHistogram(validateMetrics *ValidateMetrics, limits *Limits, userID string, ls []cortexpb.LabelAdapter, histogramSample cortexpb.Histogram) (cortexpb.Histogram, error) {
269270
if limits.MaxNativeHistogramBuckets == 0 {
270-
return histogram, nil
271+
return histogramSample, nil
271272
}
272273

273274
var (
274275
exceedLimit bool
275276
)
276-
if histogram.IsFloatHistogram() {
277+
if histogramSample.IsFloatHistogram() {
277278
// Initial check to see if the bucket limit is exceeded or not. If not, we can avoid type casting.
278-
exceedLimit = len(histogram.PositiveCounts)+len(histogram.NegativeCounts) > limits.MaxNativeHistogramBuckets
279+
exceedLimit = len(histogramSample.PositiveCounts)+len(histogramSample.NegativeCounts) > limits.MaxNativeHistogramBuckets
279280
if !exceedLimit {
280-
return histogram, nil
281+
return histogramSample, nil
281282
}
282283
// Exceed limit.
283-
if histogram.Schema <= cortexpb.ExponentialSchemaMin {
284+
if histogramSample.Schema <= histogram.ExponentialSchemaMin {
284285
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
285286
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
286287
}
287-
fh := cortexpb.FloatHistogramProtoToFloatHistogram(histogram)
288+
fh := cortexpb.FloatHistogramProtoToFloatHistogram(histogramSample)
288289
for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > limits.MaxNativeHistogramBuckets {
289-
if fh.Schema <= cortexpb.ExponentialSchemaMin {
290+
if fh.Schema <= histogram.ExponentialSchemaMin {
290291
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
291292
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
292293
}
293294
fh = fh.ReduceResolution(fh.Schema - 1)
294295
}
295296
// If resolution reduced, convert new float histogram to protobuf type again.
296-
return cortexpb.FloatHistogramToHistogramProto(histogram.TimestampMs, fh), nil
297+
return cortexpb.FloatHistogramToHistogramProto(histogramSample.TimestampMs, fh), nil
297298
}
298299

299300
// Initial check to see if bucket limit is exceeded or not. If not, we can avoid type casting.
300-
exceedLimit = len(histogram.PositiveDeltas)+len(histogram.NegativeDeltas) > limits.MaxNativeHistogramBuckets
301+
exceedLimit = len(histogramSample.PositiveDeltas)+len(histogramSample.NegativeDeltas) > limits.MaxNativeHistogramBuckets
301302
if !exceedLimit {
302-
return histogram, nil
303+
return histogramSample, nil
303304
}
304305
// Exceed limit.
305-
if histogram.Schema <= cortexpb.ExponentialSchemaMin {
306+
if histogramSample.Schema <= histogram.ExponentialSchemaMin {
306307
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
307308
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
308309
}
309-
h := cortexpb.HistogramProtoToHistogram(histogram)
310+
h := cortexpb.HistogramProtoToHistogram(histogramSample)
310311
for len(h.PositiveBuckets)+len(h.NegativeBuckets) > limits.MaxNativeHistogramBuckets {
311-
if h.Schema <= cortexpb.ExponentialSchemaMin {
312+
if h.Schema <= histogram.ExponentialSchemaMin {
312313
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
313314
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
314315
}
315316
h = h.ReduceResolution(h.Schema - 1)
316317
}
317318
// If resolution reduced, convert new histogram to protobuf type again.
318-
return cortexpb.HistogramToHistogramProto(histogram.TimestampMs, h), nil
319+
return cortexpb.HistogramToHistogramProto(histogramSample.TimestampMs, h), nil
319320
}
320321

321322
func DeletePerUserValidationMetrics(validateMetrics *ValidateMetrics, userID string, log log.Logger) {

pkg/util/validation/validate_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/prometheus/client_golang/prometheus"
99
"github.com/prometheus/client_golang/prometheus/testutil"
1010
"github.com/prometheus/common/model"
11+
"github.com/prometheus/prometheus/model/histogram"
1112
"github.com/prometheus/prometheus/model/labels"
1213
"github.com/prometheus/prometheus/tsdb/tsdbutil"
1314
"github.com/stretchr/testify/assert"
@@ -303,9 +304,9 @@ func TestValidateNativeHistogram(t *testing.T) {
303304
fh := tsdbutil.GenerateTestFloatHistogram(0)
304305

305306
histogramWithSchemaMin := tsdbutil.GenerateTestHistogram(0)
306-
histogramWithSchemaMin.Schema = cortexpb.ExponentialSchemaMin
307+
histogramWithSchemaMin.Schema = histogram.ExponentialSchemaMin
307308
floatHistogramWithSchemaMin := tsdbutil.GenerateTestFloatHistogram(0)
308-
floatHistogramWithSchemaMin.Schema = cortexpb.ExponentialSchemaMin
309+
floatHistogramWithSchemaMin.Schema = histogram.ExponentialSchemaMin
309310
for _, tc := range []struct {
310311
name string
311312
bucketLimit int

0 commit comments

Comments
 (0)