Skip to content

Commit 779412a

Browse files
committed
Add presence check for com.sun.management.UnixOperatingSystemMXBean
1 parent 5f22abd commit 779412a

File tree

1 file changed

+12
-1
lines changed
  • instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/internal

1 file changed

+12
-1
lines changed

instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/internal/ExperimentalFileDescriptor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
* any time.
1818
*/
1919
public final class ExperimentalFileDescriptor {
20+
private static final Class<?> unixOperatingSystemMxBeanClass =
21+
loadClass("com.sun.management.UnixOperatingSystemMXBean");
22+
23+
private static Class<?> loadClass(String className) {
24+
try {
25+
return Class.forName(className, false, ExperimentalFileDescriptor.class.getClassLoader());
26+
} catch (ClassNotFoundException | LinkageError e) {
27+
return null;
28+
}
29+
}
2030

2131
/** Register observers for java runtime file descriptor metrics. */
2232
public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry) {
@@ -29,7 +39,8 @@ static List<AutoCloseable> registerObservers(
2939
Meter meter = JmxRuntimeMetricsUtil.getMeter(openTelemetry);
3040
List<AutoCloseable> observables = new ArrayList<>();
3141

32-
if (osBean instanceof com.sun.management.UnixOperatingSystemMXBean) {
42+
if (unixOperatingSystemMxBeanClass != null
43+
&& unixOperatingSystemMxBeanClass.isInstance(osBean)) {
3344
observables.add(
3445
meter
3546
.upDownCounterBuilder("jvm.file_descriptor.count")

0 commit comments

Comments
 (0)