Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize empty filter queries #1792

Closed
slim-bean opened this issue Mar 10, 2020 · 3 comments · Fixed by #1810
Closed

Optimize empty filter queries #1792

slim-bean opened this issue Mar 10, 2020 · 3 comments · Fixed by #1810
Assignees
Labels
component/loki type/enhancement Something existing could be improved

Comments

@slim-bean
Copy link
Collaborator

If a query like:

{label="foo"} |= "" or {label="foo"} |~ "" these should be optimized to {label="foo"}

You might ask why would someone do this? When creating a dashboard with a log panel you might want to create a text variable on the dashboard which could be used in the filter expression like so:

{label="foo"} |~ "$searchVar"

If the user leaves this filter blank it ends up sending the query as {label="foo"} |~ ""

@cyriltovena cyriltovena added component/loki type/enhancement Something existing could be improved labels Mar 11, 2020
@cyriltovena
Copy link
Contributor

We should also support {label="foo"} |~ ".*"

cyriltovena added a commit to cyriltovena/loki that referenced this issue Mar 16, 2020
…oesn't contains a filter.

The query is not really rewritten and the stringer func still returns the correct original query.
However the `Filter` func of the `Expr` returns nil which what is being checked when splitting queries in the frontend and also what is being used in the memchunk.

see https://github.com/grafana/loki/blob/master/pkg/querier/queryrange/roundtrip.go#L81 and https://github.com/grafana/loki/blob/master/pkg/chunkenc/memchunk.go#L614

Fixes grafana#1792

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
owen-d pushed a commit that referenced this issue Mar 17, 2020
* Optimize filter queries such as `{label="foo"} |~ "" |~ ".*" |= ""` doesn't contains a filter.

The query is not really rewritten and the stringer func still returns the correct original query.
However the `Filter` func of the `Expr` returns nil which what is being checked when splitting queries in the frontend and also what is being used in the memchunk.

see https://github.com/grafana/loki/blob/master/pkg/querier/queryrange/roundtrip.go#L81 and https://github.com/grafana/loki/blob/master/pkg/chunkenc/memchunk.go#L614

Fixes #1792

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Add missing tests with combination of empty or regex.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Review feedback.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
@broferek
Copy link

broferek commented Oct 19, 2021

Let's say you have this
{filename="foo.log",platform="$platform",host=~"$host"} |= "ERROR" | pattern "<date> [<thread>][<level>] <class> - <stacktrace>"| $search
And you want to filter your search on the labels created by pattern such as class="bar"

It only works when the search is not empty but when you enter in the Grafana dashboard, it is empty and the query fails.

@slim-bean is there any way to get it working ?

@gabzim
Copy link

gabzim commented May 14, 2023

It only works when the search is not empty but when you enter in the Grafana dashboard, it is empty and the query fails.

I've been struggling with this. I use a | json | field=$var and I need a way to say if $var is empty, don't filter at all, otherwise do filter on the label "field" that was added by the json step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/loki type/enhancement Something existing could be improved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants