Skip to content

Commit f6967fe

Browse files
authored
Add additional logging to make spotting stats issues easier (#133972)
* Add additional logging to make spotting stats issues easier * Refactor logging in JVM monitoring classes for improved error handling * Switch to debug level logging
1 parent 622ff24 commit f6967fe

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

server/src/main/java/org/elasticsearch/monitor/Probes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,29 @@
99

1010
package org.elasticsearch.monitor;
1111

12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
14+
1215
import java.lang.management.OperatingSystemMXBean;
1316
import java.lang.reflect.Method;
1417

1518
public class Probes {
19+
private static final Logger logger = LogManager.getLogger(Probes.class);
20+
1621
public static short getLoadAndScaleToPercent(Method method, OperatingSystemMXBean osMxBean) {
22+
logger.debug("Starting probe of method {} on osMxBean {}", method, osMxBean);
1723
if (method != null) {
1824
try {
1925
double load = (double) method.invoke(osMxBean);
2026
if (load >= 0) {
2127
return (short) (load * 100);
2228
}
2329
} catch (Exception e) {
30+
logger.debug(() -> "failed to invoke method [" + method + "] on osMxBean [" + osMxBean + "]", e);
2431
return -1;
2532
}
2633
}
34+
logger.debug("Method is null. Returning default value.");
2735
return -1;
2836
}
2937
}

server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.monitor.jvm;
1111

12+
import org.apache.logging.log4j.Logger;
1213
import org.elasticsearch.TransportVersions;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -30,10 +31,14 @@
3031
import java.util.List;
3132
import java.util.Map;
3233

34+
import static org.apache.logging.log4j.LogManager.getLogger;
35+
3336
public class JvmInfo implements ReportingService.Info {
3437

3538
private static final JvmInfo INSTANCE;
3639

40+
private static final Logger logger = getLogger(JvmInfo.class);
41+
3742
static {
3843
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
3944
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
@@ -94,47 +99,63 @@ public class JvmInfo implements ReportingService.Info {
9499
try {
95100
Object onErrorObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnError");
96101
onError = (String) valueMethod.invoke(onErrorObject);
97-
} catch (Exception ignored) {}
102+
} catch (Exception e) {
103+
logger.debug("Error getting JVM info from MX Bean", e);
104+
}
98105

99106
try {
100107
Object onOutOfMemoryErrorObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnOutOfMemoryError");
101108
onOutOfMemoryError = (String) valueMethod.invoke(onOutOfMemoryErrorObject);
102-
} catch (Exception ignored) {}
109+
} catch (Exception e) {
110+
logger.debug("Error getting JVM info from MX Bean", e);
111+
}
103112

104113
try {
105114
Object useCompressedOopsVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseCompressedOops");
106115
useCompressedOops = (String) valueMethod.invoke(useCompressedOopsVmOptionObject);
107-
} catch (Exception ignored) {}
116+
} catch (Exception e) {
117+
logger.debug("Error getting JVM info from MX Bean", e);
118+
}
108119

109120
try {
110121
Object useG1GCVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseG1GC");
111122
useG1GC = (String) valueMethod.invoke(useG1GCVmOptionObject);
112123
Object regionSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "G1HeapRegionSize");
113124
g1RegisionSize = Long.parseLong((String) valueMethod.invoke(regionSizeVmOptionObject));
114-
} catch (Exception ignored) {}
125+
} catch (Exception e) {
126+
logger.debug("Error getting JVM info from MX Bean", e);
127+
}
115128

116129
try {
117130
Object initialHeapSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "InitialHeapSize");
118131
configuredInitialHeapSize = Long.parseLong((String) valueMethod.invoke(initialHeapSizeVmOptionObject));
119-
} catch (Exception ignored) {}
132+
} catch (Exception e) {
133+
logger.debug("Error getting JVM info from MX Bean", e);
134+
}
120135

121136
try {
122137
Object maxHeapSizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxHeapSize");
123138
configuredMaxHeapSize = Long.parseLong((String) valueMethod.invoke(maxHeapSizeVmOptionObject));
124-
} catch (Exception ignored) {}
139+
} catch (Exception e) {
140+
logger.debug("Error getting JVM info from MX Bean", e);
141+
}
125142

126143
try {
127144
Object maxDirectMemorySizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxDirectMemorySize");
128145
directMemoryMax = Long.parseLong((String) valueMethod.invoke(maxDirectMemorySizeVmOptionObject));
129-
} catch (Exception ignored) {}
146+
} catch (Exception e) {
147+
logger.debug("Error getting JVM info from MX Bean", e);
148+
}
130149

131150
try {
132151
Object useSerialGCVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseSerialGC");
133152
useSerialGC = (String) valueMethod.invoke(useSerialGCVmOptionObject);
134-
} catch (Exception ignored) {}
135-
136-
} catch (Exception ignored) {
153+
} catch (Exception e) {
154+
logger.debug("Error getting JVM info from MX Bean", e);
155+
}
137156

