Skip to content

Commit b18fde9

Browse files
author
Ganesh Vernekar
committed
Fix timestamp() function for @ modifier
Signed-off-by: Ganesh Vernekar <[email protected]>
1 parent 41247b6 commit b18fde9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

promql/engine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,11 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
11251125
vs, ok := arg.(*parser.VectorSelector)
11261126
if ok {
11271127
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) (Vector, storage.Warnings) {
1128+
if vs.Timestamp != nil {
1129+
// This is a special case only for "timestamp" since the offset
1130+
// needs to be adjusted for every point.
1131+
vs.Offset = time.Duration(enh.Ts-*vs.Timestamp) * time.Millisecond
1132+
}
11281133
val, ws := ev.vectorSelector(vs, enh.Ts)
11291134
return call([]parser.Value{val}, e.Args, enh), ws
11301135
})

promql/engine_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,11 @@ load 1ms
907907
for ts := int64(-1000000 + 1000); ts <= 0; ts += 1000 {
908908
require.NoError(t, app.AddFast(ref, ts, -float64(ts/1000)+1))
909909
}
910+
911+
// To test the fix for https://github.com/prometheus/prometheus/issues/8433.
912+
_, err = app.Add(labels.FromStrings("__name__", "metric_timestamp"), 3600*1000, 1000)
913+
require.NoError(t, err)
914+
910915
require.NoError(t, app.Commit())
911916

912917
cases := []struct {
@@ -1033,6 +1038,26 @@ load 1ms
10331038
Metric: lblstopk2,
10341039
},
10351040
},
1041+
}, {
1042+
// Tests for https://github.com/prometheus/prometheus/issues/8433.
1043+
// The trick here is that the query range should be > lookback delta.
1044+
query: `timestamp(metric_timestamp @ 3600)`,
1045+
start: 0, end: 7 * 60, interval: 60,
1046+
result: Matrix{
1047+
Series{
1048+
Points: []Point{
1049+
{V: 3600, T: 0},
1050+
{V: 3600, T: 60 * 1000},
1051+
{V: 3600, T: 2 * 60 * 1000},
1052+
{V: 3600, T: 3 * 60 * 1000},
1053+
{V: 3600, T: 4 * 60 * 1000},
1054+
{V: 3600, T: 5 * 60 * 1000},
1055+
{V: 3600, T: 6 * 60 * 1000},
1056+
{V: 3600, T: 7 * 60 * 1000},
1057+
},
1058+
Metric: labels.Labels{},
1059+
},
1060+
},
10361061
},
10371062
}
10381063

@@ -1061,6 +1086,7 @@ load 1ms
10611086
})
10621087
}
10631088
}
1089+
10641090
func TestRecoverEvaluatorRuntime(t *testing.T) {
10651091
ev := &evaluator{logger: log.NewNopLogger()}
10661092

@@ -2124,3 +2150,8 @@ func TestEngineOptsValidation(t *testing.T) {
21242150
}
21252151
}
21262152
}
2153+
2154+
// TestFuncTimestampWithAtModifier tests for https://github.com/prometheus/prometheus/issues/8433.
2155+
func TestFuncTimestampWithAtModifier(t *testing.T) {
2156+
2157+
}

0 commit comments

Comments
 (0)