Fix timestamp() function for @ modifier

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>
This commit is contained in:
Ganesh Vernekar 2021-02-03 19:13:12 +05:30
parent 41247b6950
commit b18fde996e
No known key found for this signature in database
GPG Key ID: 0241A11211763456
2 changed files with 36 additions and 0 deletions

View File

@ -1125,6 +1125,11 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
vs, ok := arg.(*parser.VectorSelector) vs, ok := arg.(*parser.VectorSelector)
if ok { if ok {
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) (Vector, storage.Warnings) { 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) val, ws := ev.vectorSelector(vs, enh.Ts)
return call([]parser.Value{val}, e.Args, enh), ws return call([]parser.Value{val}, e.Args, enh), ws
}) })

View File

@ -907,6 +907,11 @@ load 1ms
for ts := int64(-1000000 + 1000); ts <= 0; ts += 1000 { for ts := int64(-1000000 + 1000); ts <= 0; ts += 1000 {
require.NoError(t, app.AddFast(ref, ts, -float64(ts/1000)+1)) 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()) require.NoError(t, app.Commit())
cases := []struct { cases := []struct {
@ -1033,6 +1038,26 @@ load 1ms
Metric: lblstopk2, 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) { func TestRecoverEvaluatorRuntime(t *testing.T) {
ev := &evaluator{logger: log.NewNopLogger()} 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) {
}