Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify class compatibility before attempting cast #1435

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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