157+
} catch (Exception e) {
158+
logger.debug("Error getting JVM info from MX Bean", e);
138159
}
139160

140161
Mem mem = new Mem(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax);

server/src/main/java/org/elasticsearch/monitor/jvm/SunThreadInfo.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99

1010
package org.elasticsearch.monitor.jvm;
1111

12-
import org.apache.logging.log4j.LogManager;
1312
import org.apache.logging.log4j.Logger;
1413

1514
import java.lang.management.ManagementFactory;
1615
import java.lang.management.ThreadMXBean;
1716
import java.lang.reflect.Method;
1817

18+
import static org.apache.logging.log4j.LogManager.getLogger;
19+
1920
public class SunThreadInfo {
2021

2122
private static final ThreadMXBean threadMXBean;
2223
private static final Method getThreadAllocatedBytes;
2324
private static final Method isThreadAllocatedMemorySupported;
2425
private static final Method isThreadAllocatedMemoryEnabled;
2526

26-
private static final Logger logger = LogManager.getLogger(SunThreadInfo.class);
27+
private static final Logger logger = getLogger(SunThreadInfo.class);
2728
public static final SunThreadInfo INSTANCE = new SunThreadInfo();
2829

2930
static {
@@ -83,11 +84,13 @@ public long getThreadAllocatedBytes(long id) {
8384
}
8485

8586
private static Method getMethod(String methodName, Class<?>... parameterTypes) {
87+
String className = "com.sun.management.ThreadMXBean";
8688
try {
87-
Method method = Class.forName("com.sun.management.ThreadMXBean").getMethod(methodName, parameterTypes);
89+
Method method = Class.forName(className).getMethod(methodName, parameterTypes);
8890
return method;
8991
} catch (Exception e) {
9092
// not available
93+
logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e);
9194
return null;
9295
}
9396
}

server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ public static OsProbe getInstance() {
773773

774774
}
775775

776-
private final Logger logger = LogManager.getLogger(getClass());
776+
private static final Logger logger = LogManager.getLogger(OsProbe.class);
777777

778778
OsInfo osInfo(long refreshInterval, Processors processors) throws IOException {
779779
return new OsInfo(
@@ -913,10 +913,12 @@ public OsStats osStats() {
913913
* Returns a given method of the OperatingSystemMXBean, or null if the method is not found or unavailable.
914914
*/
915915
private static Method getMethod(String methodName) {
916+
String className = "com.sun.management.OperatingSystemMXBean";
916917
try {
917-
return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName);
918+
return Class.forName(className).getMethod(methodName);
918919
} catch (Exception e) {
919920
// not available
921+
logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e);
920922
return null;
921923
}
922924
}

server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99

1010
package org.elasticsearch.monitor.process;
1111

12+
import org.apache.logging.log4j.Logger;
1213
import org.elasticsearch.bootstrap.BootstrapInfo;
1314
import org.elasticsearch.monitor.Probes;
1415

1516
import java.lang.management.ManagementFactory;
1617
import java.lang.management.OperatingSystemMXBean;
1718
import java.lang.reflect.Method;
1819

20+
import static org.apache.logging.log4j.LogManager.getLogger;
1921
import static org.elasticsearch.monitor.jvm.JvmInfo.jvmInfo;
2022

2123
public class ProcessProbe {
2224

25+
private static final Logger logger = getLogger(ProcessProbe.class);
26+
2327
private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
2428

2529
private static final Method getMaxFileDescriptorCountField;
@@ -130,10 +134,12 @@ public static ProcessStats processStats() {
130134
* or null if the method is not found or unavailable.
131135
*/
132136
private static Method getMethod(String methodName) {
137+
String className = "com.sun.management.OperatingSystemMXBean";
133138
try {
134-
return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName);
135-
} catch (Exception t) {
139+
return Class.forName(className).getMethod(methodName);
140+
} catch (Exception e) {
136141
// not available
142+
logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e);
137143
return null;
138144
}
139145
}
@@ -143,10 +149,12 @@ private static Method getMethod(String methodName) {
143149
* or null if the method is not found or unavailable.
144150
*/
145151
private static Method getUnixMethod(String methodName) {
152+
String className = "com.sun.management.UnixOperatingSystemMXBean";
146153
try {
147-
return Class.forName("com.sun.management.UnixOperatingSystemMXBean").getMethod(methodName);
148-
} catch (Exception t) {
154+
return Class.forName(className).getMethod(methodName);
155+
} catch (Exception e) {
149156
// not available
157+
logger.debug(() -> "failed to get method [" + methodName + "] from class [" + className + "]", e);
150158
return null;
151159
}
152160
}

0 commit comments

Comments
 (0)