From 1fce79518a58956c8cc8d62654907edbefe53a04 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 23 Sep 2020 12:58:46 +0300 Subject: [PATCH] app/vmselect/prometheus: return timestamps from `/api/v1/query`, which match the `time` query arg Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/720 --- app/vmselect/prometheus/prometheus.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index ca2adc3fc7..77530ae1f2 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -730,12 +730,6 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r if len(query) > maxQueryLen.N { return fmt.Errorf("too long query; got %d bytes; mustn't exceed `-search.maxQueryLen=%d` bytes", len(query), maxQueryLen.N) } - queryOffset := getLatencyOffsetMilliseconds() - if !searchutils.GetBool(r, "nocache") && ct-start < queryOffset { - // Adjust start time only if `nocache` arg isn't set. - // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/241 - start = ct - queryOffset - } if childQuery, windowStr, offsetStr := promql.IsMetricSelectorWithRollup(query); childQuery != "" { window, err := parsePositiveDuration(windowStr, step) if err != nil { @@ -780,6 +774,16 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r return nil } + queryOffset := getLatencyOffsetMilliseconds() + if !searchutils.GetBool(r, "nocache") && ct-start < queryOffset && start-ct < queryOffset { + // Adjust start time only if `nocache` arg isn't set. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/241 + startPrev := start + start = ct - queryOffset + queryOffset = startPrev - start + } else { + queryOffset = 0 + } ec := promql.EvalConfig{ AuthToken: at, Start: start, @@ -795,6 +799,15 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r if err != nil { return fmt.Errorf("error when executing query=%q for (time=%d, step=%d): %w", query, start, step, err) } + if queryOffset > 0 { + for i := range result { + rs := &result[i] + timestamps := rs.Timestamps + for j := range timestamps { + timestamps[j] += queryOffset + } + } + } w.Header().Set("Content-Type", "application/json") WriteQueryResponse(w, result)