Search code examples
prometheusgrafanapromqlgrafana-variable

Show or hide query result depending on variable value in Grafana


I'm trying to show/hide queries on the graph panel depending on the dashboard variable values.

Motivation.

On the same graph panel (which is used to display several hosts) I have two queries:

rate(ClickHouseProfileEvents_Query{instance=~"$instance"}[$__rate_interval])

And:

max_over_time( (irate(ClickHouseProfileEvents_Query{instance=~"$instance"}[2m]))[$__rate_interval:15s] )

Where ClickHouseProfileEvents_Query is a counter.

The first one is a usual rate that is better for showing trends, and the other one is a little more complex but shows all picks. Of course, I would like to make one panel for the graph but also have some switches like trends/picks to analyze both. I've introduced a grafana dashboard variable with custom values, but I can't understand how I can use it to hide or show a particular query.

Also, I tried to make a single query using things like or or unless but unsuccessfully.

Environment: Grafana v7.5.2, Prometheus v2.26.0

UPDATE:

Thanks to Marcelo Ávila de Oliveira for answer. My current solution is a little bit different but based on his idea.

First, I add type Custom variables with values 1 and null.

enter image description here

Second, I use multiply instead of division in my queries.

enter image description here

And finally, as a result, when I turn off, for example, trends showing, I'm not seeing them both in graph and the legend too.

enter image description here


Solution

    • Create a custom variable with 2 values: 0 and 1

    enter image description here

    • Divide the query you want to control by the variable

    enter image description here

    • When "1" is selected all queries are showed

    enter image description here

    • When "0" is selected the query you've added the division is hided

    enter image description here