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

Do not install GC metrics when GarbageCollectionNotificationInfo is n… #7405

Merged
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 @@ -15,6 +15,7 @@
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Logger;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
Expand All @@ -32,6 +33,8 @@
*/
public final class GarbageCollector {

private static final Logger logger = Logger.getLogger(GarbageCollector.class.getName());

private static final AttributeKey<String> GC_KEY = AttributeKey.stringKey("gc");
private static final AttributeKey<String> ACTION_KEY = AttributeKey.stringKey("action");

Expand All @@ -43,6 +46,13 @@ public final class GarbageCollector {

/** Register observers for java runtime memory metrics. */
public static void registerObservers(OpenTelemetry openTelemetry) {
if (!isNotificationClassPresent()) {
logger.fine(
"The com.sun.management.GarbageCollectionNotificationInfo class is not available;"
+ " GC metrics will not be reported.");
return;
}

registerObservers(
openTelemetry,
ManagementFactory.getGarbageCollectorMXBeans(),
Expand Down Expand Up @@ -110,5 +120,17 @@ private static GarbageCollectionNotificationInfo extractNotificationInfo(
return GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
}

private static boolean isNotificationClassPresent() {
try {
Class.forName(
"com.sun.management.GarbageCollectionNotificationInfo",
false,
GarbageCollectorMXBean.class.getClassLoader());
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

private GarbageCollector() {}
}