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?
Prometheus provides the following ways to divide time series with labels by time series without labels:
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With