Skip to content

Commit

Permalink
verify class compatibility before attempting cast
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk committed Sep 7, 2023
1 parent 7792c45 commit 0511ebf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class OtelAllocatedMemoryMetrics {

public void install() {
AllocatedMemoryMetrics allocatedMemoryMetrics = new AllocatedMemoryMetrics();
if (!allocatedMemoryMetrics.isAvailable()) {
if (allocatedMemoryMetrics.isUnavailable()) {
return;
}

Expand Down

0 comments on commit 0511ebf

Please sign in to comment.