diff --git a/internal/observabilityproxy/metricslookback.go b/internal/observabilityproxy/metricslookback.go index 9f01936b7..55e78a0c2 100644 --- a/internal/observabilityproxy/metricslookback.go +++ b/internal/observabilityproxy/metricslookback.go @@ -41,14 +41,20 @@ const latencyOffset = 30 * time.Second // queryDurationPart is the subset of the MetricsQL duration grammar // (scanSingleDuration in metricsql) the scan understands. Step multiples -// (5i, $__interval), negative parts and upper-case units are left out on -// purpose: anything else makes the bound unknown. +// (5i, $__interval) and negative parts are left out on purpose: anything +// else makes the bound unknown. Offsets are scanned lower-cased, as +// metricsql reads units in any case; windows and steps in brackets are +// not, so an upper-case unit there is unknown. var ( queryDurationPart = regexp.MustCompile(`(\d+(?:\.\d+)?)(ms|s|m|h|d|w|y)`) queryDuration = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y))+$`) querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`) ) +// offsetTerminators may follow an offset's duration: whitespace, a +// closing bracket or separator, or a binary operator other than minus. +const offsetTerminators = " \t\n\r)]},+*/^%<>=!" + 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, @@ -212,10 +218,11 @@ 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. - // 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] == '-' { + // metricsql continues a duration token in more forms than the scan + // reads: after a minus (-30m, -1.h, -.5h) and with a trailing + // $__interval, all summed by DurationValue. Only what certainly + // ends the token is accepted after the duration. + if end < len(arg) && !strings.ContainsRune(offsetTerminators, rune(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 75b88d7f3..0fbbcd7a9 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -480,6 +480,10 @@ func TestQueryLookahead(t *testing.T) { `x offset 5m-.5h`, `x offset 1h-1`, `x offset 5m-y`, + // metricsql also reads a trailing $__interval as part of the + // duration: -1h$__interval is -(1h + step). + `x offset -1h$__interval`, + `rate(x[5m]) offset -1h$__interval`, // A subquery step the scan cannot read, an unclosed bracket, and a // sum past the duration range. `rate(x[1h:5i])`, diff --git a/internal/observabilityproxy/server_app_test.go b/internal/observabilityproxy/server_app_test.go index c451d35af..8b3a4e587 100644 --- a/internal/observabilityproxy/server_app_test.go +++ b/internal/observabilityproxy/server_app_test.go @@ -523,6 +523,7 @@ func TestServerApplicationRefusesForwardReads(t *testing.T) { for _, query := range []string{ "container_cpu_usage_seconds_total offset -1h-30m", "container_cpu_usage_seconds_total offset 1h-2h", + "container_cpu_usage_seconds_total offset -1h$__interval", "rollup_candlestick(container_cpu_usage_seconds_total)", } { q := url.Values{"query": {query}, "time": {unix(testNow.Add(-2 * time.Hour))}}