[backport 7.17]Publish CPU percentage also non Unix systems (#13727) (#13732)

Clean backport of #13727 to branch `7.17.`

----
Original comment:

Use same technique for Unix system to extract the CPU percentage and publish to Logstash's metrics collector.
It doesn't retrieve the full set of metric of a Unix system, but the ones that are available from internal JDK class com.sun.management.OperatingSystemMXBean

(cherry picked from commit d8f4784d69)
This commit is contained in:
Andrea Selva 2022-02-08 18:14:22 +01:00 committed by GitHub
parent 5fca46e638
commit 0db25a80b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View file

@ -54,7 +54,7 @@ public class ProcessMonitor {
this.isUnix = osMxBean instanceof UnixOperatingSystemMXBean;
// Defaults are -1
if (this.isUnix) {
UnixOperatingSystemMXBean unixOsBean = (UnixOperatingSystemMXBean) osMxBean;;
UnixOperatingSystemMXBean unixOsBean = (UnixOperatingSystemMXBean) osMxBean;
this.openFds = unixOsBean.getOpenFileDescriptorCount();
this.maxFds = unixOsBean.getMaxFileDescriptorCount();
@ -62,9 +62,19 @@ public class ProcessMonitor {
unixOsBean.getProcessCpuTime(), TimeUnit.NANOSECONDS
);
this.cpuProcessPercent = scaleLoadToPercent(unixOsBean.getProcessCpuLoad());
this.cpuSystemPercent = getSystemCpuLoad();
this.cpuSystemPercent = getSystemCpuLoad(unixOsBean);
this.memTotalVirtualInBytes = unixOsBean.getCommittedVirtualMemorySize();
} else {
com.sun.management.OperatingSystemMXBean otherOsBean = (com.sun.management.OperatingSystemMXBean) osMxBean;
this.cpuMillisTotal = TimeUnit.MILLISECONDS.convert(
otherOsBean.getProcessCpuTime(), TimeUnit.NANOSECONDS
);
this.cpuProcessPercent = scaleLoadToPercent(otherOsBean.getProcessCpuLoad());
this.cpuSystemPercent = getSystemCpuLoad(otherOsBean);
this.memTotalVirtualInBytes = otherOsBean.getCommittedVirtualMemorySize();
}
}
@ -87,12 +97,8 @@ public class ProcessMonitor {
}
private static short scaleLoadToPercent(double load) {
if (osMxBean instanceof UnixOperatingSystemMXBean) {
if (load >= 0) {
return (short) (load * 100);
} else {
return -1;
}
if (load >= 0) {
return (short) (load * 100);
} else {
return -1;
}
@ -101,12 +107,12 @@ public class ProcessMonitor {
// The method `getSystemCpuLoad` is deprecated in favour of `getCpuLoad` since JDK14
// This method uses reflection to use the correct method depending on the version of
// the JDK being used.
private short getSystemCpuLoad() {
private short getSystemCpuLoad(OperatingSystemMXBean mxBeanInstance) {
if (CPU_LOAD_METHOD == null){
return -1;
}
try {
return scaleLoadToPercent((double)CPU_LOAD_METHOD.invoke(osMxBean));
return scaleLoadToPercent((double) CPU_LOAD_METHOD.invoke(mxBeanInstance));
} catch (Exception e){
return -1;
}

View file

@ -45,7 +45,6 @@ public class ProcessMonitorTest {
@SuppressWarnings("unchecked")
public void testReportCpuStats(){
Map<String, Object> processStats = ProcessMonitor.detect().toMap();
assumeTrue((Boolean) processStats.get("is_unix"));
assertThat("cpu", processStats.get("cpu"), instanceOf(Map.class));
Map<String, Object> cpuStats = (Map<String, Object>) processStats.get("cpu");
assertThat("cpu.process_percent", (Short)cpuStats.get("process_percent") >= 0, is(true));