2015-03-30 10:13:36 -07:00
|
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
"sort"
|
|
|
|
|
|
2021-12-06 05:47:22 -08:00
|
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-11-08 06:23:17 -08:00
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2015-03-30 10:13:36 -07:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Helpers to calculate quantiles.
|
|
|
|
|
|
|
|
|
|
// excludedLabels are the labels to exclude from signature calculation for
|
|
|
|
|
// quantiles.
|
2016-12-23 04:51:59 -08:00
|
|
|
|
var excludedLabels = []string{
|
2016-12-24 05:35:24 -08:00
|
|
|
|
labels.MetricName,
|
|
|
|
|
labels.BucketLabel,
|
2015-03-30 10:13:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type bucket struct {
|
|
|
|
|
upperBound float64
|
2016-12-23 04:51:59 -08:00
|
|
|
|
count float64
|
2015-03-30 10:13:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buckets implements sort.Interface.
|
|
|
|
|
type buckets []bucket
|
|
|
|
|
|
|
|
|
|
func (b buckets) Len() int { return len(b) }
|
|
|
|
|
func (b buckets) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
|
func (b buckets) Less(i, j int) bool { return b[i].upperBound < b[j].upperBound }
|
|
|
|
|
|
|
|
|
|
type metricWithBuckets struct {
|
2016-12-23 04:51:59 -08:00
|
|
|
|
metric labels.Labels
|
2015-03-30 10:13:36 -07:00
|
|
|
|
buckets buckets
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 05:33:20 -07:00
|
|
|
|
// bucketQuantile calculates the quantile 'q' based on the given buckets. The
|
|
|
|
|
// buckets will be sorted by upperBound by this function (i.e. no sorting
|
|
|
|
|
// needed before calling this function). The quantile value is interpolated
|
|
|
|
|
// assuming a linear distribution within a bucket. However, if the quantile
|
|
|
|
|
// falls into the highest bucket, the upper bound of the 2nd highest bucket is
|
|
|
|
|
// returned. A natural lower bound of 0 is assumed if the upper bound of the
|
|
|
|
|
// lowest bucket is greater 0. In that case, interpolation in the lowest bucket
|
|
|
|
|
// happens linearly between 0 and the upper bound of the lowest bucket.
|
|
|
|
|
// However, if the lowest bucket has an upper bound less or equal 0, this upper
|
|
|
|
|
// bound is returned if the quantile falls into the lowest bucket.
|
2015-03-30 10:13:36 -07:00
|
|
|
|
//
|
|
|
|
|
// There are a number of special cases (once we have a way to report errors
|
|
|
|
|
// happening during evaluations of AST functions, we should report those
|
|
|
|
|
// explicitly):
|
|
|
|
|
//
|
2020-06-01 01:40:39 -07:00
|
|
|
|
// If 'buckets' has 0 observations, NaN is returned.
|
|
|
|
|
//
|
2015-03-30 10:13:36 -07:00
|
|
|
|
// If 'buckets' has fewer than 2 elements, NaN is returned.
|
|
|
|
|
//
|
|
|
|
|
// If the highest bucket is not +Inf, NaN is returned.
|
|
|
|
|
//
|
2022-02-13 05:59:03 -08:00
|
|
|
|
// If q==NaN, NaN is returned.
|
|
|
|
|
//
|
2015-03-30 10:13:36 -07:00
|
|
|
|
// If q<0, -Inf is returned.
|
|
|
|
|
//
|
|
|
|
|
// If q>1, +Inf is returned.
|
2016-12-23 04:51:59 -08:00
|
|
|
|
func bucketQuantile(q float64, buckets buckets) float64 {
|
2022-02-13 05:59:03 -08:00
|
|
|
|
if math.IsNaN(q) {
|
2022-02-13 05:41:28 -08:00
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
2015-03-30 10:13:36 -07:00
|
|
|
|
if q < 0 {
|
|
|
|
|
return math.Inf(-1)
|
|
|
|
|
}
|
|
|
|
|
if q > 1 {
|
|
|
|
|
return math.Inf(+1)
|
|
|
|
|
}
|
|
|
|
|
sort.Sort(buckets)
|
|
|
|
|
if !math.IsInf(buckets[len(buckets)-1].upperBound, +1) {
|
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 02:22:44 -08:00
|
|
|
|
buckets = coalesceBuckets(buckets)
|
Force buckets in a histogram to be monotonic for quantile estimation (#2610)
* Force buckets in a histogram to be monotonic for quantile estimation
The assumption that bucket counts increase monotonically with increasing
upperBound may be violated during:
* Recording rule evaluation of histogram_quantile, especially when rate()
has been applied to the underlying bucket timeseries.
* Evaluation of histogram_quantile computed over federated bucket
timeseries, especially when rate() has been applied
This is because scraped data is not made available to RR evalution or
federation atomically, so some buckets are computed with data from the N
most recent scrapes, but the other buckets are missing the most recent
observations.
Monotonicity is usually guaranteed because if a bucket with upper bound
u1 has count c1, then any bucket with a higher upper bound u > u1 must
have counted all c1 observations and perhaps more, so that c >= c1.
Randomly interspersed partial sampling breaks that guarantee, and rate()
exacerbates it. Specifically, suppose bucket le=1000 has a count of 10 from
4 samples but the bucket with le=2000 has a count of 7, from 3 samples. The
monotonicity is broken. It is exacerbated by rate() because under normal
operation, cumulative counting of buckets will cause the bucket counts to
diverge such that small differences from missing samples are not a problem.
rate() removes this divergence.)
bucketQuantile depends on that monotonicity to do a binary search for the
bucket with the qth percentile count, so breaking the monotonicity
guarantee causes bucketQuantile() to return undefined (nonsense) results.
As a somewhat hacky solution until the Prometheus project is ready to
accept the changes required to make scrapes atomic, we calculate the
"envelope" of the histogram buckets, essentially removing any decreases
in the count between successive buckets.
* Fix up comment docs for ensureMonotonic
* ensureMonotonic: Use switch statement
Use switch statement rather than if/else for better readability.
Process the most frequent cases first.
2017-04-14 07:21:49 -07:00
|
|
|
|
ensureMonotonic(buckets)
|
|
|
|
|
|
2019-02-01 02:22:44 -08:00
|
|
|
|
if len(buckets) < 2 {
|
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
2020-06-01 01:40:39 -07:00
|
|
|
|
observations := buckets[len(buckets)-1].count
|
|
|
|
|
if observations == 0 {
|
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
|
|
|
|
rank := q * observations
|
2015-03-30 10:13:36 -07:00
|
|
|
|
b := sort.Search(len(buckets)-1, func(i int) bool { return buckets[i].count >= rank })
|
|
|
|
|
|
|
|
|
|
if b == len(buckets)-1 {
|
|
|
|
|
return buckets[len(buckets)-2].upperBound
|
|
|
|
|
}
|
|
|
|
|
if b == 0 && buckets[0].upperBound <= 0 {
|
|
|
|
|
return buckets[0].upperBound
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
bucketStart float64
|
|
|
|
|
bucketEnd = buckets[b].upperBound
|
|
|
|
|
count = buckets[b].count
|
|
|
|
|
)
|
|
|
|
|
if b > 0 {
|
|
|
|
|
bucketStart = buckets[b-1].upperBound
|
|
|
|
|
count -= buckets[b-1].count
|
|
|
|
|
rank -= buckets[b-1].count
|
|
|
|
|
}
|
2018-06-06 10:20:38 -07:00
|
|
|
|
return bucketStart + (bucketEnd-bucketStart)*(rank/count)
|
2015-03-30 10:13:36 -07:00
|
|
|
|
}
|
2016-07-08 05:33:20 -07:00
|
|
|
|
|
2021-12-06 05:47:22 -08:00
|
|
|
|
// histogramQuantile calculates the quantile 'q' based on the given histogram.
|
2021-12-15 07:50:37 -08:00
|
|
|
|
//
|
|
|
|
|
// The quantile value is interpolated assuming a linear distribution within a
|
|
|
|
|
// bucket.
|
|
|
|
|
// TODO(beorn7): Find an interpolation method that is a better fit for
|
|
|
|
|
// exponential buckets (and think about configurable interpolation).
|
|
|
|
|
//
|
2022-06-16 09:54:28 -07:00
|
|
|
|
// A natural lower bound of 0 is assumed if the histogram has only positive
|
|
|
|
|
// buckets. Likewise, a natural upper bound of 0 is assumed if the histogram has
|
|
|
|
|
// only negative buckets.
|
|
|
|
|
// TODO(beorn7): Come to terms if we want that.
|
2021-12-06 05:47:22 -08:00
|
|
|
|
//
|
|
|
|
|
// There are a number of special cases (once we have a way to report errors
|
|
|
|
|
// happening during evaluations of AST functions, we should report those
|
|
|
|
|
// explicitly):
|
|
|
|
|
//
|
2021-12-15 07:50:37 -08:00
|
|
|
|
// If the histogram has 0 observations, NaN is returned.
|
2021-12-06 05:47:22 -08:00
|
|
|
|
//
|
|
|
|
|
// If q<0, -Inf is returned.
|
|
|
|
|
//
|
2021-12-15 07:50:37 -08:00
|
|
|
|
// If q>1, +Inf is returned.
|
2022-06-16 11:44:12 -07:00
|
|
|
|
//
|
|
|
|
|
// If q is NaN, NaN is returned.
|
2021-12-06 05:47:22 -08:00
|
|
|
|
func histogramQuantile(q float64, h *histogram.FloatHistogram) float64 {
|
|
|
|
|
if q < 0 {
|
|
|
|
|
return math.Inf(-1)
|
|
|
|
|
}
|
|
|
|
|
if q > 1 {
|
|
|
|
|
return math.Inf(+1)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 11:44:12 -07:00
|
|
|
|
if h.Count == 0 || math.IsNaN(q) {
|
2021-12-06 05:47:22 -08:00
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2021-12-15 08:39:32 -08:00
|
|
|
|
bucket histogram.FloatBucket
|
2021-12-06 05:47:22 -08:00
|
|
|
|
count float64
|
2021-12-15 07:50:37 -08:00
|
|
|
|
it = h.AllBucketIterator()
|
2021-12-06 05:47:22 -08:00
|
|
|
|
rank = q * h.Count
|
|
|
|
|
)
|
|
|
|
|
for it.Next() {
|
2021-12-15 08:39:32 -08:00
|
|
|
|
bucket = it.At()
|
|
|
|
|
count += bucket.Count
|
2021-12-06 05:47:22 -08:00
|
|
|
|
if count >= rank {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-16 09:54:28 -07:00
|
|
|
|
if bucket.Lower < 0 && bucket.Upper > 0 {
|
|
|
|
|
if len(h.NegativeBuckets) == 0 && len(h.PositiveBuckets) > 0 {
|
|
|
|
|
// The result is in the zero bucket and the histogram has only
|
|
|
|
|
// positive buckets. So we consider 0 to be the lower bound.
|
|
|
|
|
bucket.Lower = 0
|
|
|
|
|
} else if len(h.PositiveBuckets) == 0 && len(h.NegativeBuckets) > 0 {
|
|
|
|
|
// The result is in the zero bucket and the histogram has only
|
|
|
|
|
// negative buckets. So we consider 0 to be the upper bound.
|
|
|
|
|
bucket.Upper = 0
|
|
|
|
|
}
|
2021-12-06 05:47:22 -08:00
|
|
|
|
}
|
2021-12-15 08:39:32 -08:00
|
|
|
|
// Due to numerical inaccuracies, we could end up with a higher count
|
|
|
|
|
// than h.Count. Thus, make sure count is never higher than h.Count.
|
|
|
|
|
if count > h.Count {
|
|
|
|
|
count = h.Count
|
|
|
|
|
}
|
|
|
|
|
// We could have hit the highest bucket without even reaching the rank
|
|
|
|
|
// (observations not counted in any bucket are considered "overflow"
|
|
|
|
|
// observations above the highest bucket), in which case we simple
|
|
|
|
|
// return the upper limit of the highest explicit bucket.
|
|
|
|
|
if count < rank {
|
|
|
|
|
return bucket.Upper
|
|
|
|
|
}
|
2021-12-06 05:47:22 -08:00
|
|
|
|
|
|
|
|
|
rank -= count - bucket.Count
|
|
|
|
|
// TODO(codesome): Use a better estimation than linear.
|
|
|
|
|
return bucket.Lower + (bucket.Upper-bucket.Lower)*(rank/bucket.Count)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 11:44:12 -07:00
|
|
|
|
// histogramFraction calculates the fraction of observations between the
|
|
|
|
|
// provided lower and upper bounds, based on the provided histogram.
|
|
|
|
|
//
|
|
|
|
|
// histogramFraction is in a certain way the inverse of histogramQuantile. If
|
|
|
|
|
// histogramQuantile(0.9, h) returns 123.4, then histogramFraction(-Inf, 123.4, h)
|
|
|
|
|
// returns 0.9.
|
|
|
|
|
//
|
|
|
|
|
// The same notes (and TODOs) with regard to interpolation and assumptions about
|
|
|
|
|
// the zero bucket boundaries apply as for histogramQuantile.
|
|
|
|
|
//
|
|
|
|
|
// Whether either boundary is inclusive or exclusive doesn’t actually matter as
|
|
|
|
|
// long as interpolation has to be performed anyway. In the case of a boundary
|
|
|
|
|
// coinciding with a bucket boundary, the inclusive or exclusive nature of the
|
|
|
|
|
// boundary determines the exact behavior of the threshold. With the current
|
|
|
|
|
// implementation, that means that lower is exclusive for positive values and
|
|
|
|
|
// inclusive for negative values, while upper is inclusive for positive values
|
|
|
|
|
// and exclusive for negative values.
|
|
|
|
|
//
|
|
|
|
|
// Special cases:
|
|
|
|
|
//
|
|
|
|
|
// If the histogram has 0 observations, NaN is returned.
|
|
|
|
|
//
|
|
|
|
|
// Use a lower bound of -Inf to get the fraction of all observations below the
|
|
|
|
|
// upper bound.
|
|
|
|
|
//
|
|
|
|
|
// Use an upper bound of +Inf to get the fraction of all observations above the
|
|
|
|
|
// lower bound.
|
|
|
|
|
//
|
|
|
|
|
// If lower or upper is NaN, NaN is returned.
|
|
|
|
|
//
|
|
|
|
|
// If lower >= upper and the histogram has at least 1 observation, zero is returned.
|
|
|
|
|
func histogramFraction(lower, upper float64, h *histogram.FloatHistogram) float64 {
|
|
|
|
|
if h.Count == 0 || math.IsNaN(lower) || math.IsNaN(upper) {
|
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
|
|
|
|
if lower >= upper {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
rank, lowerRank, upperRank float64
|
|
|
|
|
lowerSet, upperSet bool
|
|
|
|
|
it = h.AllBucketIterator()
|
|
|
|
|
)
|
|
|
|
|
for it.Next() {
|
|
|
|
|
b := it.At()
|
|
|
|
|
if b.Lower < 0 && b.Upper > 0 {
|
|
|
|
|
if len(h.NegativeBuckets) == 0 && len(h.PositiveBuckets) > 0 {
|
|
|
|
|
// This is the zero bucket and the histogram has only
|
|
|
|
|
// positive buckets. So we consider 0 to be the lower
|
|
|
|
|
// bound.
|
|
|
|
|
b.Lower = 0
|
|
|
|
|
} else if len(h.PositiveBuckets) == 0 && len(h.NegativeBuckets) > 0 {
|
|
|
|
|
// This is in the zero bucket and the histogram has only
|
|
|
|
|
// negative buckets. So we consider 0 to be the upper
|
|
|
|
|
// bound.
|
|
|
|
|
b.Upper = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !lowerSet && b.Lower >= lower {
|
|
|
|
|
lowerRank = rank
|
|
|
|
|
lowerSet = true
|
|
|
|
|
}
|
|
|
|
|
if !upperSet && b.Lower >= upper {
|
|
|
|
|
upperRank = rank
|
|
|
|
|
upperSet = true
|
|
|
|
|
}
|
|
|
|
|
if lowerSet && upperSet {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if !lowerSet && b.Lower < lower && b.Upper > lower {
|
|
|
|
|
lowerRank = rank + b.Count*(lower-b.Lower)/(b.Upper-b.Lower)
|
|
|
|
|
lowerSet = true
|
|
|
|
|
}
|
|
|
|
|
if !upperSet && b.Lower < upper && b.Upper > upper {
|
|
|
|
|
upperRank = rank + b.Count*(upper-b.Lower)/(b.Upper-b.Lower)
|
|
|
|
|
upperSet = true
|
|
|
|
|
}
|
|
|
|
|
if lowerSet && upperSet {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
rank += b.Count
|
|
|
|
|
}
|
|
|
|
|
if !lowerSet || lowerRank > h.Count {
|
|
|
|
|
lowerRank = h.Count
|
|
|
|
|
}
|
|
|
|
|
if !upperSet || upperRank > h.Count {
|
|
|
|
|
upperRank = h.Count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (upperRank - lowerRank) / h.Count
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 02:22:44 -08:00
|
|
|
|
// coalesceBuckets merges buckets with the same upper bound.
|
|
|
|
|
//
|
|
|
|
|
// The input buckets must be sorted.
|
|
|
|
|
func coalesceBuckets(buckets buckets) buckets {
|
|
|
|
|
last := buckets[0]
|
|
|
|
|
i := 0
|
|
|
|
|
for _, b := range buckets[1:] {
|
|
|
|
|
if b.upperBound == last.upperBound {
|
|
|
|
|
last.count += b.count
|
|
|
|
|
} else {
|
|
|
|
|
buckets[i] = last
|
|
|
|
|
last = b
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buckets[i] = last
|
|
|
|
|
return buckets[:i+1]
|
|
|
|
|
}
|
|
|
|
|
|
Force buckets in a histogram to be monotonic for quantile estimation (#2610)
* Force buckets in a histogram to be monotonic for quantile estimation
The assumption that bucket counts increase monotonically with increasing
upperBound may be violated during:
* Recording rule evaluation of histogram_quantile, especially when rate()
has been applied to the underlying bucket timeseries.
* Evaluation of histogram_quantile computed over federated bucket
timeseries, especially when rate() has been applied
This is because scraped data is not made available to RR evalution or
federation atomically, so some buckets are computed with data from the N
most recent scrapes, but the other buckets are missing the most recent
observations.
Monotonicity is usually guaranteed because if a bucket with upper bound
u1 has count c1, then any bucket with a higher upper bound u > u1 must
have counted all c1 observations and perhaps more, so that c >= c1.
Randomly interspersed partial sampling breaks that guarantee, and rate()
exacerbates it. Specifically, suppose bucket le=1000 has a count of 10 from
4 samples but the bucket with le=2000 has a count of 7, from 3 samples. The
monotonicity is broken. It is exacerbated by rate() because under normal
operation, cumulative counting of buckets will cause the bucket counts to
diverge such that small differences from missing samples are not a problem.
rate() removes this divergence.)
bucketQuantile depends on that monotonicity to do a binary search for the
bucket with the qth percentile count, so breaking the monotonicity
guarantee causes bucketQuantile() to return undefined (nonsense) results.
As a somewhat hacky solution until the Prometheus project is ready to
accept the changes required to make scrapes atomic, we calculate the
"envelope" of the histogram buckets, essentially removing any decreases
in the count between successive buckets.
* Fix up comment docs for ensureMonotonic
* ensureMonotonic: Use switch statement
Use switch statement rather than if/else for better readability.
Process the most frequent cases first.
2017-04-14 07:21:49 -07:00
|
|
|
|
// The assumption that bucket counts increase monotonically with increasing
|
|
|
|
|
// upperBound may be violated during:
|
|
|
|
|
//
|
|
|
|
|
// * Recording rule evaluation of histogram_quantile, especially when rate()
|
|
|
|
|
// has been applied to the underlying bucket timeseries.
|
|
|
|
|
// * Evaluation of histogram_quantile computed over federated bucket
|
|
|
|
|
// timeseries, especially when rate() has been applied.
|
|
|
|
|
//
|
|
|
|
|
// This is because scraped data is not made available to rule evaluation or
|
|
|
|
|
// federation atomically, so some buckets are computed with data from the
|
|
|
|
|
// most recent scrapes, but the other buckets are missing data from the most
|
|
|
|
|
// recent scrape.
|
|
|
|
|
//
|
|
|
|
|
// Monotonicity is usually guaranteed because if a bucket with upper bound
|
|
|
|
|
// u1 has count c1, then any bucket with a higher upper bound u > u1 must
|
|
|
|
|
// have counted all c1 observations and perhaps more, so that c >= c1.
|
|
|
|
|
//
|
|
|
|
|
// Randomly interspersed partial sampling breaks that guarantee, and rate()
|
|
|
|
|
// exacerbates it. Specifically, suppose bucket le=1000 has a count of 10 from
|
|
|
|
|
// 4 samples but the bucket with le=2000 has a count of 7 from 3 samples. The
|
|
|
|
|
// monotonicity is broken. It is exacerbated by rate() because under normal
|
|
|
|
|
// operation, cumulative counting of buckets will cause the bucket counts to
|
|
|
|
|
// diverge such that small differences from missing samples are not a problem.
|
|
|
|
|
// rate() removes this divergence.)
|
|
|
|
|
//
|
|
|
|
|
// bucketQuantile depends on that monotonicity to do a binary search for the
|
|
|
|
|
// bucket with the φ-quantile count, so breaking the monotonicity
|
|
|
|
|
// guarantee causes bucketQuantile() to return undefined (nonsense) results.
|
|
|
|
|
//
|
|
|
|
|
// As a somewhat hacky solution until ingestion is atomic per scrape, we
|
|
|
|
|
// calculate the "envelope" of the histogram buckets, essentially removing
|
|
|
|
|
// any decreases in the count between successive buckets.
|
|
|
|
|
|
|
|
|
|
func ensureMonotonic(buckets buckets) {
|
|
|
|
|
max := buckets[0].count
|
2020-06-15 03:32:10 -07:00
|
|
|
|
for i := 1; i < len(buckets); i++ {
|
Force buckets in a histogram to be monotonic for quantile estimation (#2610)
* Force buckets in a histogram to be monotonic for quantile estimation
The assumption that bucket counts increase monotonically with increasing
upperBound may be violated during:
* Recording rule evaluation of histogram_quantile, especially when rate()
has been applied to the underlying bucket timeseries.
* Evaluation of histogram_quantile computed over federated bucket
timeseries, especially when rate() has been applied
This is because scraped data is not made available to RR evalution or
federation atomically, so some buckets are computed with data from the N
most recent scrapes, but the other buckets are missing the most recent
observations.
Monotonicity is usually guaranteed because if a bucket with upper bound
u1 has count c1, then any bucket with a higher upper bound u > u1 must
have counted all c1 observations and perhaps more, so that c >= c1.
Randomly interspersed partial sampling breaks that guarantee, and rate()
exacerbates it. Specifically, suppose bucket le=1000 has a count of 10 from
4 samples but the bucket with le=2000 has a count of 7, from 3 samples. The
monotonicity is broken. It is exacerbated by rate() because under normal
operation, cumulative counting of buckets will cause the bucket counts to
diverge such that small differences from missing samples are not a problem.
rate() removes this divergence.)
bucketQuantile depends on that monotonicity to do a binary search for the
bucket with the qth percentile count, so breaking the monotonicity
guarantee causes bucketQuantile() to return undefined (nonsense) results.
As a somewhat hacky solution until the Prometheus project is ready to
accept the changes required to make scrapes atomic, we calculate the
"envelope" of the histogram buckets, essentially removing any decreases
in the count between successive buckets.
* Fix up comment docs for ensureMonotonic
* ensureMonotonic: Use switch statement
Use switch statement rather than if/else for better readability.
Process the most frequent cases first.
2017-04-14 07:21:49 -07:00
|
|
|
|
switch {
|
|
|
|
|
case buckets[i].count > max:
|
|
|
|
|
max = buckets[i].count
|
|
|
|
|
case buckets[i].count < max:
|
|
|
|
|
buckets[i].count = max
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 22:11:16 -07:00
|
|
|
|
// quantile calculates the given quantile of a vector of samples.
|
2016-07-08 05:48:48 -07:00
|
|
|
|
//
|
2016-12-24 01:40:09 -08:00
|
|
|
|
// The Vector will be sorted.
|
2016-07-08 05:48:48 -07:00
|
|
|
|
// If 'values' has zero elements, NaN is returned.
|
2022-02-13 05:59:03 -08:00
|
|
|
|
// If q==NaN, NaN is returned.
|
2016-07-08 05:48:48 -07:00
|
|
|
|
// If q<0, -Inf is returned.
|
|
|
|
|
// If q>1, +Inf is returned.
|
2016-12-24 02:37:16 -08:00
|
|
|
|
func quantile(q float64, values vectorByValueHeap) float64 {
|
2022-02-13 05:59:03 -08:00
|
|
|
|
if len(values) == 0 || math.IsNaN(q) {
|
2016-07-08 05:48:48 -07:00
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
2016-07-08 05:33:20 -07:00
|
|
|
|
if q < 0 {
|
|
|
|
|
return math.Inf(-1)
|
|
|
|
|
}
|
|
|
|
|
if q > 1 {
|
|
|
|
|
return math.Inf(+1)
|
|
|
|
|
}
|
2016-07-08 05:48:48 -07:00
|
|
|
|
sort.Sort(values)
|
2016-07-08 05:33:20 -07:00
|
|
|
|
|
|
|
|
|
n := float64(len(values))
|
|
|
|
|
// When the quantile lies between two samples,
|
|
|
|
|
// we use a weighted average of the two samples.
|
|
|
|
|
rank := q * (n - 1)
|
|
|
|
|
|
|
|
|
|
lowerIndex := math.Max(0, math.Floor(rank))
|
|
|
|
|
upperIndex := math.Min(n-1, lowerIndex+1)
|
|
|
|
|
|
|
|
|
|
weight := rank - math.Floor(rank)
|
2018-06-06 10:20:38 -07:00
|
|
|
|
return values[int(lowerIndex)].V*(1-weight) + values[int(upperIndex)].V*weight
|
2016-07-08 05:33:20 -07:00
|
|
|
|
}
|