vmalert: always convert step value to seconds for better compatibility (#1955)

When using `vmalert` with older Prometheus versions, the passed
`step=2m` may be parsed by Prometheus with an err: "cannot parse \"2m0s\" to a valid duration".
In order to improve compatibility vmalert will always convert step duration to seconds.

https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
Signed-off-by: hagen1778 <roman@victoriametrics.com>
This commit is contained in:
Roman Khavronenko 2021-12-17 15:26:34 +02:00 committed by Aliaksandr Valialkin
parent f22aab411b
commit 1f0301c809
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
2 changed files with 22 additions and 7 deletions

View File

@ -159,13 +159,15 @@ func (s *VMStorage) setPrometheusReqParams(r *http.Request, query string) {
} }
} }
q.Set("query", query) q.Set("query", query)
if s.evaluationInterval > 0 { if s.evaluationInterval > 0 { // set step as evaluationInterval by default
// set step as evaluationInterval by default // always convert to seconds to keep compatibility with older
q.Set("step", s.evaluationInterval.String()) // Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
q.Set("step", fmt.Sprintf("%ds", int(s.evaluationInterval.Seconds())))
} }
if s.queryStep > 0 { if s.queryStep > 0 { // override step with user-specified value
// override step with user-specified value // always convert to seconds to keep compatibility with older
q.Set("step", s.queryStep.String()) // Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
q.Set("step", fmt.Sprintf("%ds", int(s.queryStep.Seconds())))
} }
r.URL.RawQuery = q.Encode() r.URL.RawQuery = q.Encode()
} }

View File

@ -436,7 +436,20 @@ func TestRequestParams(t *testing.T) {
queryStep: time.Minute, queryStep: time.Minute,
}, },
func(t *testing.T, r *http.Request) { func(t *testing.T, r *http.Request) {
exp := fmt.Sprintf("query=%s&step=%v&time=%d", query, time.Minute, timestamp.Unix()) exp := fmt.Sprintf("query=%s&step=%ds&time=%d", query, int(time.Minute.Seconds()), timestamp.Unix())
checkEqualString(t, exp, r.URL.RawQuery)
},
},
{
"step to seconds",
false,
&VMStorage{
evaluationInterval: 3 * time.Hour,
},
func(t *testing.T, r *http.Request) {
evalInterval := 3 * time.Hour
tt := timestamp.Truncate(evalInterval)
exp := fmt.Sprintf("query=%s&step=%ds&time=%d", query, int(evalInterval.Seconds()), tt.Unix())
checkEqualString(t, exp, r.URL.RawQuery) checkEqualString(t, exp, r.URL.RawQuery)
}, },
}, },