From aadec25faf7cef145dfab029767424383f51bd6d Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Tue, 6 Aug 2024 18:10:40 +1000 Subject: [PATCH] promql: Fix issue where some native histogram-related annotations are not emitted by `rate` (#14575) Signed-off-by: Charles Korn --- promql/functions.go | 11 +++++++++-- promql/promqltest/testdata/native_histograms.test | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/promql/functions.go b/promql/functions.go index dcc2cd759..b9e93b85a 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -97,9 +97,10 @@ func extrapolatedRate(vals []parser.Value, args parser.Expressions, enh *EvalNod lastT = samples.Histograms[numSamplesMinusOne].T var newAnnos annotations.Annotations resultHistogram, newAnnos = histogramRate(samples.Histograms, isCounter, metricName, args[0].PositionRange()) + annos.Merge(newAnnos) if resultHistogram == nil { // The histograms are not compatible with each other. - return enh.Out, annos.Merge(newAnnos) + return enh.Out, annos } case len(samples.Floats) > 1: numSamplesMinusOne = len(samples.Floats) - 1 @@ -189,6 +190,12 @@ func histogramRate(points []HPoint, isCounter bool, metricName string, pos posra var annos annotations.Annotations + // We check for gauge type histograms in the loop below, but the loop below does not run on the first and last point, + // so check the first and last point now. + if isCounter && (prev.CounterResetHint == histogram.GaugeType || last.CounterResetHint == histogram.GaugeType) { + annos.Add(annotations.NewNativeHistogramNotCounterWarning(metricName, pos)) + } + // First iteration to find out two things: // - What's the smallest relevant schema? // - Are all data points histograms? @@ -241,7 +248,7 @@ func histogramRate(points []HPoint, isCounter bool, metricName string, pos posra } h.CounterResetHint = histogram.GaugeType - return h.Compact(0), nil + return h.Compact(0), annos } // === delta(Matrix parser.ValueTypeMatrix) (Vector, Annotations) === diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index 6a8189a54..034d73eb5 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -748,3 +748,17 @@ eval instant at 5m histogram_quantile(0.5, custom_buckets_histogram) eval instant at 5m sum(custom_buckets_histogram) {} {{schema:-53 sum:5 count:4 custom_values:[5 10] buckets:[1 2 1]}} + +clear + +# Test 'this native histogram metric is not a gauge' warning for rate +load 30s + some_metric {{schema:0 sum:1 count:1 buckets:[1] counter_reset_hint:gauge}} {{schema:0 sum:2 count:2 buckets:[2] counter_reset_hint:gauge}} {{schema:0 sum:3 count:3 buckets:[3] counter_reset_hint:gauge}} + +# Test the case where we only have two points for rate +eval_warn instant at 30s rate(some_metric[30s]) + {} {{count:0.03333333333333333 sum:0.03333333333333333 buckets:[0.03333333333333333]}} + +# Test the case where we have more than two points for rate +eval_warn instant at 1m rate(some_metric[1m]) + {} {{count:0.03333333333333333 sum:0.03333333333333333 buckets:[0.03333333333333333]}}