如何使用PromQL获取两个标签具有相同值的所有指标?

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

How can i get all the metrics where two label have same values using promql?

问题

max_over_time(cbnode_systemstats_cpu_utilization_rate{instance=node}[6h])

英文:

I am new to promql. So not sure if promql supports my requirement or not.

max_over_time(cbnode_systemstats_cpu_utilization_rate{instance="a",node="a"}[6h])

This above query gives me result of max cpu utilization in past 6 hr for instance a single instnace a.

However I want a query which fetches all metrics for all the instances where instance and node has same value. Something similar to below:

max_over_time(cbnode_systemstats_cpu_utilization_rate{instance = node}[6h])

答案1

得分: 1

没有简洁的方法来做到这一点。

但是你可以利用 label_replace,标签匹配的逻辑进行二进制操作,再加上一点创造力。

label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "instance", "(.*)")
== label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "node", "(.*)")

在这里,我们向左边的指标添加了一个名为 pseudoid 的新标签,其值为 instance,右边也是一样,但值为 node

只有当所有标签都相同时才会返回结果,这意味着 instance == pseudoid == node

类似查询的演示可以在这里看到。

请注意,因为这不是即时向量选择器,你需要使用子查询语法将其传递给 max_over_time

你的最终查询应该如下所示:

max_over_time(
 (
   label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "instance", "(.*)")
     == label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "node", "(.*)")
 )[6h:]
)

<details>
<summary>英文:</summary>

There is no easy elegant way to do that. 

But you can utilize `label_replace`, logic of label matching for binary operations and a pinch of ingenuity.

label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "instance", "(.)")
== label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "node", "(.
)")

Here we add to LHS metric new label called `pseudoid` with value of `instance`, and same for RHS, but with value of `node`.

Result will be returned only if all labels are the same, and in turn it will mean that `instance == pseudoid == node`.

Demo of similar query can be seen [here][1].

Notice that since it is not the instant vector selector, you&#39;ll need to use subquery syntax to pass it into `max_over_time`.

You resulting query should look like this:

max_over_time(
(
label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "instance", "(.)")
== label_replace(cbnode_systemstats_cpu_utilization_rate{}, "pseudoid", "$1", "node", "(.
)")
)[6h:]
)


  [1]: https://prometheus.demo.do.prometheus.io/graph?g0.expr=go_build_info%7B%7D&amp;g0.tab=1&amp;g0.stacked=0&amp;g0.range_input=1h&amp;g1.expr=label_replace(go_build_info%7B%7D%2C%20%22id%22%2C%20%22%241%22%2C%20%22path%22%2C%20%22(.*)%22)%20%3D%3D%20label_replace(go_build_info%7B%7D%2C%20%22id%22%2C%20%22%241%22%2C%20%22version%22%2C%20%22(.*)%22)&amp;g1.tab=1&amp;g1.stacked=0&amp;g1.range_input=1h

</details>



huangapple
  • 本文由 发表于 2023年6月2日 01:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384356.html
匿名

发表评论

匿名网友

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

确定