I created a script to test and get the CPU usage in thousands (ex. 50m=50/1000 CPU core utilization) using rate() and then get the sum to compare the two results:
window_sec=120
curl -s -G "http://master.com:30355/api/v1/query_range" \
--data-urlencode "query=rate(container_cpu_usage_seconds_total{namespace='arka-ingress', \
pod='pod', container=''}[${window_sec}s]) * 1000" \
--data-urlencode "start=$(($(date +%s)-window_sec))" \
--data-urlencode "end=$(date +%s)" \
--data-urlencode "step=15s" | jq
curl -s -G "http://master.com:30355/api/v1/query" \
--data-urlencode "query=avg(rate(container_cpu_usage_seconds_total{namespace='arka-ingress', \
pod='pod', container=''}[${window_sec}s])) * 1000" | jq
Here is the output:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"beta_kubernetes_io_arch": "amd64",
"beta_kubernetes_io_os": "linux",
"cpu": "total",
"feature_node_kubernetes_io_network_sriov_capable": "true",
"id": "/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod0c5de2b8_2dea_4dd6_9a5c_bef29bc01c35.slice",
"instance": "master.com",
"job": "kubernetes-nodes-cadvisor",
"kubernetes_io_arch": "amd64",
"kubernetes_io_hostname": "master.com",
"kubernetes_io_os": "linux",
"namespace": "ingress",
"node_openshift_io_os_id": "rhcos",
"pod": "pod"
},
"values": [
[
1685838435,
"3.0482985848760746"
],
[
1685838450,
"3.0482985848760746"
],
[
1685838510,
"3.604911126207486"
]
]
}
]
}
}
{
"status": "success",
"data": {
"resultType": "vector",
"result": []
}
}
The data from the second response should be the average of the values in the result, but it's empty. When I increase the window, window_sec, I do get a response but it's not the average and the value is incorrect. I know the first query with rate() is correct, did I write the second query incorrect?