From c6cbc0bd192118a9d6380fa56d83c2708bce5231 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 29 Mar 2020 21:56:37 +0300 Subject: [PATCH] app/vmselect/prometheus: allow passing relative time to `start`, `end` and `time` args of `/api/v1/*` queries --- app/vmselect/prometheus/prometheus.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index e1c6289262..3c61bdc986 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -923,7 +923,15 @@ func getTime(r *http.Request, argKey string, defaultValue int64) (int64, error) case prometheusMaxTimeFormatted: return maxTimeMsecs, nil } - return 0, fmt.Errorf("cannot parse %q=%q: %s", argKey, argValue, err) + // Try parsing duration relative to the current time + d, err1 := time.ParseDuration(argValue) + if err1 != nil { + return 0, fmt.Errorf("cannot parse %q=%q: %s", argKey, argValue, err) + } + if d > 0 { + d = -d + } + t = time.Now().Add(d) } secs = float64(t.UnixNano()) / 1e9 }