From 82cf9f26db6df9528e4cd65b2f171a525486d058 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 12 Nov 2021 13:50:43 +0200 Subject: [PATCH] app/vmselect/promql: prevent from incorrect calculations for `deriv()` over multiple samples with identical timestamps --- app/vmselect/promql/rollup.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go index d04b3d967..7cc29f7ed 100644 --- a/app/vmselect/promql/rollup.go +++ b/app/vmselect/promql/rollup.go @@ -875,7 +875,12 @@ func linearRegression(rfa *rollupFuncArg) (float64, float64) { tvSum += dt * v ttSum += dt * dt } - k := (tvSum - tSum*vSum/n) / (ttSum - tSum*tSum/n) + k := float64(0) + tDiff := ttSum - tSum*tSum/n + if math.Abs(tDiff) >= 1e-6 { + // Prevent from incorrect division for too small tDiff values. + k = (tvSum - tSum*vSum/n) / tDiff + } v := vSum/n - k*tSum/n return v, k }