Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created December 18, 2024 19:56
Show Gist options
  • Save kavirajk/e21b4441918ba9ca7674ea1baac51327 to your computer and use it in GitHub Desktop.
Save kavirajk/e21b4441918ba9ca7674ea1baac51327 to your computer and use it in GitHub Desktop.
CPU hours of all pods
  1. It calculates the total CPU hours spent by all the pods in given namespace for chosen time period (say a month). But instead of choosing "actual" value for each 1-minute datapoint, it chooses max value of all the per-minute data point and calculates per hour value by that max * 60.
sum_over_time(
    (
        60 * (max_over_time(sum(rate(container_cpu_usage_seconds_total{namespace="", pod=~".*", container!=""}[1m]))[1h:1m]))
    )[$__range:1h] # sum over the time period choosen (e.g: previous month)
)
  1. If I don't have to use max in an hour, instead use normal absolute value. I can do something like.
sum_over_time(
    sum_over_time(
        sum(rate(container_cpu_usage_seconds_total{namespace="", pod=~".*", container!=""}[1m]))[1h:1m])
    )[$__range:1h] # sum over the time period choosen (e.g: previous month)
)

is it correct? I belive I don't have / 3600 or anything because here I care about CPU hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment