From e2150f9dbc54a8bf4608882196a8b5f5518ea9bb Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 13 Dec 2022 16:00:07 +0100 Subject: [PATCH 1/2] Do not install GC metrics when GarbageCollectionNotificationInfo is not available --- .../runtimemetrics/GarbageCollector.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java index 37a7f3920817..ba63e01b7197 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java +++ b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java @@ -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; @@ -32,6 +33,8 @@ */ public final class GarbageCollector { + private static final Logger logger = Logger.getLogger(GarbageCollector.class.getName()); + private static final AttributeKey GC_KEY = AttributeKey.stringKey("gc"); private static final AttributeKey ACTION_KEY = AttributeKey.stringKey("action"); @@ -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(), @@ -110,5 +120,15 @@ 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() {} } From bc3de4fb1702ecdb625474ba4ccc3ba3d22161a4 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 13 Dec 2022 16:28:49 +0100 Subject: [PATCH 2/2] spotless --- .../instrumentation/runtimemetrics/GarbageCollector.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java index ba63e01b7197..6a9481ce2f85 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java +++ b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java @@ -122,7 +122,9 @@ private static GarbageCollectionNotificationInfo extractNotificationInfo( private static boolean isNotificationClassPresent() { try { - Class.forName("com.sun.management.GarbageCollectionNotificationInfo", false, + Class.forName( + "com.sun.management.GarbageCollectionNotificationInfo", + false, GarbageCollectorMXBean.class.getClassLoader()); return true; } catch (ClassNotFoundException e) {