cpucpu-usageopen-telemetrycpu-time

Calculate CPU usage from process.cpu.time


https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md

I have been using this library which gives me 3 values for a single process :

user time, system time & wait time

One example value is : 0.05, 0.01, 0.00

How can I calculate CPU percent of the particular process ?


Solution

  • To calculate the total CPU load/utilization percent of the system, we need to calculate "total system cpu time (during the period)" + "total user cpu time (during the period)" / "period"

    In your case, suppose you take sample every 2 seconds, then for every sample, you need to calculate:

    = ( (process.cpu.time.sys - previous_process.cpu.time.sys) + (process.cpu.time.user - previous_process.cpu.time.user) ) / 2