Is there a way to access the current instance while grouping by instance. What I am trying to do is to get the kube_node_status_capacity_cpu_cores while grouping over instance.
At the moment I am hardcoding the amount of cpu cores, but I want to parametrize it, since the nodes might have different amounts of cores.
My promQL query now:
(1 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[15s])))) * 0.007 + 0.23) * 165 * 1.125 * 4)
where 4 is the number of CPUs. What I want now is something like
(1 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[15s])))) * 0.007 + 0.23) * 165 * 1.125 * kube_node_status_capacity_cpu_cores{instance=currentInstance})
Thank you for your help
Edit
I now know why the approach using on(instance) does not work. The instance label on the left is the ip of the node exporter providing the cpu time. The instance on the right is the ip of the kube-state-metrics pod.
What I actually need is to somehow map node from the state metrics onto the node name of the node export exporter in question. Any ideas?
Edit 2
In my specific case I was looking for a different query. But the accepted answer is actually very helpful for future reference, since it would have answered the question correctly, if I described the case correctly.
You can use on()
operator:
sum by(instance) (metric1) * on(instance) metric2 * 0.007 + 0.23
And it will work if there is only one metric2
with matching instance
. Otherwise you need to apply some aggregation to metric2
(like use sum()
, avg()
, etc) or maybe filter out irrelevant time series (metric2{foo="bar"}
). More on one-to-one vector matches in the docs.