I have a Grafana dashboard panel, it does have a variable called host, host can be domain1.com, domain2.com, domain3.com, domain4.com. I need another custom variable (var2) that be somehow connected to the host, for instance when user choose domain1.com var2 become "some text", if host is domain2.com var2 become "another text, etc. How can I define this custom variable?
1 Answers
You have two way of dealing with this:
Easier one
Use single variable with labels.
For example Custom variable can be defined with the following value:
label1 : value1, label2 : value2
This way you will see in dropdown lables (label1
, label2
).
And you can use this variable in the following way:
$var
will return value of selected item, for examplevalue1
,${var:text}
will return label of selected item, for examplelable1
.
You can see demo here.
More agile one
You can use two variables and some (pseudo-)query with first variable as parameter to generate value of the second one.
This way you can chain multiple variables and create complex rules. But it requires a data source, that will allow to evaluate this rules.
Here are couple examples of data sources allowing for such rules:
SQL
With SQL data source one can utilize case
statement to decode value.
For example with MySQL data source your query will look something like this:
select case
when '$var' = 'host1.com' then 'some text'
when '$var' = 'host2.com' then 'some other text'
else 'Unexpected text'
end
Prometheus
In Prometheus such a construction can be created by combination of boolean operators and absent
.
For case where initial value numeric:
query_result(
absent(non_existent{pseudo_label="value1"}) * 1 == $var1 or
absent(non_existent{pseudo_label="value2"}) * 2 == $var1 or
absent(non_existent{pseudo_label="value3"}) * 3 == $var1
)
For string values:
query_result(
(absent(non_existent{pseudo_label="output1"}) and on() (absent(non_existent{pseudo_input="input1"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="output2"}) and on() (absent(non_existent{pseudo_input="input2"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="output3"}) and on() (absent(non_existent{pseudo_input="input3"}) and absent(non_existent{pseudo_input="$value1"})))
In both Prometheus' cases you need to provide Regex for linked variable, to extract label from result of query. For examples provided here regex will be /pseudo_label="(.+?)"/
-
Thank a lot. The first one is interesting, my variables are coming from a data source, and I can not change them. About the second one, my data source is Prometheus, is there anyway to define such a query for prometheus? – SMA Jun 02 '23 at 21:45
-
1@SMA, 1. if both potential labels and values of you variable are present within Prometheus, you can excract them using combination of `query_result` and named regex groups. 2. I'm not aware of a simple straightforward way to do it with Prometheus data source. Maybe some clever combination of boolean logic and `absent` function to produce arbitrary string values. But I feel like this will qualify for separate full-fledged Q&A. – markalex Jun 02 '23 at 21:53
-
1@SMA, I've updated answer, to provided some insight how something like we discussed can be done in Prometheus. – markalex Jun 07 '23 at 19:18
-
Incredible answer! Variable mapping has been a missing feature and you've solved it – Calvin Jul 13 '23 at 17:28