From b876368a3fd057debcb8147fa96a1cb00d594522 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Thu, 5 Nov 2026 06:16:24 +0700 Subject: [PATCH] fix(observability-proxy): refuse any minus right after an offset again Telling a compound duration from a subtraction by a duration-part pattern missed forms metricsql accepts after the minus: offset 5m-1.h is 55 minutes forward and offset 5m-.5h is 25, yet both read as five minutes back, and the lookup silently missed pods created in between. A pattern that followed scanSingleDuration exactly would still be a second copy of the grammar to keep in step, so any minus right after an offset's duration is refused again; offset 1h-1 and offset 5m-y now answer 422 as well, and metricsql cannot parse the latter anyway. Assisted-by: LLM Signed-off-by: ZverGuy --- internal/observabilityproxy/metricslookback.go | 10 ++++------ internal/observabilityproxy/metricsparams_test.go | 10 +++++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/observabilityproxy/metricslookback.go b/internal/observabilityproxy/metricslookback.go index 16f1f4cff..9f01936b7 100644 --- a/internal/observabilityproxy/metricslookback.go +++ b/internal/observabilityproxy/metricslookback.go @@ -49,10 +49,6 @@ var ( querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`) ) -// negativeDurationPart is what metricsql's scanSingleDuration accepts -// after a minus inside one duration token. -var negativeDurationPart = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y|i)|\$__interval)`) - var queryDurationUnits = map[string]time.Duration{ "ms": time.Millisecond, "s": time.Second, "m": time.Minute, "h": time.Hour, "d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "y": 365 * 24 * time.Hour, @@ -216,8 +212,10 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) { // metricsql lexes 1h-30m as one duration whose parts after the // first negative one all subtract (DurationValue), so -1h-30m is // -90m and 1h-2h is -1h; the scan does not read such compounds. - // A minus not followed by a duration part is an operator. - if end < len(arg) && arg[end] == '-' && negativeDurationPart.MatchString(arg[end+1:]) { + // metricsql continues a duration after a minus in more forms than + // the scan can tell from a subtraction (-1.h and -.5h among them), + // so any minus right after the duration is refused. + if end < len(arg) && arg[end] == '-' { return 0, 0, false } d, ok := parseQueryDuration(arg[:end]) diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index 26db536cf..15067ed6c 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -450,9 +450,6 @@ func TestQueryLookahead(t *testing.T) { {`x offset 1h - 30m`, 0}, {`x offset -1h + 30m`, time.Hour}, {`x offset -1h30m`, 90 * time.Minute}, - // A minus not followed by a duration is an operator. - {`x offset 1h-1`, 0}, - {`x offset 5m-y`, 0}, } for _, tt := range tests { t.Run(tt.query, func(t *testing.T) { @@ -477,6 +474,12 @@ func TestQueryLookahead(t *testing.T) { `x offset - 1h-30m`, `x offset 1h-2h`, `x offset 1h-30m`, + // metricsql continues a duration with -1.h or -.5h too, so any minus + // right after the duration is refused rather than told apart. + `x offset 5m-1.h`, + `x offset 5m-.5h`, + `x offset 1h-1`, + `x offset 5m-y`, `rate(x[5m] offset 1h-30m)`, // vmselect evaluates rollup_candlestick one step ahead // (evalRollupFuncWithoutAt), whatever the offset. @@ -498,6 +501,7 @@ func TestQueryLookahead(t *testing.T) { // values back onto earlier points, so a series that starts after the // evaluation time can reach the answer. func TestQueryLookaheadSubquery(t *testing.T) { + for _, q := range []string{`max_over_time(range_last(x)[1h:1h])`, `max_over_time(keep_next_value(x)[1h:1h])`} { got, ok := queryLookahead(q, time.Minute) if !ok || got < 2*time.Hour {