user13375075
user13375075

Reputation:

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

Upvotes: 7

Views: 16932

Answers (3)

Aman Jain
Aman Jain

Reputation: 3045

In addition to Marcelo's answer, instead of dividing by zero. There is a binary operator or and and in Prometheus, that will act like a disable/enable query button.

OR -> union

AND -> intersection

So, when we apply OR operator between a real query and some string it will query as it is while with AND operator it will take intersection with some string, which will result in no result.

Use Grafana Version 7.3.0 or above.

Define custom variable with or and and custom values.

show : or represent the label : value for variable

enter image description here

Add $variable_name as suffix or prefix to the query along with one custom string. enter image description here

When show, it will show the graph along with the legends. In the below picture look for sum in the right table and the graph for sum legend query. enter image description here

When hide, it will hide the graph and legend text completely. In the below picture sum is hidden from the right table and the graph corresponding to it. enter image description here

Upvotes: 6

Perry45
Perry45

Reputation: 86

I just want to point out that you can even filter multiple queries with one variable, for example if you have 3 queries:

query1 * ($var % 2)
query2 * ($var % 3)
query3 * ($var % 5)

Value for $var ALL: 1
Value for $var only select query1 : 15
Value for $var only select query2 : 10
Value for $var only select query3 : 6

Works for infinite queries but the number gets bigger (for 4 queries with also % 7: 105 (2), 70 (3) , 126 (5) ,120 (7)) , but I think you shouldn't have much more than 4-5 queries in one dashboard anyway.

Residue field salvation ;-)

Upvotes: 1

  • 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

Upvotes: 4

Related Questions