英文:
Dashboard variable based on another variables
问题
我有一个带有名为 host
的变量的 Grafana 仪表板。host
可以是 "domain1.com"、"domain2.com" 或 "domain3.com"。
我需要另一个自定义变量 var2
,它应该与 host
相关联。例如,当用户选择 "domain1.com" 作为 host
时,var2
应该变为 "some text",如果选择 "domain2.com",则变为 "another text",依此类推。
我该如何定义这个自定义变量?
英文:
I have a Grafana dashboard with variable called host
. Host can be "domain1.com", "domain2.com" or "domain3.com".
I need another custom variable var2
that would somehow be connected to the host
. For example, when user choose "domain1.com" as host
, var2
should become "some text", if "domain2.com" - "another text", etc.
How can I define this custom variable?
答案1
得分: 3
以下是翻译好的内容:
更简单的方法
如果你的两个值都是常量且在变量创建时已知,你可以使用带有标签的单个变量。
例如,自定义变量可以定义为以下值:
label1 : value1, label2 : value2
这样,你将在下拉菜单中看到标签"label1"、"label2"。
你可以以以下方式使用这个变量:
$var
或${var:value}
将返回所选项的值,例如value1
,${var:text}
将返回所选项的标签,例如lable1
。
你可以在这里查看演示。
对于问题中描述的情况,你需要将变量的值字段设置为:
domain1.com : some text, domain2.com : another text, domain3.com : etc
更灵活的方法
如果依赖于变量的值在创建阶段不是已知的,或者如果你希望第二个变量有多个值供用户选择,你需要使用两个变量和一些(伪)查询,其中第一个变量作为参数生成第二个变量的值。
这种方式可以链接多个变量并创建复杂的规则。但它需要一个数据源,可以评估这些规则。
以下是一些允许使用这些规则的数据源示例:
SQL
如果你有任何 SQL 数据源,变量的链接就是最简单的情况。你可以使用 CTE(公共表达式)、函数等等。对于我的示例,我将使用 MySQL 语法,但我相信几乎所有数据库都可以做到这一点。
为了适应问题中的一对一关系,你可以利用 case
语句来解码值:
select case
when '$var' = 'host1.com' then 'some text'
when '$var' = 'host2.com' then 'another text'
else 'Unexpected text'
end
为了为依赖变量提供多个值,你可以使用带有变量对的 CTE,并按需要筛选它们:
with PAIRS as (
select 'var1' var1, 'var1-dependent1' var2 union all
select 'var1', 'var1-dependent2' union all
select 'var2', 'var2-dependent1' union all
select 'var2', 'var2-dependent2' union all
select 'var2', 'var2-dependent3'
)
select var2
from PAIRS
where var1 = '$var'
你甚至可以对原始变量的内容应用转换以获取依赖变量。例如:用下划线替换所有点:
select replace('$var', '.', '_');
Prometheus
Prometheus 对于这种技巧来说不太理想,但大多数任务可以通过布尔运算符和 absent
的组合来解决。
对于初始变量是数字的情况:
- 依赖变量的单个值:
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
)
- 依赖变量的多个值:
query_result(
absent(non_existent{pseudo_label="dependent1 for 1"}) * 1 == $var1 or
absent(non_existent{pseudo_label="dependent2 for 1"}) * 1 == $var1 or
absent(non_existent{pseudo_label="dependent1 for 2"}) * 2 == $var1 or
absent(non_existent{pseudo_label="dependent2 for 2"}) * 2 == $var1 or
absent(non_existent{pseudo_label="dependent1 for 3"}) * 3 == $var1
)
对于字符串值:
- 依赖变量的单个值:
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"})))
- 依赖变量的多个值:
query_result(
(absent(non_existent{pseudo_label="dependent1 for input1"} or absent(non_existent{pseudo_label="dependent2 for input1"})
and on() (absent(non_existent{pseudo_input="input1"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="dependent1 for input2"} or absent(non_existent{pseudo_label="dependent2 for input2"})
and on() (absent(non_existent{pseudo_input="input2"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="dependent1 for input3"})
and on() (absent(non_existent{pseudo_input="input3"}) and absent(non_existent{pseudo_input="$value1"})))
一些正则表达式修改也是可能的。例如,你可以用下面的方法将变量中的最后一个点替换为下划线:
label_replace(
absent(non_existent{pseudo_input="$value1"}),
"pseudo_input", "${1}_$2",
"pseudo_input", "(.*)\\.(.*)"
)
但这种方法比使用 SQL 数据源更有限。
重要提示:在所有 Prometheus 的情况下,你需要提供链接变量的正则表达式,以从查询结果中提取标签。对于这里提供的示例,正则表达式将是 /pseudo_label="(.+?)"/
。
英文:
You have two way of dealing with this:
Easier one
If both your values are constant and known on variable creation, you can 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 labels "label1", "label2".
And you can use this variable in the following way:
$var
or${var:value}
will return value of selected item, for examplevalue1
,${var:text}
will return label of selected item, for examplelable1
.
You can see demo here.
And for the case described in the question you'll need Value field of variable to be set to
domain1.com : some text, domain2.com : another text, domain3.com : etc
More agile one
If values dependent variable are not known on the creation stage, or if you want second variable to have more than one value for user to chose from, you'll need to 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
Chaining of variables if you have any SQL data source is arguable the easiest case. You can use, CTEs, functions and much more. For my examples I'll use MySQL syntax, but I believe same can be done with all (or almost all) databases.
To accommodate one-to-one relation like in question you can utilize case
statement to decode value:
select case
when '$var' = 'host1.com' then 'some text'
when '$var' = 'host2.com' then 'another text'
else 'Unexpected text'
end
To provide multiple values for dependent variable, you can use CTE with pairs of variables and filter them like you need:
with PAIRS as (
select 'var1' var1, 'var1-dependent1' var2 union all
select 'var1', 'var1-dependent2' union all
select 'var2', 'var2-dependent1' union all
select 'var2', 'var2-dependent2' union all
select 'var2', 'var2-dependent3'
)
select var2
from PAIRS
where var1 = '$var'
You can event apply transformation to content of original variable to get dependent variable. For example: replace all dots with underscores:
select replace('$var', '.', '_');
Prometheus
Prometheus is less ideal for such tricks, but most of the tasks can be accommodated by combination of boolean operators and absent
.
For case where initial variable is numeric:
- single value for dependent variable:
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
)
- multiple values for dependent variable:
query_result(
absent(non_existent{pseudo_label="dependent1 for 1"}) * 1 == $var1 or
absent(non_existent{pseudo_label="dependent2 for 1"}) * 1 == $var1 or
absent(non_existent{pseudo_label="dependent1 for 2"}) * 2 == $var1 or
absent(non_existent{pseudo_label="dependent2 for 2"}) * 2 == $var1 or
absent(non_existent{pseudo_label="dependent1 for 3"}) * 3 == $var1
)
For string values:
- single value for dependent variable:
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"})))
- multiple values for dependent variable:
query_result(
(absent(non_existent{pseudo_label="dependent1 for input1"} or absent(non_existent{pseudo_label="dependent2 for input1"})
and on() (absent(non_existent{pseudo_input="input1"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="dependent1 for input2"} or absent(non_existent{pseudo_label="dependent2 for input2"})
and on() (absent(non_existent{pseudo_input="input2"}) and absent(non_existent{pseudo_input="$value1"}))) or
(absent(non_existent{pseudo_label="dependent1 for input3"})
and on() (absent(non_existent{pseudo_input="input3"}) and absent(non_existent{pseudo_input="$value1"})))
Some regex modification is also possible. For example you can replace last dot in your variable with underscore like this:
label_replace(
absent(non_existent{pseudo_input="$value1"}),
"pseudo_input", "_$2",
"pseudo_input", "(.*)\\.(.*)"
)
But this approach is more limited than what can be done with SQL data sources.
IMPORTANT: In all 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="(.+?)"/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论