From b28c9a394400c458bff4b9f8dfffc9b7456826a4 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 28 Jan 2020 16:29:48 +0200 Subject: [PATCH] app/vmselect/promql: return expected results from `increase()` over the beginning of time series, which start from big value Examples for such counters: OS-level counters for network or cpu stats. --- app/vmselect/promql/rollup.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go index 563f9dffed..0175b734cc 100644 --- a/app/vmselect/promql/rollup.go +++ b/app/vmselect/promql/rollup.go @@ -1157,10 +1157,18 @@ func rollupDeltaInternal(rfa *rollupFuncArg, canUseRealPrevValue bool) float64 { if len(values) == 0 { return nan } - // Assume that the previous non-existing value was 0. - prevValue = 0 - if canUseRealPrevValue && !math.IsNaN(rfa.prevValue) { - prevValue = rfa.prevValue + // Assume that the previous non-existing value was 0 + // only if the first value is quite small. + // This should prevent from improper increase() results for os-level counters + // such as cpu time or bytes sent over the network interface. + // These counters may start long ago before the first value appears in the db. + if values[0] < 1e6 { + prevValue = 0 + if canUseRealPrevValue && !math.IsNaN(rfa.realPrevValue) { + prevValue = rfa.realPrevValue + } + } else { + prevValue = values[0] } } if len(values) == 0 {