Laxmikant
Laxmikant

Reputation: 1611

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

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])

Upvotes: 2

Views: 1014

Answers (1)

markalex
markalex

Reputation: 13351

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.

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:]
)

Upvotes: 1

Related Questions