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

Unregister jmx gc metrics #8650

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
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 @@ -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