system.cpu.utilization
values are always 0 in first export from PeriodicExportingMetricReader
#2797
Labels
bug
Something isn't working
What happened?
Combining
PeriodicExportingMetricReader
andSystemMetricsInstrumentor
produces only zero values forsystem.cpu.utilization
in the first export, which may be the only export if the process doesn't last long.Steps to Reproduce
Output:
Expected Result
Some
value
s greater than 0.Actual Result
"value": 0.0
for every data point.Additional context
In https://psutil.readthedocs.io/en/latest/#psutil.cpu_times_percent it says:
which is where the problem comes from. However, the first time a user calls
cpu_times_percent
in the main thread, it actually returns a nonzero value. This is becausepsutil
has this at the top level of the module:This means that when metrics are read on the main thread (e.g. by adding
meter_provider.force_flush()
to the script above, or by usingInMemoryMetricReader
) then the CPU values are nonzero.The problem is that
PeriodicExportingMetricReader
naturally has to perform readings on a separate thread. From the same psutil docs:Adding the following code to the script above anywhere between
reader = ...
andtime.sleep
seems to fix the problem:Actually implementing this fix in a sensible and maintainable way seems more complicated.
Would you like to implement a fix?
No
The text was updated successfully, but these errors were encountered: