Skip to content

Commit

Permalink
Do not install GC metrics when GarbageCollectionNotificationInfo is n… (
Browse files Browse the repository at this point in the history
#7405)

…ot available

Hopefully fix #7401
  • Loading branch information
Mateusz Rzeszutek authored Dec 13, 2022
1 parent c57435f commit d1c0b3d
Showing 1 changed file with 22 additions and 0 deletions.
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() {}
}

0 comments on commit d1c0b3d

Please sign in to comment.