Return NaN for histogram_quantile when buckets have 0 observations (#7318)

Signed-off-by: jberny <f.bernardi89@gmail.com>
This commit is contained in:
B++ 2020-06-01 10:40:39 +02:00 committed by GitHub
parent 4424d2c3a4
commit d6374ae1b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -182,8 +182,8 @@ is assumed to be 0 if the upper bound of that bucket is greater than
bucket. Otherwise, the upper bound of the lowest bucket is returned bucket. Otherwise, the upper bound of the lowest bucket is returned
for quantiles located in the lowest bucket. for quantiles located in the lowest bucket.
If `b` contains fewer than two buckets, `NaN` is returned. For φ < 0, `-Inf` is If `b` has 0 observations, `NaN` is returned. If `b` contains fewer than two buckets,
returned. For φ > 1, `+Inf` is returned. `NaN` is returned. For φ < 0, `-Inf` is returned. For φ > 1, `+Inf` is returned.
## `holt_winters()` ## `holt_winters()`

View file

@ -61,6 +61,8 @@ type metricWithBuckets struct {
// happening during evaluations of AST functions, we should report those // happening during evaluations of AST functions, we should report those
// explicitly): // explicitly):
// //
// If 'buckets' has 0 observations, NaN is returned.
//
// If 'buckets' has fewer than 2 elements, NaN is returned. // If 'buckets' has fewer than 2 elements, NaN is returned.
// //
// If the highest bucket is not +Inf, NaN is returned. // If the highest bucket is not +Inf, NaN is returned.
@ -86,8 +88,11 @@ func bucketQuantile(q float64, buckets buckets) float64 {
if len(buckets) < 2 { if len(buckets) < 2 {
return math.NaN() return math.NaN()
} }
observations := buckets[len(buckets)-1].count
rank := q * buckets[len(buckets)-1].count if observations == 0 {
return math.NaN()
}
rank := q * observations
b := sort.Search(len(buckets)-1, func(i int) bool { return buckets[i].count >= rank }) b := sort.Search(len(buckets)-1, func(i int) bool { return buckets[i].count >= rank })
if b == len(buckets)-1 { if b == len(buckets)-1 {

View file

@ -179,3 +179,11 @@ eval instant at 50m histogram_quantile(0.75, rate(mixed_bucket[5m]))
eval instant at 50m histogram_quantile(1, rate(mixed_bucket[5m])) eval instant at 50m histogram_quantile(1, rate(mixed_bucket[5m]))
{instance="ins1", job="job1"} 0.2 {instance="ins1", job="job1"} 0.2
{instance="ins2", job="job1"} NaN {instance="ins2", job="job1"} NaN
load 5m
empty_bucket{le="0.1", job="job1", instance="ins1"} 0x10
empty_bucket{le="0.2", job="job1", instance="ins1"} 0x10
empty_bucket{le="+Inf", job="job1", instance="ins1"} 0x10
eval instant at 50m histogram_quantile(0.2, rate(empty_bucket[5m]))
{instance="ins1", job="job1"} NaN