Skip to content

Commit

Permalink
Ensure that all AutoCloseable binders are closed
Browse files Browse the repository at this point in the history
Fixes: #422335
  • Loading branch information
geoand committed Aug 8, 2024
1 parent a34e10f commit dde46df
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ public void configureRegistries(MicrometerConfig config,
}
}

List<AutoCloseable> autoCloseables = new ArrayList<>();

// Base JVM Metrics
if (config.checkBinderEnabledWithDefault(() -> config.binder.jvm)) {
new ClassLoaderMetrics().bindTo(Metrics.globalRegistry);
new JvmHeapPressureMetrics().bindTo(Metrics.globalRegistry);
JvmHeapPressureMetrics jvmHeapPressureMetrics = new JvmHeapPressureMetrics();
jvmHeapPressureMetrics.bindTo(Metrics.globalRegistry);
autoCloseables.add(jvmHeapPressureMetrics);
new JvmMemoryMetrics().bindTo(Metrics.globalRegistry);
new JvmThreadMetrics().bindTo(Metrics.globalRegistry);
new JVMInfoBinder().bindTo(Metrics.globalRegistry);
if (ImageMode.current() == ImageMode.JVM) {
new JvmGcMetrics().bindTo(Metrics.globalRegistry);
JvmGcMetrics jvmGcMetrics = new JvmGcMetrics();
jvmGcMetrics.bindTo(Metrics.globalRegistry);
autoCloseables.add(jvmGcMetrics);
}
}

Expand Down Expand Up @@ -149,6 +155,14 @@ public void run() {
meterRegistry.close();
Metrics.removeRegistry(meterRegistry);
}
// iterate over the auto-closeables and close them
for (AutoCloseable autoCloseable : autoCloseables) {
try {
autoCloseable.close();
} catch (Exception e) {
log.error("Error closing", e);
}
}
}
});
}
Expand Down

0 comments on commit dde46df

Please sign in to comment.