mberge
mberge

Reputation: 97

How to get CPU usage at a specific point in time with PromQL

I currently have these three PromQL queries:

CPU Sum:

curl -s -G "http://10.192.248.10:30000/api/v1/query" --data-urlencode "query=sum_over_time(container_cpu_usage_seconds_total{pod='f5ingress-f5ingress-55ff78b955-vqxch', namespace='arka-ingress'}[10m])" | jq

CPU Average:

curl -s -G "http://10.192.248.10:30000/api/v1/query" --data-urlencode "query=avg_over_time(container_cpu_usage_seconds_total{pod='f5ingress-f5ingress-55ff78b955-vqxch', namespace='arka-ingress'}[10m])" | jq

CPU Datapoints:

curl -s -G "http://10.192.248.10:30000/api/v1/query" --data-urlencode "query=container_cpu_usage_seconds_total{pod='f5ingress-f5ingress-55ff78b955-vqxch', namespace='arka-ingress'}[10m]" | jq

I technically have two questions:

Upvotes: 0

Views: 611

Answers (1)

valyala
valyala

Reputation: 18056

The container_cpu_usage_seconds_total metric shows cumulative CPU time used by the given container since the start of cadvisor - see these docs.

This is so called counter metric type.

If you want obtaining the real CPU usage at the given time, then use rate() function. For example, the following command would return the average CPU usage over the last 5 minutes for every running container at the given timestamp 1685665200:

curl -G http://10.192.248.10:30000/api/v1/query --data-urlencode 'time=1685665200' --data-urlencode 'query=rate(container_cpu_usage_seconds_total[5m])'

This command sends the query to instant query API provided by Prometheus. This API returns a single calculated point at the given time per each time series, which match the given filter.

If you need obtaining multiple calculated points per each matching time series on the given time range, then take a look at range query API.

Upvotes: 0

Related Questions