diff --git a/app/vmalert/datasource/vm_prom_api.go b/app/vmalert/datasource/vm_prom_api.go index 47a34e9e64..3bf358a24e 100644 --- a/app/vmalert/datasource/vm_prom_api.go +++ b/app/vmalert/datasource/vm_prom_api.go @@ -159,13 +159,15 @@ func (s *VMStorage) setPrometheusReqParams(r *http.Request, query string) { } } q.Set("query", query) - if s.evaluationInterval > 0 { - // set step as evaluationInterval by default - q.Set("step", s.evaluationInterval.String()) + if s.evaluationInterval > 0 { // set step as evaluationInterval by default + // always convert to seconds to keep compatibility with older + // Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943 + q.Set("step", fmt.Sprintf("%ds", int(s.evaluationInterval.Seconds()))) } - if s.queryStep > 0 { - // override step with user-specified value - q.Set("step", s.queryStep.String()) + if s.queryStep > 0 { // override step with user-specified value + // always convert to seconds to keep compatibility with older + // 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() } diff --git a/app/vmalert/datasource/vm_test.go b/app/vmalert/datasource/vm_test.go index 068cb62776..81e12b2f69 100644 --- a/app/vmalert/datasource/vm_test.go +++ b/app/vmalert/datasource/vm_test.go @@ -436,7 +436,20 @@ func TestRequestParams(t *testing.T) { queryStep: time.Minute, }, 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) }, },