diff --git a/internal/observabilityproxy/appscope.go b/internal/observabilityproxy/appscope.go index 0fe8b05e9..53f8d5582 100644 --- a/internal/observabilityproxy/appscope.go +++ b/internal/observabilityproxy/appscope.go @@ -103,7 +103,7 @@ func applicationSelectors(ns, kind, name string, pods, pvcs []string) []string { // application's series. func (s *Server) applicationScope(ctx context.Context, r *http.Request, store string, req metricsRequest, ns, kind, name string) ([]string, *HTTPError) { if req.last.IsZero() { - return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ anywhere in the query, a backslash outside a string, rollup_candlestick, WITH with a negative offset, an offset that is not a plain duration, or a subquery step that is not one); use the namespace root") + return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ anywhere in the query, a backslash or a comment outside a string, rollup_candlestick, WITH with a negative offset, an offset that is not a plain duration, or a subquery step that is not one); use the namespace root") } lim := s.cfg.Metrics.App if req.start.IsZero() || req.start.Before(req.from.Add(-lim.Lookback)) { diff --git a/internal/observabilityproxy/metricslookback.go b/internal/observabilityproxy/metricslookback.go index 7f936307f..b478f582b 100644 --- a/internal/observabilityproxy/metricslookback.go +++ b/internal/observabilityproxy/metricslookback.go @@ -145,12 +145,12 @@ func queryLookback(query string, step time.Duration) (time.Duration, bool) { // lookback, every "(" counts as a possible implicit subquery and each // term uses the largest step seen. Any @ makes the bound unknown, as it // does for queryLookback, and so does WITH, which can apply one offset -// several times, and a backslash outside a string, which can spell an -// identifier the scan looks for differently. vmselect also evaluates +// several times, and a backslash or a comment outside a string, which can +// hide what the scan looks for. vmselect also evaluates // rollup_candlestick one step ahead of the evaluation time // (evalRollupFuncWithoutAt), so it makes the bound unknown too. func queryLookahead(query string, step time.Duration) (time.Duration, bool) { - if strings.Contains(query, "@") || escapeOutsideString(query) { + if strings.Contains(query, "@") || unscannableOutsideString(query) { return 0, false } lower := strings.ToLower(query) @@ -238,17 +238,20 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) { } } -// escapeOutsideString reports a backslash outside a string literal. -// metricsql unescapes identifiers (unescapeIdent), so rollup\_candlestick -// or offs\et name what the lexical scan looks for without its spelling. -// Inside double or single quotes a backslash escapes the next character; -// inside backquotes it is literal. -func escapeOutsideString(query string) bool { +// unscannableOutsideString reports a backslash or a "#" outside a string +// literal. metricsql unescapes identifiers (unescapeIdent), so +// rollup\_candlestick or offs\et name what the lexical scan looks for +// without its spelling; "#" starts a comment to the end of the line, and +// quotes or brackets inside one would mislead the string tracking here and +// the bracket and offset scans. Inside double or single quotes a backslash +// escapes the next character; inside backquotes it is literal, and a +// backquote escaped there makes metricsql refuse the string anyway. +func unscannableOutsideString(query string) bool { var quote byte for i := 0; i < len(query); i++ { c := query[i] switch { - case quote == 0 && c == '\\': + case quote == 0 && (c == '\\' || c == '#'): return true case quote == 0 && (c == '"' || c == '\'' || c == '`'): quote = c diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index cf452a74f..e201a6fa8 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -501,6 +501,11 @@ func TestQueryLookahead(t *testing.T) { `rollup_candle\stick(x)`, `rollup\x5fcandlestick(x)`, `x offs\et -1h`, + // A comment runs to the end of the line, so quotes in it would + // confuse the string tracking and brackets in it the scans. + "x # '\n+ rollup\\_candlestick(y) # '", + "x # \"\n+ rollup\\_candlestick(y) # \"", + "rate(x[5m]) # comment", // vmselect evaluates rollup_candlestick one step ahead // (evalRollupFuncWithoutAt), whatever the offset. `rollup_candlestick(x)`,