Skip to content

Commit

Permalink
Verify class compatibility before attempting cast (#1435)
Browse files Browse the repository at this point in the history
* verify class compatibility before attempting cast

* specify classloader

* rename for consistency
  • Loading branch information
breedx-splk authored Sep 13, 2023
1 parent 09c2c80 commit 8e07921
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 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,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public long getDeltaSum() {
return deltaSum.get();
}

public boolean isAvailable() {
return managementExtensionsPresent;
public boolean isUnavailable() {
return !managementExtensionsPresent;
}

public void registerListener() {
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 MicrometerGcMemoryMetrics implements MeterBinder, AutoCloseable {

@Override
public void bindTo(MeterRegistry registry) {
if (!gcMemoryMetrics.isAvailable()) {
if (gcMemoryMetrics.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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class OtelGcMemoryMetrics {

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

Expand Down

0 comments on commit 8e07921

Please sign in to comment.