Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 459
trallnag Avatar asked Jan 20 '26 00:01

trallnag


2 Answers

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.

like image 77
valyala Avatar answered Jan 21 '26 18:01

valyala


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

like image 24
trallnag Avatar answered Jan 21 '26 17:01

trallnag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!