From 02e032884a6836e5566434d76a79908cf082254b Mon Sep 17 00:00:00 2001 From: jyz0309 <45495947@qq.com> Date: Sun, 13 Feb 2022 21:59:03 +0800 Subject: [PATCH] add doc Signed-off-by: jyz0309 <45495947@qq.com> --- docs/querying/functions.md | 4 ++-- docs/querying/operators.md | 2 +- promql/promql_test.go | 15 --------------- promql/quantile.go | 9 ++++++--- promql/testdata/histograms.test | 5 +++++ promql/testdata/nan.test | 21 --------------------- 6 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 promql/testdata/nan.test diff --git a/docs/querying/functions.md b/docs/querying/functions.md index 33740ac4a..1de8bb2aa 100644 --- a/docs/querying/functions.md +++ b/docs/querying/functions.md @@ -192,7 +192,7 @@ bucket. Otherwise, the upper bound of the lowest bucket is returned for quantiles located in the lowest bucket. If `b` has 0 observations, `NaN` is returned. If `b` contains fewer than two buckets, -`NaN` is returned. For φ < 0, `-Inf` is returned. For φ > 1, `+Inf` is returned. +`NaN` is returned. For φ < 0, `-Inf` is returned. For φ > 1, `+Inf` is returned. For φ = `NaN`, `NaN` is returned. ## `holt_winters()` @@ -456,4 +456,4 @@ The following are useful for converting between degrees and radians: - `deg(v instant-vector)`: converts radians to degrees for all elements in `v`. - `pi()`: returns pi. -- `rad(v instant-vector)`: converts degrees to radians for all elements in `v`. \ No newline at end of file +- `rad(v instant-vector)`: converts degrees to radians for all elements in `v`. diff --git a/docs/querying/operators.md b/docs/querying/operators.md index a0491bdbe..714b41b3c 100644 --- a/docs/querying/operators.md +++ b/docs/querying/operators.md @@ -241,7 +241,7 @@ vector. `by` and `without` are only used to bucket the input vector. `quantile` calculates the φ-quantile, the value that ranks at number φ*N among the N metric values of the dimensions aggregated over. φ is provided as the aggregation parameter. For example, `quantile(0.5, ...)` calculates the median, -`quantile(0.95, ...)` the 95th percentile. +`quantile(0.95, ...)` the 95th percentile. For φ = `NaN`, `NaN` is returned. Example: diff --git a/promql/promql_test.go b/promql/promql_test.go index 18704fb6b..38dafb61c 100644 --- a/promql/promql_test.go +++ b/promql/promql_test.go @@ -34,18 +34,3 @@ func TestEvaluations(t *testing.T) { }) } } - -func TestOne(t *testing.T) { - files, err := filepath.Glob("testdata/nan.test") - require.NoError(t, err) - - for _, fn := range files { - t.Run(fn, func(t *testing.T) { - test, err := newTestFromFile(t, fn) - require.NoError(t, err) - require.NoError(t, test.Run()) - - test.Close() - }) - } -} diff --git a/promql/quantile.go b/promql/quantile.go index f4a6fa6a1..26dcf81d2 100644 --- a/promql/quantile.go +++ b/promql/quantile.go @@ -67,11 +67,13 @@ type metricWithBuckets struct { // // If the highest bucket is not +Inf, NaN is returned. // +// If q==NaN, NaN is returned. +// // If q<0, -Inf is returned. // // If q>1, +Inf is returned. func bucketQuantile(q float64, buckets buckets) float64 { - if math.IsNaN(q){ + if math.IsNaN(q) { return math.NaN() } if q < 0 { @@ -184,11 +186,12 @@ func ensureMonotonic(buckets buckets) { // quantile calculates the given quantile of a vector of samples. // // The Vector will be sorted. -// If 'values' has zero elements or 'q' == NaN, NaN is returned. +// If 'values' has zero elements +// If q==NaN, NaN is returned. // If q<0, -Inf is returned. // If q>1, +Inf is returned. func quantile(q float64, values vectorByValueHeap) float64 { - if len(values) == 0 || q == math.NaN() { + if len(values) == 0 || math.IsNaN(q) { return math.NaN() } if q < 0 { diff --git a/promql/testdata/histograms.test b/promql/testdata/histograms.test index 508f1efac..f30c07e7b 100644 --- a/promql/testdata/histograms.test +++ b/promql/testdata/histograms.test @@ -60,6 +60,11 @@ eval instant at 50m histogram_quantile(1.01, testhistogram_bucket) {start="positive"} +Inf {start="negative"} +Inf +# Quantile invalid. +eval instant at 50m histogram_quantile(NaN, testhistogram_bucket) + {start="positive"} NaN + {start="negative"} NaN + # Quantile value in lowest bucket, which is positive. eval instant at 50m histogram_quantile(0, testhistogram_bucket{start="positive"}) {start="positive"} 0 diff --git a/promql/testdata/nan.test b/promql/testdata/nan.test deleted file mode 100644 index a9583ac9a..000000000 --- a/promql/testdata/nan.test +++ /dev/null @@ -1,21 +0,0 @@ -# Two histograms with 4 buckets each (x_sum and x_count not included, -# only buckets). Lowest bucket for one histogram < 0, for the other > -# 0. They have the same name, just separated by label. Not useful in -# practice, but can happen (if clients change bucketing), and the -# server has to cope with it. - -# Test histogram. -load 5m - testhistogram_bucket{le="0.1", start="positive"} 0+5x10 - testhistogram_bucket{le=".2", start="positive"} 0+7x10 - testhistogram_bucket{le="1e0", start="positive"} 0+11x10 - testhistogram_bucket{le="+Inf", start="positive"} 0+12x10 - testhistogram_bucket{le="-.2", start="negative"} 0+1x10 - testhistogram_bucket{le="-0.1", start="negative"} 0+2x10 - testhistogram_bucket{le="0.3", start="negative"} 0+2x10 - testhistogram_bucket{le="+Inf", start="negative"} 0+3x10 - - -# Quantile value in lowest bucket, which is positive. -eval instant at 50m histogram_quantile(NaN, testhistogram_bucket{start="positive"}) - {start="positive"} 0