diff --git a/internal/observabilityproxy/metricsparams.go b/internal/observabilityproxy/metricsparams.go index f02620b3c..c40bc058f 100644 --- a/internal/observabilityproxy/metricsparams.go +++ b/internal/observabilityproxy/metricsparams.go @@ -134,8 +134,11 @@ func BuildMetricsParams(endpoint string, in url.Values, namespace string, now ti return req, herr } at = t - out.Set("time", formatTime(t)) } + // The default is forwarded too: the application lookup is sized + // for at, and vmselect's own clock reads later, by as long as the + // lookups took. + out.Set("time", formatTime(at)) step := instantDefaultStep if v := in.Get("step"); v != "" { var herr *HTTPError diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index 2cc22a4ae..ba14a9781 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -227,7 +227,7 @@ func TestBuildMetricsParamsCanonicalForms(t *testing.T) { if req.params.Get("end") != "2026-11-04T12:00:00.000Z" || req.params.Get("start") != "2026-11-04T11:55:00.000Z" { t.Errorf("labels window = %s..%s, want the last 5m", req.params.Get("start"), req.params.Get("end")) } - if q := buildMetrics(t, "api/v1/query", url.Values{"query": {"up"}}); q.params.Has("time") || !q.start.Equal(testNow.Add(-15*time.Minute-2*latencyOffset)) { + if q := buildMetrics(t, "api/v1/query", url.Values{"query": {"up"}}); q.params.Get("time") != "2026-11-04T12:00:00.000Z" || !q.start.Equal(testNow.Add(-15*time.Minute-2*latencyOffset)) { t.Errorf("instant query without time: params %v, start %v", q.params, q.start) } } diff --git a/internal/observabilityproxy/server_app_test.go b/internal/observabilityproxy/server_app_test.go index ade81ca04..7afaa3618 100644 --- a/internal/observabilityproxy/server_app_test.go +++ b/internal/observabilityproxy/server_app_test.go @@ -363,6 +363,27 @@ func TestServerApplicationLookupCoversNegativeOffset(t *testing.T) { } } +// TestServerApplicationForwardsDefaultTime: without time the lookup is +// sized at the proxy's clock, so that instant is forwarded. Otherwise +// vmselect evaluates at its own later clock, past the end of the lookup by +// however long the lookups took, and leaves out pods first sampled since. +func TestServerApplicationForwardsDefaultTime(t *testing.T) { + h := newPolicyHarness(t, Config{}, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil) + q := url.Values{"query": {"container_cpu_usage_seconds_total"}} + if got := h.status(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+q.Encode(), "good"); got != http.StatusOK { + t.Fatalf("status = %d", got) + } + reqs := h.requests() + lookup, final := reqs[0], reqs[len(reqs)-1] + want := formatTime(testNow) + if lookup.path != "/api/v1/label/pod/values" || lookup.form.Get("end") != want { + t.Fatalf("lookup %s end = %s, want %s", lookup.path, lookup.form.Get("end"), want) + } + if final.path != "/api/v1/query" || final.form.Get("time") != want { + t.Fatalf("query %s time = %q, want the lookup's instant %s", final.path, final.form.Get("time"), want) + } +} + // TestServerApplicationRefusesUnboundedSampleTime refuses a query whose // samples may lie at a time the lookup cannot be sized for, rather than // answer with series silently left out.