Skip to content

Commit

Permalink
Unregister jmx gc metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Jun 5, 2023
1 parent 65c5972 commit 9e641b5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AutoCloseable> 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<AutoCloseable> registerObservers(
OpenTelemetry openTelemetry,
List<GarbageCollectorMXBean> gcBeans,
Function<Notification, GarbageCollectionNotificationInfo> notificationInfoExtractor) {
Expand All @@ -82,14 +84,18 @@ static void registerObservers(
setGcDurationBuckets(gcDurationBuilder);
DoubleHistogram gcDuration = gcDurationBuilder.build();

List<AutoCloseable> 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) {
Expand Down

0 comments on commit 9e641b5

Please sign in to comment.