From 7dc632719dde71b99d47b360c1fa0f632f3f765b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 2 Sep 2022 23:18:01 +0300 Subject: [PATCH] app/vmselect/promql: consistently calculate `rate_over_sum(m[d])` as `sum_over_time(m[d])/d` Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3045 --- app/vmselect/promql/rollup.go | 6 +----- app/vmselect/promql/rollup_test.go | 2 +- docs/CHANGELOG.md | 2 ++ 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go index 57f0cd62c..be6ade111 100644 --- a/app/vmselect/promql/rollup.go +++ b/app/vmselect/promql/rollup.go @@ -1349,15 +1349,11 @@ func rollupRateOverSum(rfa *rollupFuncArg) float64 { // Assume that the value didn't change since rfa.prevValue. return 0 } - dt := rfa.window - if !math.IsNaN(rfa.prevValue) { - dt = timestamps[len(timestamps)-1] - rfa.prevTimestamp - } sum := float64(0) for _, v := range rfa.values { sum += v } - return sum / (float64(dt) / 1e3) + return sum / (float64(rfa.window) / 1e3) } func rollupRange(rfa *rollupFuncArg) float64 { diff --git a/app/vmselect/promql/rollup_test.go b/app/vmselect/promql/rollup_test.go index fa589a0b1..ae3362814 100644 --- a/app/vmselect/promql/rollup_test.go +++ b/app/vmselect/promql/rollup_test.go @@ -1307,7 +1307,7 @@ func TestRollupFuncsNoWindow(t *testing.T) { if samplesScanned == 0 { t.Fatalf("expecting non-zero samplesScanned from rollupConfig.Do") } - valuesExpected := []float64{nan, 2775, 5262.5, 3678.5714285714284, 2880} + valuesExpected := []float64{nan, 2775, 5262.5, 3862.5, 1800} timestampsExpected := []int64{0, 40, 80, 120, 160} testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected) }) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 84785fbae..3a86a3d7f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f ## tip +* BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): properly calculate `rate_over_sum(m[d])` as `sum_over_time(m[d])/d`. Previously the `sum_over_time(m[d])` could be improperly divided by smaller than `d` time range. See [rate_over_sum() docs](https://docs.victoriametrics.com/MetricsQL.html#rate_over_sum) and [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3045). + ## [v1.81.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.81.1)