diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/AllocatedMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/AllocatedMemoryMetrics.java index 7574738dd..9b9fbbf00 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/AllocatedMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/AllocatedMemoryMetrics.java @@ -25,19 +25,18 @@ public class AllocatedMemoryMetrics { public static final String METRIC_NAME = "process.runtime.jvm.memory.allocated"; - private static final boolean hasComSunThreadMXBean = hasComSunThreadMXBean(); private final AllocationTracker allocationTracker = createAllocationTracker(); - public boolean isAvailable() { - return allocationTracker != null; + public boolean isUnavailable() { + return allocationTracker == null; } public long getCumulativeAllocationTotal() { - return allocationTracker != null ? allocationTracker.getCumulativeAllocationTotal() : 0; + return allocationTracker == null ? 0 : allocationTracker.getCumulativeAllocationTotal(); } private AllocationTracker createAllocationTracker() { - if (hasComSunThreadMXBean && isThreadAllocatedMemoryEnabled()) { + if (hasComSunThreadMXBean() && isThreadAllocatedMemoryEnabled() && mxBeanTypeIsCompatible()) { return new AllocationTracker(); } return null; @@ -53,13 +52,26 @@ private static boolean hasComSunThreadMXBean() { } } + private static boolean mxBeanTypeIsCompatible() { + try { + Class mxBeanClass = + Class.forName( + "com.sun.management.ThreadMXBean", + false, + AllocatedMemoryMetrics.class.getClassLoader()); + return mxBeanClass.isInstance(ManagementFactory.getThreadMXBean()); + } catch (Exception e) { + return false; + } + } + private static boolean isThreadAllocatedMemoryEnabled() { - ThreadMXBean threadBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); + ThreadMXBean threadBean = (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean(); try { - threadBean.getAllThreadIds(); - return threadBean.isThreadAllocatedMemorySupported() - && threadBean.isThreadAllocatedMemoryEnabled(); + threadBean.getAllThreadIds(); // java.lang.management.ThreadMXBean + return threadBean.isThreadAllocatedMemorySupported() // com.sun.management.ThreadMXBean + && threadBean.isThreadAllocatedMemoryEnabled(); // com.sun.management.ThreadMXBean } catch (Error error) { // An error will be thrown for unsupported operations // e.g. SubstrateVM does not support getAllThreadIds diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/GcMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/GcMemoryMetrics.java index c56fc7d9d..382969ae3 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/GcMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/GcMemoryMetrics.java @@ -47,8 +47,8 @@ public long getDeltaSum() { return deltaSum.get(); } - public boolean isAvailable() { - return managementExtensionsPresent; + public boolean isUnavailable() { + return !managementExtensionsPresent; } public void registerListener() { diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerAllocatedMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerAllocatedMemoryMetrics.java index 6240dc2cc..02dd90add 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerAllocatedMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerAllocatedMemoryMetrics.java @@ -19,6 +19,7 @@ import static com.splunk.opentelemetry.instrumentation.jvmmetrics.AllocatedMemoryMetrics.METRIC_NAME; import com.splunk.opentelemetry.instrumentation.jvmmetrics.AllocatedMemoryMetrics; +import io.micrometer.common.lang.NonNull; import io.micrometer.core.instrument.FunctionCounter; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.BaseUnits; @@ -27,9 +28,9 @@ public class MicrometerAllocatedMemoryMetrics implements MeterBinder { @Override - public void bindTo(MeterRegistry registry) { + public void bindTo(@NonNull MeterRegistry registry) { AllocatedMemoryMetrics allocatedMemoryMetrics = new AllocatedMemoryMetrics(); - if (!allocatedMemoryMetrics.isAvailable()) { + if (allocatedMemoryMetrics.isUnavailable()) { return; } diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerGcMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerGcMemoryMetrics.java index a86747d6a..05f5dd62f 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerGcMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/micrometer/MicrometerGcMemoryMetrics.java @@ -29,7 +29,7 @@ public class MicrometerGcMemoryMetrics implements MeterBinder, AutoCloseable { @Override public void bindTo(MeterRegistry registry) { - if (!gcMemoryMetrics.isAvailable()) { + if (gcMemoryMetrics.isUnavailable()) { return; } diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelAllocatedMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelAllocatedMemoryMetrics.java index 647fec143..371fa7ebf 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelAllocatedMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelAllocatedMemoryMetrics.java @@ -29,7 +29,7 @@ public class OtelAllocatedMemoryMetrics { public void install() { AllocatedMemoryMetrics allocatedMemoryMetrics = new AllocatedMemoryMetrics(); - if (!allocatedMemoryMetrics.isAvailable()) { + if (allocatedMemoryMetrics.isUnavailable()) { return; } diff --git a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelGcMemoryMetrics.java b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelGcMemoryMetrics.java index 7b7fbe4ae..ce0667499 100644 --- a/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelGcMemoryMetrics.java +++ b/instrumentation/jvm-metrics/src/main/java/com/splunk/opentelemetry/instrumentation/jvmmetrics/otel/OtelGcMemoryMetrics.java @@ -25,7 +25,7 @@ public class OtelGcMemoryMetrics { public void install() { GcMemoryMetrics gcMemoryMetrics = new GcMemoryMetrics(); - if (!gcMemoryMetrics.isAvailable()) { + if (gcMemoryMetrics.isUnavailable()) { return; }