Prashant Pathak
Prashant Pathak

Reputation: 173

Monitoring Container CPU Utilization using Prometheus

On Prometheus UI when I am running the below query it worked as expected

(sum ( rate (container_cpu_usage_seconds_total{namespace="nginx-enabled", container="nginx"}[30s])) * 100000) /  110000
Output Returned: 23.34


container_spec_cpu_quota{namespace="nginx-enabled", container="nginx"}
Output Returned: 110000

But when I am trying with below query I did not see any output and no error message related to query string.

(sum ( rate (container_cpu_usage_seconds_total{namespace="nginx-enabled", container="nginx"}[30s])) * 100000) / (container_spec_cpu_quota{namespace="nginx-enabled", container="nginx"})

Can someone please help me what I am missing, I am very very new to Prometheus Query

Upvotes: 1

Views: 2878

Answers (1)

Michael Hausenblas
Michael Hausenblas

Reputation: 14001

PromQL can indeed be tricky, for example, if one happens to use a wrong data type it simply fails silently. The problem you are running into seems to be that the left-hand side of your combined query, that is, the (sum(rate(container_cpu_usage_seconds_total... part returns a scalar value (so, exactly one value) while the right-hand side (container_spec_cpu_quota{...}) is a vector whose most recent value is shown only.

I tried to re-create your combined query in PromLens, which offers a nice way to debug queries. Unfortunately, it doesn't offer container_spec_cpu_quota in the data so I replaced it with something close-by that is available (container_spec_cpu_shares).

Now, if you plug in the following query into PromLens, emulating your query, you will see that it fails as well (you will see why when you click on the Explain tab):

(sum ( rate (container_cpu_usage_seconds_total[30s])) * 100000)
/
container_spec_cpu_shares

Whereas, when you aggregate the right-hand side as well, turning it into a scalar (and since it's a gauge, a simple sum does it), you get a result:

(sum ( rate (container_cpu_usage_seconds_total[30s])) * 100000)
/
sum(container_spec_cpu_shares)

Further options to deal with left and right side not aligning in terms of data types, such as using the on or ignoring keyword are available via the docs in the Vector matching section.

Upvotes: 1

Related Questions