diff --git a/server/src/main/java/org/elasticsearch/monitor/Probes.java b/server/src/main/java/org/elasticsearch/monitor/Probes.java index d9834e8456fd5..e125e2440d73e 100644 --- a/server/src/main/java/org/elasticsearch/monitor/Probes.java +++ b/server/src/main/java/org/elasticsearch/monitor/Probes.java @@ -9,11 +9,17 @@ package org.elasticsearch.monitor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.lang.management.OperatingSystemMXBean; import java.lang.reflect.Method; public class Probes { + private static final Logger logger = LogManager.getLogger(Probes.class); + public static short getLoadAndScaleToPercent(Method method, OperatingSystemMXBean osMxBean) { + logger.debug("Starting probe of method {} on osMxBean {}", method, osMxBean); if (method != null) { try { double load = (double) method.invoke(osMxBean); @@ -21,9 +27,11 @@ public static short getLoadAndScaleToPercent(Method method, OperatingSystemMXBea return (short) (load * 100); } } catch (Exception e) { + logger.debug(() -> "failed to invoke method [" + method + "] on osMxBean [" + osMxBean + "]", e); return -1; } } + logger.debug("Method is null. Returning default value."); return -1; } } diff --git a/server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java b/server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java index a1faf24b512c4..81c3b039392e2 100644 --- a/server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java +++ b/server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java @@ -9,6 +9,7 @@ package org.elasticsearch.monitor.jvm; +import org.apache.logging.log4j.Logger; import org.elasticsearch.TransportVersions; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -30,10 +31,14 @@ import java.util.List; import java.util.Map; +import static org.apache.logging.log4j.LogManager.getLogger; + public class JvmInfo implements ReportingService.Info { private static final JvmInfo INSTANCE; + private static final Logger logger = getLogger(JvmInfo.class); + static { RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); @@ -94,47 +99,63 @@ public class JvmInfo implements ReportingService.Info { try { Object onErrorObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnError"); onError = (String) valueMethod.invoke(onErrorObject); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object onOutOfMemoryErrorObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnOutOfMemoryError"); onOutOfMemoryError = (String) valueMethod.invoke(onOutOfMemoryErrorObject); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object useCompressedOopsVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseCompressedOops"); useCompressedOops = (String) valueMethod.invoke(useCompressedOopsVmOptionObject); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object useG1GCVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseG1GC"); useG1GC = (String) valueMethod.invoke(useG1GCVmOptionObject); Object regionSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "G1HeapRegionSize"); g1RegisionSize = Long.parseLong((String) valueMethod.invoke(regionSizeVmOptionObject)); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object initialHeapSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "InitialHeapSize"); configuredInitialHeapSize = Long.parseLong((String) valueMethod.invoke(initialHeapSizeVmOptionObject)); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object maxHeapSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxHeapSize"); configuredMaxHeapSize = Long.parseLong((String) valueMethod.invoke(maxHeapSizeVmOptionObject)); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object maxDirectMemorySizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxDirectMemorySize"); directMemoryMax = Long.parseLong((String) valueMethod.invoke(maxDirectMemorySizeVmOptionObject)); - } catch (Exception ignored) {} + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } try { Object useSerialGCVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseSerialGC"); useSerialGC = (String) valueMethod.invoke(useSerialGCVmOptionObject); - } catch (Exception ignored) {} - - } catch (Exception ignored) { + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); + } + } catch (Exception e) { + logger.debug("Error getting JVM info from MX Bean", e); } Mem mem = new Mem(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax); diff --git a/server/src/main/java/org/elasticsearch/monitor/jvm/SunThreadInfo.java b/server/src/main/java/org/elasticsearch/monitor/jvm/SunThreadInfo.java index 8ada1f2da33dd..b62e58c04b278 100644 --- a/server/src/main/java/org/elasticsearch/monitor/jvm/SunThreadInfo.java +++ b/server/src/main/java/org/elasticsearch/monitor/jvm/SunThreadInfo.java @@ -9,13 +9,14 @@ package org.elasticsearch.monitor.jvm; -import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.lang.reflect.Method; +import static org.apache.logging.log4j.LogManager.getLogger; + public class SunThreadInfo { private static final ThreadMXBean threadMXBean; @@ -23,7 +24,7 @@ public class SunThreadInfo { private static final Method isThreadAllocatedMemorySupported; private static final Method isThreadAllocatedMemoryEnabled; - private static final Logger logger = LogManager.getLogger(SunThreadInfo.class); + private static final Logger logger = getLogger(SunThreadInfo.class); public static final SunThreadInfo INSTANCE = new SunThreadInfo(); static { @@ -83,11 +84,13 @@ public long getThreadAllocatedBytes(long id) { } private static Method getMethod(String methodName, Class... parameterTypes) { + String className = "com.sun.management.ThreadMXBean"; try { - Method method = Class.forName("com.sun.management.ThreadMXBean").getMethod(methodName, parameterTypes); + Method method = Class.forName(className).getMethod(methodName, parameterTypes); return method; } catch (Exception e) { // not available + logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e); return null; } } diff --git a/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java b/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java index 06ab6a6eee410..829f17e911e97 100644 --- a/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java +++ b/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java @@ -773,7 +773,7 @@ public static OsProbe getInstance() { } - private final Logger logger = LogManager.getLogger(getClass()); + private static final Logger logger = LogManager.getLogger(OsProbe.class); OsInfo osInfo(long refreshInterval, Processors processors) throws IOException { return new OsInfo( @@ -913,10 +913,12 @@ public OsStats osStats() { * Returns a given method of the OperatingSystemMXBean, or null if the method is not found or unavailable. */ private static Method getMethod(String methodName) { + String className = "com.sun.management.OperatingSystemMXBean"; try { - return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName); + return Class.forName(className).getMethod(methodName); } catch (Exception e) { // not available + logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e); return null; } } diff --git a/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java b/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java index 89c219945f45b..96b6ad1513e7f 100644 --- a/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java +++ b/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java @@ -9,6 +9,7 @@ package org.elasticsearch.monitor.process; +import org.apache.logging.log4j.Logger; import org.elasticsearch.bootstrap.BootstrapInfo; import org.elasticsearch.monitor.Probes; @@ -16,10 +17,13 @@ import java.lang.management.OperatingSystemMXBean; import java.lang.reflect.Method; +import static org.apache.logging.log4j.LogManager.getLogger; import static org.elasticsearch.monitor.jvm.JvmInfo.jvmInfo; public class ProcessProbe { + private static final Logger logger = getLogger(ProcessProbe.class); + private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean(); private static final Method getMaxFileDescriptorCountField; @@ -130,10 +134,12 @@ public static ProcessStats processStats() { * or null if the method is not found or unavailable. */ private static Method getMethod(String methodName) { + String className = "com.sun.management.OperatingSystemMXBean"; try { - return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName); - } catch (Exception t) { + return Class.forName(className).getMethod(methodName); + } catch (Exception e) { // not available + logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e); return null; } } @@ -143,10 +149,12 @@ private static Method getMethod(String methodName) { * or null if the method is not found or unavailable. */ private static Method getUnixMethod(String methodName) { + String className = "com.sun.management.UnixOperatingSystemMXBean"; try { - return Class.forName("com.sun.management.UnixOperatingSystemMXBean").getMethod(methodName); - } catch (Exception t) { + return Class.forName(className).getMethod(methodName); + } catch (Exception e) { // not available + logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e); return null; } }