From f125ea8f9f994969c07b8a0246cd135505b9b57d Mon Sep 17 00:00:00 2001 From: Helen Yang Date: Wed, 12 Jan 2022 14:07:40 -0800 Subject: [PATCH 1/4] Catch error during oshi initialization --- .../perfcounter/OshiPerformanceCounter.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java index 3279fc37e5b..fa575ef2c3d 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java @@ -31,6 +31,7 @@ import oshi.hardware.CentralProcessor.TickType; import oshi.software.os.OSProcess; import oshi.software.os.OperatingSystem; +import java.util.concurrent.atomic.AtomicBoolean; public class OshiPerformanceCounter implements PerformanceCounter { @@ -45,6 +46,7 @@ public class OshiPerformanceCounter implements PerformanceCounter { private volatile OSProcess processInfo; private volatile CentralProcessor processor; + private static final AtomicBoolean hasError = new AtomicBoolean(); @Override public String getId() { @@ -55,10 +57,22 @@ public String getId() { public void report(TelemetryClient telemetryClient) { if (processInfo == null || processor == null) { // lazy initializing these because they add to slowness during startup - SystemInfo systemInfo = new SystemInfo(); - OperatingSystem osInfo = systemInfo.getOperatingSystem(); - processInfo = osInfo.getProcess(osInfo.getProcessId()); - processor = systemInfo.getHardware().getProcessor(); + try { + SystemInfo systemInfo = new SystemInfo(); + OperatingSystem osInfo = systemInfo.getOperatingSystem(); + processInfo = osInfo.getProcess(osInfo.getProcessId()); + processor = systemInfo.getHardware().getProcessor(); + } catch (Error ex) { + // e.g. icm 253155448: NoClassDefFoundError + // e.g. icm 276640835: ExceptionInInitializerError + hasError.set(true); + return; + } + } + + // stop collecting oshi perf counters when initialization fails. + if (hasError.get()) { + return; } long currCollectionTimeMillis = System.currentTimeMillis(); From 4555ffcd81202c2eea00f27ab2af987edc9f42f6 Mon Sep 17 00:00:00 2001 From: Helen Yang Date: Wed, 12 Jan 2022 14:08:51 -0800 Subject: [PATCH 2/4] Fix spotless --- .../agent/internal/perfcounter/OshiPerformanceCounter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java index fa575ef2c3d..271a325af7c 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java @@ -24,6 +24,7 @@ import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem; import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient; import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryUtil; +import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import oshi.SystemInfo; @@ -31,7 +32,6 @@ import oshi.hardware.CentralProcessor.TickType; import oshi.software.os.OSProcess; import oshi.software.os.OperatingSystem; -import java.util.concurrent.atomic.AtomicBoolean; public class OshiPerformanceCounter implements PerformanceCounter { From 85ee74aac700a4403bcc3916ea548b08af9eb420 Mon Sep 17 00:00:00 2001 From: Helen Yang Date: Wed, 12 Jan 2022 14:20:29 -0800 Subject: [PATCH 3/4] Move hasError check to the beginning --- .../internal/perfcounter/OshiPerformanceCounter.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java index 271a325af7c..26a8a839da5 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java @@ -55,6 +55,11 @@ public String getId() { @Override public void report(TelemetryClient telemetryClient) { + // stop collecting oshi perf counters when initialization fails. + if (hasError.get()) { + return; + } + if (processInfo == null || processor == null) { // lazy initializing these because they add to slowness during startup try { @@ -70,11 +75,6 @@ public void report(TelemetryClient telemetryClient) { } } - // stop collecting oshi perf counters when initialization fails. - if (hasError.get()) { - return; - } - long currCollectionTimeMillis = System.currentTimeMillis(); long currProcessBytes = 0L; if (processInfo != null) { From f6ee9a6921a46970542cd2cbba636ce3cff660f2 Mon Sep 17 00:00:00 2001 From: Helen Yang Date: Wed, 12 Jan 2022 17:29:14 -0800 Subject: [PATCH 4/4] Add debug log --- .../agent/internal/perfcounter/OshiPerformanceCounter.java | 1 + 1 file changed, 1 insertion(+) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java index 26a8a839da5..43e90efdafe 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/OshiPerformanceCounter.java @@ -71,6 +71,7 @@ public void report(TelemetryClient telemetryClient) { // e.g. icm 253155448: NoClassDefFoundError // e.g. icm 276640835: ExceptionInInitializerError hasError.set(true); + logger.debug("Fail to initialize OSProcess and CentralProcessor", ex); return; } }