0

I'm trying to calculate the MIN/MAX/AVG of the CPU usage of a pod using the rate function, but rate() is only returning a single value.

Here is the command to get the CPU usage in seconds and the values from the response of the container_cpu_usage_seconds_total metric:

curl -s -G "http://master-1.ocp9.pd.f5net.com:30355/api/v1/query" --data-urlencode "query=container_cpu_usage_seconds_total{namespace='arka-ingress', pod='f5ingress-f5ingress-55ff78b955-bjhbc', container=''}[12h:1h]" | jq

"values": [
  [
    1685746800,
    "233.925484799"
  ],
  [
    1685750400,
    "245.503601613"
  ],
  [
    1685754000,
    "257.245759184"
  ],
  [
    1685757600,
    "268.81255512"
  ],
  [
    1685761200,
    "280.711385953"
  ],
  [
    1685764800,
    "292.226109825"
  ],
  [
    1685768400,
    "304.090964418"
  ],
  [
    1685772000,
    "315.670226267"
  ],
  [
    1685775600,
    "327.400741452"
  ],
  [
    1685779200,
    "339.122179483"
  ],
  [
    1685782800,
    "350.865870242"
  ]
]

When I query using the rate function around it, it should return a list of the rates of change, but it only returns a single value.

curl -s -G "http://master-1.ocp9.pd.f5net.com:30355/api/v1/query" --data-urlencode "query=rate(container_cpu_usage_seconds_total{namespace='arka-ingress', pod='f5ingress-f5ingress-55ff78b955-bjhbc', container=''}[12h:1h])" | jq

"value": [
  1685786028.71,
  "0.003085077907426099"
]

Why is this happening?


UPDATE: The query now returns the correct response:

curl -s -G "http://master-1.ocp9.pd.f5net.com:30355/api/v1/query_range" --data-urlencode "query=rate(container_cpu_usage_seconds_total{namespace='arka-ingress', pod='f5ingress-f5ingress-55ff78b955-bjhbc', container=''}[5m]) * 1000" --data-urlencode "start=1685811600" --data-urlencode "end=1685811660" --data-urlencode "step=15s" | jq

mberge
  • 57
  • 5
  • `/api/v1/query` is expected to return single value. In your initial example it was not the case because you used range selector. Use [`/api/v1/query_range`](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries) instead. – markalex Jun 03 '23 at 10:39
  • @markalex Look at the update I added. See how container_cpu_usage_seconds_total metric uses a window of 5m? I understand that by default Prometheus uses a 1m step, so if I specify a 5m window then it will return 5 datapoints. These values are then passed into rate(), which calculates each rate and returns 5 datapoints as well. But to use query_range you also have to specify start, end, and step parameters. What do the start and end time values do to the output? There are two sources of time that are used in this metric that are required to get a result (window: [5m] and start and end time). – mberge Jun 03 '23 at 18:54
  • "I understand that by default Prometheus uses a 1m step, so if I specify a 5m window then it will return 5 datapoints." - more specifically: number of datapoints depends on scrape_interval (which is by default `1m`). "These values are then passed into rate(), which calculates each rate and returns 5 datapoints" - no. It calculates values for every second between first and last sample. And in case of `api/v1/query` AFAIK(!) return only the last one. – markalex Jun 03 '23 at 19:36
  • When you use `query_range` you specify time range and steps, and for every step internally query calculated separately and values over length of this step are returned, and later combined into single response. My might be a bit confused by range selectors (`[5m]`) and time range (`start`, `end`) of query. Maybe analogue might help you here: first is how values internally are calculated. Second is width of graph you would draw with results. – markalex Jun 03 '23 at 19:41

0 Answers0