@@ -344,85 +344,116 @@ func TestValidateNativeHistogram(t *testing.T) {
344
344
histogramWithSchemaMin .Schema = histogram .ExponentialSchemaMin
345
345
floatHistogramWithSchemaMin := tsdbutil .GenerateTestFloatHistogram (0 )
346
346
floatHistogramWithSchemaMin .Schema = histogram .ExponentialSchemaMin
347
+
348
+ belowMinRangeSchemaHistogram := tsdbutil .GenerateTestFloatHistogram (0 )
349
+ belowMinRangeSchemaHistogram .Schema = - 5
350
+ exceedMaxRangeSchemaFloatHistogram := tsdbutil .GenerateTestFloatHistogram (0 )
351
+ exceedMaxRangeSchemaFloatHistogram .Schema = 20
352
+
347
353
for _ , tc := range []struct {
348
- name string
349
- bucketLimit int
350
- resolutionReduced bool
351
- histogram cortexpb.Histogram
352
- expectedHistogram cortexpb.Histogram
353
- expectedErr error
354
+ name string
355
+ bucketLimit int
356
+ resolutionReduced bool
357
+ histogram cortexpb.Histogram
358
+ expectedHistogram cortexpb.Histogram
359
+ expectedErr error
360
+ discardedSampleLabelValue string
354
361
}{
355
362
{
356
- name : "no limit, histogram" ,
357
- histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
358
- expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
363
+ name : "no limit, histogram" ,
364
+ histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
365
+ expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
366
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
367
+ },
368
+ {
369
+ name : "no limit, float histogram" ,
370
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
371
+ expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
372
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
373
+ },
374
+ {
375
+ name : "within limit, histogram" ,
376
+ bucketLimit : 8 ,
377
+ histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
378
+ expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
379
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
359
380
},
360
381
{
361
- name : "no limit, float histogram" ,
362
- histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
363
- expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
382
+ name : "within limit, float histogram" ,
383
+ bucketLimit : 8 ,
384
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
385
+ expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
386
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
364
387
},
365
388
{
366
- name : "within limit, histogram" ,
367
- bucketLimit : 8 ,
368
- histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
369
- expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
389
+ name : "exceed limit and reduce resolution for 1 level, histogram" ,
390
+ bucketLimit : 6 ,
391
+ histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
392
+ expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ().ReduceResolution (0 )),
393
+ resolutionReduced : true ,
394
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
370
395
},
371
396
{
372
- name : "within limit, float histogram" ,
373
- bucketLimit : 8 ,
374
- histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
375
- expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
397
+ name : "exceed limit and reduce resolution for 1 level, float histogram" ,
398
+ bucketLimit : 6 ,
399
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
400
+ expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ().ReduceResolution (0 )),
401
+ resolutionReduced : true ,
402
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
376
403
},
377
404
{
378
- name : "exceed limit and reduce resolution for 1 level , histogram" ,
379
- bucketLimit : 6 ,
380
- histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
381
- expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ().ReduceResolution (0 )),
382
- resolutionReduced : true ,
405
+ name : "exceed limit and reduce resolution for 2 levels , histogram" ,
406
+ bucketLimit : 4 ,
407
+ histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
408
+ expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ().ReduceResolution (- 1 )),
409
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
383
410
},
384
411
{
385
- name : "exceed limit and reduce resolution for 1 level , float histogram" ,
386
- bucketLimit : 6 ,
387
- histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
388
- expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ().ReduceResolution (0 )),
389
- resolutionReduced : true ,
412
+ name : "exceed limit and reduce resolution for 2 levels , float histogram" ,
413
+ bucketLimit : 4 ,
414
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
415
+ expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ().ReduceResolution (- 1 )),
416
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
390
417
},
391
418
{
392
- name : "exceed limit and reduce resolution for 2 levels, histogram" ,
393
- bucketLimit : 4 ,
394
- histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
395
- expectedHistogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ().ReduceResolution (- 1 )),
419
+ name : "exceed limit but cannot reduce resolution further, histogram" ,
420
+ bucketLimit : 1 ,
421
+ histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
422
+ expectedErr : newHistogramBucketLimitExceededError (lbls , 1 ),
423
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
396
424
},
397
425
{
398
- name : "exceed limit and reduce resolution for 2 levels, float histogram" ,
399
- bucketLimit : 4 ,
400
- histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
401
- expectedHistogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ().ReduceResolution (- 1 )),
426
+ name : "exceed limit but cannot reduce resolution further, float histogram" ,
427
+ bucketLimit : 1 ,
428
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
429
+ expectedErr : newHistogramBucketLimitExceededError (lbls , 1 ),
430
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
402
431
},
403
432
{
404
- name : "exceed limit but cannot reduce resolution further, histogram" ,
405
- bucketLimit : 1 ,
406
- histogram : cortexpb .HistogramToHistogramProto (0 , h .Copy ()),
407
- expectedErr : newHistogramBucketLimitExceededError (lbls , 1 ),
433
+ name : "exceed limit but cannot reduce resolution further with min schema, histogram" ,
434
+ bucketLimit : 4 ,
435
+ histogram : cortexpb .HistogramToHistogramProto (0 , histogramWithSchemaMin .Copy ()),
436
+ expectedErr : newHistogramBucketLimitExceededError (lbls , 4 ),
437
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
408
438
},
409
439
{
410
- name : "exceed limit but cannot reduce resolution further, float histogram" ,
411
- bucketLimit : 1 ,
412
- histogram : cortexpb .FloatHistogramToHistogramProto (0 , fh .Copy ()),
413
- expectedErr : newHistogramBucketLimitExceededError (lbls , 1 ),
440
+ name : "exceed limit but cannot reduce resolution further with min schema, float histogram" ,
441
+ bucketLimit : 4 ,
442
+ histogram : cortexpb .FloatHistogramToHistogramProto (0 , floatHistogramWithSchemaMin .Copy ()),
443
+ expectedErr : newHistogramBucketLimitExceededError (lbls , 4 ),
444
+ discardedSampleLabelValue : nativeHistogramBucketCountLimitExceeded ,
414
445
},
415
446
{
416
- name : "exceed limit but cannot reduce resolution further with min schema, histogram " ,
417
- bucketLimit : 4 ,
418
- histogram : cortexpb . HistogramToHistogramProto ( 0 , histogramWithSchemaMin . Copy ( )),
419
- expectedErr : newHistogramBucketLimitExceededError ( lbls , 4 ) ,
447
+ name : "exceed min schema limit " ,
448
+ histogram : cortexpb . FloatHistogramToHistogramProto ( 0 , belowMinRangeSchemaHistogram . Copy ()) ,
449
+ expectedErr : newNativeHistogramSchemaInvalidError ( lbls , int ( belowMinRangeSchemaHistogram . Schema )),
450
+ discardedSampleLabelValue : nativeHistogramInvalidSchema ,
420
451
},
421
452
{
422
- name : "exceed limit but cannot reduce resolution further with min schema, float histogram " ,
423
- bucketLimit : 4 ,
424
- histogram : cortexpb . FloatHistogramToHistogramProto ( 0 , floatHistogramWithSchemaMin . Copy ( )),
425
- expectedErr : newHistogramBucketLimitExceededError ( lbls , 4 ) ,
453
+ name : "exceed max schema limit " ,
454
+ histogram : cortexpb . FloatHistogramToHistogramProto ( 0 , exceedMaxRangeSchemaFloatHistogram . Copy ()) ,
455
+ expectedErr : newNativeHistogramSchemaInvalidError ( lbls , int ( exceedMaxRangeSchemaFloatHistogram . Schema )),
456
+ discardedSampleLabelValue : nativeHistogramInvalidSchema ,
426
457
},
427
458
} {
428
459
t .Run (tc .name , func (t * testing.T ) {
@@ -433,7 +464,7 @@ func TestValidateNativeHistogram(t *testing.T) {
433
464
actualHistogram , actualErr := ValidateNativeHistogram (validateMetrics , limits , userID , lbls , tc .histogram )
434
465
if tc .expectedErr != nil {
435
466
require .Equal (t , tc .expectedErr , actualErr )
436
- require .Equal (t , float64 (1 ), testutil .ToFloat64 (validateMetrics .DiscardedSamples .WithLabelValues (nativeHistogramBucketCountLimitExceeded , userID )))
467
+ require .Equal (t , float64 (1 ), testutil .ToFloat64 (validateMetrics .DiscardedSamples .WithLabelValues (tc . discardedSampleLabelValue , userID )))
437
468
// Should never increment if error was returned
438
469
require .Equal (t , float64 (0 ), testutil .ToFloat64 (validateMetrics .HistogramSamplesReducedResolution .WithLabelValues (userID )))
439
470
0 commit comments