Search code examples
prometheuspromql

How to divide "sum()" by "count()" without labels


I have the CPU usage of a number of containers in percent of their respective container instance and I want to divide this value by the number of container instances available.

This query gives me as expected the CPU usage in percent:

sum by (name) (
  rate(container_cpu_usage_seconds_total{$default, promstack_alias=~"$promstack_alias"}[$__rate_interval]) 
  * 100
)

And this query gives me nothing more than the number of instances:

count(sum by (instance_id) (container_last_seen{$default, instance_state="running"}))

But I am not able to combine them. What I want is basically this:

sum by (name) (
  rate(container_cpu_usage_seconds_total{$default, promstack_alias=~"$promstack_alias"}[$__rate_interval]) 
  * 100
)
/
count(sum by (instance_id) (container_last_seen{$default, instance_state="running"}))

If I divide by a number, for example 3, the query succeeds. What am I missing?


Solution

  • Found the answer here How to divide after grouping two different metrics in Prometheus?

    The key is to ignore existing labels with / ignoring(name) group_left(in my case).