2424import com .microsoft .applicationinsights .agent .internal .exporter .models .TelemetryItem ;
2525import com .microsoft .applicationinsights .agent .internal .telemetry .TelemetryClient ;
2626import com .microsoft .applicationinsights .agent .internal .telemetry .TelemetryUtil ;
27+ import java .util .concurrent .atomic .AtomicBoolean ;
2728import org .slf4j .Logger ;
2829import org .slf4j .LoggerFactory ;
2930import oshi .SystemInfo ;
@@ -45,6 +46,7 @@ public class OshiPerformanceCounter implements PerformanceCounter {
4546
4647 private volatile OSProcess processInfo ;
4748 private volatile CentralProcessor processor ;
49+ private static final AtomicBoolean hasError = new AtomicBoolean ();
4850
4951 @ Override
5052 public String getId () {
@@ -53,12 +55,25 @@ public String getId() {
5355
5456 @ Override
5557 public void report (TelemetryClient telemetryClient ) {
58+ // stop collecting oshi perf counters when initialization fails.
59+ if (hasError .get ()) {
60+ return ;
61+ }
62+
5663 if (processInfo == null || processor == null ) {
5764 // lazy initializing these because they add to slowness during startup
58- SystemInfo systemInfo = new SystemInfo ();
59- OperatingSystem osInfo = systemInfo .getOperatingSystem ();
60- processInfo = osInfo .getProcess (osInfo .getProcessId ());
61- processor = systemInfo .getHardware ().getProcessor ();
65+ try {
66+ SystemInfo systemInfo = new SystemInfo ();
67+ OperatingSystem osInfo = systemInfo .getOperatingSystem ();
68+ processInfo = osInfo .getProcess (osInfo .getProcessId ());
69+ processor = systemInfo .getHardware ().getProcessor ();
70+ } catch (Error ex ) {
71+ // e.g. icm 253155448: NoClassDefFoundError
72+ // e.g. icm 276640835: ExceptionInInitializerError
73+ hasError .set (true );
74+ logger .debug ("Fail to initialize OSProcess and CentralProcessor" , ex );
75+ return ;
76+ }
6277 }
6378
6479 long currCollectionTimeMillis = System .currentTimeMillis ();
0 commit comments