From 1e835db890a2de18ed314485413a1e19e7bd18a9 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 7 Sep 2023 14:48:35 -0700 Subject: [PATCH 1/3] verify class compatibility before attempting cast --- .../jvmmetrics/AllocatedMemoryMetrics.java | 26 ++++++++++++------- .../MicrometerAllocatedMemoryMetrics.java | 5 ++-- .../otel/OtelAllocatedMemoryMetrics.java | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) 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..fb84cd880 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,22 @@ private static boolean hasComSunThreadMXBean() { } } + private static boolean mxBeanTypeIsCompatible() { + try { + Class mxBeanClass = Class.forName("com.sun.management.ThreadMXBean"); + 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/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/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; } From 7a4f14d61fc177d755b7d80f968d5852038c56f0 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 8 Sep 2023 14:31:41 -0700 Subject: [PATCH 2/3] specify classloader --- .../instrumentation/jvmmetrics/AllocatedMemoryMetrics.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fb84cd880..d63dee459 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 @@ -54,7 +54,7 @@ private static boolean hasComSunThreadMXBean() { private static boolean mxBeanTypeIsCompatible() { try { - Class mxBeanClass = Class.forName("com.sun.management.ThreadMXBean"); + Class mxBeanClass = Class.forName("com.sun.management.ThreadMXBean", false, AllocatedMemoryMetrics.class.getClassLoader()); return mxBeanClass.isInstance(ManagementFactory.getThreadMXBean()); } catch (Exception e) { return false; From bb79b6de9624aeb5fecdfb9e5bf76093245daf6a Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 8 Sep 2023 14:38:23 -0700 Subject: [PATCH 3/3] rename for consistency --- .../instrumentation/jvmmetrics/AllocatedMemoryMetrics.java | 6 +++++- .../instrumentation/jvmmetrics/GcMemoryMetrics.java | 4 ++-- .../jvmmetrics/micrometer/MicrometerGcMemoryMetrics.java | 2 +- .../jvmmetrics/otel/OtelGcMemoryMetrics.java | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) 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 d63dee459..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 @@ -54,7 +54,11 @@ private static boolean hasComSunThreadMXBean() { private static boolean mxBeanTypeIsCompatible() { try { - Class mxBeanClass = Class.forName("com.sun.management.ThreadMXBean", false, AllocatedMemoryMetrics.class.getClassLoader()); + Class mxBeanClass = + Class.forName( + "com.sun.management.ThreadMXBean", + false, + AllocatedMemoryMetrics.class.getClassLoader()); return mxBeanClass.isInstance(ManagementFactory.getThreadMXBean()); } catch (Exception e) { return false; 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/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/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; }