grafana + prometheus 条件查询

huangapple go评论55阅读模式
英文:

grafana + prometheus conditional query

问题

I have this query:

sum by (name) (rate(container_cpu_usage_seconds_total{image!="", container_label_org_label_schema_group="$group"}[1m])) / scalar(count(node_cpu_seconds_total{mode="$mode"})) * 100

$group is a list and values can be 'monitoring', 'web', or 'all'.

When $group is 'web', I'd like to replace $group with an empty string. So the container_label_org_label_schema_group condition should be:

container_label_org_label_schema_group = ''

When $group is 'all', then I want to match where container_label_org_label_schema_group is equal to 'monitoring' or an empty string. Effectively something like:

container_label_org_label_schema_group = '"monitoring"|"'"'"

Not sure how to do this with the promQL. It doesn't seem like the use of some AND operators to do pseudo IFs is working here. I'm thinking maybe substitution could be an option. But I do not have success there either.

英文:

I have this query:

sum by (name) (rate(container_cpu_usage_seconds_total{image!="",
container_label_org_label_schema_group="$group"}
[1m])) / scalar(count(node_cpu_seconds_total{mode="$mode"})) * 100

$group is a list and values can be 'monitoring', 'web', or 'all'.

When $group is 'web', I'd like to replace $group with empty string. So the container_label_org_label_schema_group condition should be:

container_label_org_label_schema_group  = ''

When $group is 'all', then i want to match where container_label_org_label_schema_group is equal to 'monitoring' or empty string. Effectively something like:

container_label_org_label_schema_group  = '"monitoring"|""'

Not sure how to do do this with the promQL. It doesn't seem like the use of some AND operators to do pseudo IFs is working here. I'm thinking maybe substitution could be an option. But I don't not have success there either.

答案1

得分: 1

In Grafana,您可以为您的每个变量值指定标签:它将显示在下拉菜单中。但在替代时,将使用相应的值。

要执行此操作,请以以下格式提供变量的值:标签1:值1,标签2:值2

然后只需在查询中使用它,如$example

对于您的确切问题,请使用具有以下值的变量:

web:,monitoring:monitoring,all:monitoring|

请注意在第一个冒号和逗号之间有双空格。

英文:

Not really. In grafana you can specify label for every value of you variable: it will be shown in drop down. But when substituted corresponding value will be used instead.

To do it you provide values for variable in format label1 : value1, label2 : value2.
grafana + prometheus 条件查询

And then simply use it in query like $example
grafana + prometheus 条件查询

For your exact problem use variable with the following values:

web :  , monitoring : monitoring, all : monitoring|

Please notice double space between first colon and comma.

huangapple
  • 本文由 发表于 2023年5月14日 15:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定