英文:
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'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&g0.tab=1&g0.stacked=0&g0.range_input=1h&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)&g1.tab=1&g1.stacked=0&g1.range_input=1h
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论