From 4b0e20b791bf534d4f6e29779d4dd270c7efc406 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 5 Jun 2023 14:54:19 +0300 Subject: [PATCH] Unregister jmx gc metrics --- .../runtimemetrics/java17/RuntimeMetrics.java | 2 +- .../java8/Java8RuntimeMetricsInstaller.java | 2 +- .../runtimemetrics/java8/GarbageCollector.java | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetrics.java b/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetrics.java index 813c3ccff332..cde0b020928a 100644 --- a/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetrics.java +++ b/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetrics.java @@ -53,7 +53,7 @@ public final class RuntimeMetrics implements Closeable { observables.addAll(Cpu.registerObservers(openTelemetry)); observables.addAll(MemoryPools.registerObservers(openTelemetry)); observables.addAll(Threads.registerObservers(openTelemetry)); - GarbageCollector.registerObservers(openTelemetry); + observables.addAll(GarbageCollector.registerObservers(openTelemetry)); } } catch (Exception e) { close(); diff --git a/instrumentation/runtime-metrics/runtime-metrics-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/Java8RuntimeMetricsInstaller.java b/instrumentation/runtime-metrics/runtime-metrics-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/Java8RuntimeMetricsInstaller.java index b7f3b75c3413..9a5434a09ba6 100644 --- a/instrumentation/runtime-metrics/runtime-metrics-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/Java8RuntimeMetricsInstaller.java +++ b/instrumentation/runtime-metrics/runtime-metrics-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/Java8RuntimeMetricsInstaller.java @@ -41,7 +41,7 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) { observables.addAll(Cpu.registerObservers(openTelemetry)); observables.addAll(MemoryPools.registerObservers(openTelemetry)); observables.addAll(Threads.registerObservers(openTelemetry)); - GarbageCollector.registerObservers(openTelemetry); + observables.addAll(GarbageCollector.registerObservers(openTelemetry)); Thread cleanupTelemetry = new Thread( () -> { diff --git a/instrumentation/runtime-metrics/runtime-metrics-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/GarbageCollector.java b/instrumentation/runtime-metrics/runtime-metrics-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/GarbageCollector.java index 5d5dc96b478f..182a4e0c7e9a 100644 --- a/instrumentation/runtime-metrics/runtime-metrics-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/GarbageCollector.java +++ b/instrumentation/runtime-metrics/runtime-metrics-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/GarbageCollector.java @@ -18,6 +18,8 @@ import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsUtil; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.function.Function; @@ -53,22 +55,22 @@ public final class GarbageCollector { .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION); /** Register observers for java runtime memory metrics. */ - public static void registerObservers(OpenTelemetry openTelemetry) { + public static List registerObservers(OpenTelemetry openTelemetry) { if (!isNotificationClassPresent()) { logger.fine( "The com.sun.management.GarbageCollectionNotificationInfo class is not available;" + " GC metrics will not be reported."); - return; + return Collections.emptyList(); } - registerObservers( + return registerObservers( openTelemetry, ManagementFactory.getGarbageCollectorMXBeans(), GarbageCollector::extractNotificationInfo); } // Visible for testing - static void registerObservers( + static List registerObservers( OpenTelemetry openTelemetry, List gcBeans, Function notificationInfoExtractor) { @@ -82,14 +84,18 @@ static void registerObservers( setGcDurationBuckets(gcDurationBuilder); DoubleHistogram gcDuration = gcDurationBuilder.build(); + List result = new ArrayList<>(); for (GarbageCollectorMXBean gcBean : gcBeans) { if (!(gcBean instanceof NotificationEmitter)) { continue; } NotificationEmitter notificationEmitter = (NotificationEmitter) gcBean; - notificationEmitter.addNotificationListener( - new GcNotificationListener(gcDuration, notificationInfoExtractor), GC_FILTER, null); + GcNotificationListener listener = + new GcNotificationListener(gcDuration, notificationInfoExtractor); + notificationEmitter.addNotificationListener(listener, GC_FILTER, null); + result.add(() -> notificationEmitter.removeNotificationListener(listener)); } + return result; } private static void setGcDurationBuckets(DoubleHistogramBuilder builder) {