Skip to content

fix: while validating logql expression, detect and validate expression in label_replace - #18470

Merged
sandeepsukhani merged 2 commits into
mainfrom
validate-label-replace-expression
Jul 16, 2025
Merged

fix: while validating logql expression, detect and validate expression in label_replace#18470
sandeepsukhani merged 2 commits into
mainfrom
validate-label-replace-expression

Conversation

@sandeepsukhani

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:
There was a panic due to an invalid query, which looked something like this:
sum(label_replace(count_over_time({container="query-frontend"} |= "metrics.go" | logfmt[1m]) or 0, "tenant", "$1", "org_id", "(.*)")) by (tenant)

Notice the or 0 in the binary operation, which is invalid and results in setting an error in the initialised BinOpExpr.

Now, we validate the parsed logQL query to detect any such errors and return them to the user.
Some expressions have specific validation logic, while for the remaining, we default to calling the Selector method on that expression to see if there is an issue.
The thing to note is that we do not have specific validation logic for label_replace. Which means, when the above expression is being validated, we not only miss running the validation logic for binary operation, but we also end up returning a panic here since BinOpExpr does not implement Selector method and relies on embedded SampleExpr, which would be nil when there is invalid binary expression like above.

@sandeepsukhani
sandeepsukhani requested a review from a team as a code owner July 16, 2025 08:23
@sandeepsukhani
sandeepsukhani enabled auto-merge (squash) July 16, 2025 09:03
@sandeepsukhani
sandeepsukhani merged commit d379de5 into main Jul 16, 2025
114 of 116 checks passed
@sandeepsukhani
sandeepsukhani deleted the validate-label-replace-expression branch July 16, 2025 10:45
felix0102 pushed a commit to felix0102/loki that referenced this pull request Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants