app/vmselect/prometheus: return timestamps from /api/v1/query, which match the time query arg

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/720
This commit is contained in:
Aliaksandr Valialkin 2020-09-23 12:58:46 +03:00
parent 90d2549428
commit 1fce79518a

View File

@ -730,12 +730,6 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r
if len(query) > maxQueryLen.N { 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) 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 != "" { if childQuery, windowStr, offsetStr := promql.IsMetricSelectorWithRollup(query); childQuery != "" {
window, err := parsePositiveDuration(windowStr, step) window, err := parsePositiveDuration(windowStr, step)
if err != nil { if err != nil {
@ -780,6 +774,16 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r
return nil 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{ ec := promql.EvalConfig{
AuthToken: at, AuthToken: at,
Start: start, Start: start,
@ -795,6 +799,15 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r
if err != nil { if err != nil {
return fmt.Errorf("error when executing query=%q for (time=%d, step=%d): %w", query, start, step, err) 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") w.Header().Set("Content-Type", "application/json")
WriteQueryResponse(w, result) WriteQueryResponse(w, result)