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

4.9k views Asked by At

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?

2

There are 2 answers

0
trallnag On BEST ANSWER

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

0
valyala On

Prometheus provides the following ways to divide time series with labels by time series without labels:

  1. By wrapping the time series without labels into scalar() function:
sum by (name) (
  rate(container_cpu_usage_seconds_total{$default, promstack_alias=~"$promstack_alias"}[$__rate_interval]) 
  * 100
)
/
scalar(count(sum by (instance_id) (container_last_seen{$default, instance_state="running"})))

This enables division by scalar according to these rules.

  1. By adding on() group_left() modifiers to /:
sum by (name) (
  rate(container_cpu_usage_seconds_total{$default, promstack_alias=~"$promstack_alias"}[$__rate_interval]) 
  * 100
)
/ on() group_left()
count(sum by (instance_id) (container_last_seen{$default, instance_state="running"}))

This enables many-to-one matching for time series on the left and the right side of / operator according to these docs.