From b18fde996e29062b62bc033e8db3b3edc4887d04 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar Date: Wed, 3 Feb 2021 19:13:12 +0530 Subject: [PATCH] Fix timestamp() function for @ modifier Signed-off-by: Ganesh Vernekar --- promql/engine.go | 5 +++++ promql/engine_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/promql/engine.go b/promql/engine.go index 4486313e4..2a90b4805 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1125,6 +1125,11 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) { vs, ok := arg.(*parser.VectorSelector) if ok { return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) (Vector, storage.Warnings) { + if vs.Timestamp != nil { + // This is a special case only for "timestamp" since the offset + // needs to be adjusted for every point. + vs.Offset = time.Duration(enh.Ts-*vs.Timestamp) * time.Millisecond + } val, ws := ev.vectorSelector(vs, enh.Ts) return call([]parser.Value{val}, e.Args, enh), ws }) diff --git a/promql/engine_test.go b/promql/engine_test.go index 52ae66bc1..a3ba0f2b0 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -907,6 +907,11 @@ load 1ms for ts := int64(-1000000 + 1000); ts <= 0; ts += 1000 { require.NoError(t, app.AddFast(ref, ts, -float64(ts/1000)+1)) } + + // To test the fix for https://github.com/prometheus/prometheus/issues/8433. + _, err = app.Add(labels.FromStrings("__name__", "metric_timestamp"), 3600*1000, 1000) + require.NoError(t, err) + require.NoError(t, app.Commit()) cases := []struct { @@ -1033,6 +1038,26 @@ load 1ms Metric: lblstopk2, }, }, + }, { + // Tests for https://github.com/prometheus/prometheus/issues/8433. + // The trick here is that the query range should be > lookback delta. + query: `timestamp(metric_timestamp @ 3600)`, + start: 0, end: 7 * 60, interval: 60, + result: Matrix{ + Series{ + Points: []Point{ + {V: 3600, T: 0}, + {V: 3600, T: 60 * 1000}, + {V: 3600, T: 2 * 60 * 1000}, + {V: 3600, T: 3 * 60 * 1000}, + {V: 3600, T: 4 * 60 * 1000}, + {V: 3600, T: 5 * 60 * 1000}, + {V: 3600, T: 6 * 60 * 1000}, + {V: 3600, T: 7 * 60 * 1000}, + }, + Metric: labels.Labels{}, + }, + }, }, } @@ -1061,6 +1086,7 @@ load 1ms }) } } + func TestRecoverEvaluatorRuntime(t *testing.T) { ev := &evaluator{logger: log.NewNopLogger()} @@ -2124,3 +2150,8 @@ func TestEngineOptsValidation(t *testing.T) { } } } + +// TestFuncTimestampWithAtModifier tests for https://github.com/prometheus/prometheus/issues/8433. +func TestFuncTimestampWithAtModifier(t *testing.T) { + +}