diff --git a/internal/observabilityproxy/metricsparams.go b/internal/observabilityproxy/metricsparams.go index 5aae58479..44761a2ea 100644 --- a/internal/observabilityproxy/metricsparams.go +++ b/internal/observabilityproxy/metricsparams.go @@ -264,7 +264,8 @@ func parseMetricsDuration(key, v string) (time.Duration, *HTTPError) { // parseMetricsTime takes the two forms the Prometheus API defines: float // unix seconds and RFC3339. vmselect would also guess milliseconds from the // digit count and accept relative times; a guessed unit silently moves the -// window, so those are refused. +// window, so those are refused. The result is cut to the millisecond, as +// formatTime sends it, so the limits see the window vmselect will use. func parseMetricsTime(key, v string) (time.Time, *HTTPError) { if sec, frac, ok := strings.Cut(v, "."); isDigits(sec) && (!ok || isDigits(frac)) { if len(sec) > 10 { @@ -275,13 +276,13 @@ func parseMetricsTime(key, v string) (time.Time, *HTTPError) { if ok { nanos, _ = strconv.ParseInt((frac + "000000000")[:9], 10, 64) } - return time.Unix(secs, nanos).UTC(), nil + return time.UnixMilli(time.Unix(secs, nanos).UnixMilli()).UTC(), nil } t, err := time.Parse(time.RFC3339Nano, v) if err != nil || t.Before(time.Unix(0, 0)) { return time.Time{}, errorf(http.StatusBadRequest, "invalid %s %q", key, v) } - return t.UTC(), nil + return time.UnixMilli(t.UnixMilli()).UTC(), nil } func formatTime(t time.Time) string { diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index 4a8565564..729af997a 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -156,6 +156,9 @@ func TestBuildMetricsParamsLimits(t *testing.T) { {"points at the limit", "api/v1/query_range", rng(ago(10999*time.Minute), ago(0), "1m"), http.StatusOK}, {"points above the limit", "api/v1/query_range", rng(ago(11000*time.Minute), ago(0), "1m"), http.StatusUnprocessableEntity}, {"points above the limit after rounding the step up", "api/v1/query_range", rng(ago(22*time.Second), ago(0), "0.0019"), http.StatusUnprocessableEntity}, + // 10.9999s admits 11000 points, but vmselect gets both bounds cut to + // the millisecond: 11.000s, 11001 points. + {"points above the limit once the bounds are cut to milliseconds", "api/v1/query_range", rng("2026-11-04T11:59:49.0009Z", "2026-11-04T12:00:00.0008Z", "0.001"), http.StatusUnprocessableEntity}, {"overflowing step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "2562047h47m16.854775807s"), http.StatusBadRequest}, {"overflowing timeout", "api/v1/query", url.Values{"query": {"up"}, "timeout": {"2562047h47m16.854775807s"}}, http.StatusBadRequest}, {"step above the range limit", "api/v1/query_range", rng(ago(time.Hour), ago(0), "31d"), http.StatusUnprocessableEntity},