diff --git a/src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java b/src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java index 6e12fdbb..391ec1ee 100644 --- a/src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java +++ b/src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java @@ -17,11 +17,10 @@ package io.vertx.micrometer.impl; import io.micrometer.core.instrument.Tag; -import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; -import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; -import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; -import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; +import io.micrometer.core.instrument.binder.MeterBinder; +import io.micrometer.core.instrument.binder.jvm.*; import io.micrometer.core.instrument.binder.system.ProcessorMetrics; +import io.micrometer.core.instrument.binder.system.UptimeMetrics; import io.vertx.core.datagram.DatagramSocketOptions; import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpServerOptions; @@ -35,10 +34,8 @@ import io.vertx.micrometer.backends.BackendRegistry; import io.vertx.micrometer.impl.meters.LongGauges; -import java.util.Collections; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.Set; +import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function; import static io.vertx.core.metrics.impl.DummyVertxMetrics.*; @@ -53,7 +50,8 @@ public class VertxMetricsImpl extends AbstractMetrics implements VertxMetrics { private final BackendRegistry backendRegistry; private final String registryName; private final Set disabledCategories; - private final JvmGcMetrics jvmGcMetrics; + private final boolean bindJvmMetrics; + private final List meterBinders; private final Function> serverRequestTagsProvider; private final Function> clientRequestTagsProvider; @@ -66,22 +64,32 @@ public VertxMetricsImpl(MicrometerMetricsOptions options, BackendRegistry backen } else { disabledCategories = Collections.emptySet(); } - jvmGcMetrics = options.isJvmMetricsEnabled() ? new JvmGcMetrics() : null; + bindJvmMetrics = options.isJvmMetricsEnabled(); + meterBinders = new CopyOnWriteArrayList<>(); serverRequestTagsProvider = options.getServerRequestTagsProvider(); clientRequestTagsProvider = options.getClientRequestTagsProvider(); } void init() { backendRegistry.init(); - if (jvmGcMetrics != null) { - new ClassLoaderMetrics().bindTo(registry); - new JvmMemoryMetrics().bindTo(registry); - jvmGcMetrics.bindTo(registry); - new ProcessorMetrics().bindTo(registry); - new JvmThreadMetrics().bindTo(registry); + if (bindJvmMetrics) { + addMeterBinder(new ClassLoaderMetrics()); + addMeterBinder(new JvmCompilationMetrics()); + addMeterBinder(new JvmGcMetrics()); + addMeterBinder(new JvmHeapPressureMetrics()); + addMeterBinder(new JvmInfoMetrics()); + addMeterBinder(new JvmMemoryMetrics()); + addMeterBinder(new JvmThreadMetrics()); + addMeterBinder(new ProcessorMetrics()); + addMeterBinder(new UptimeMetrics()); } } + private void addMeterBinder(MeterBinder meterBinder) { + meterBinders.add(meterBinder); + meterBinder.bindTo(registry); + } + @Override public EventBusMetrics createEventBusMetrics() { if (disabledCategories.contains(EVENT_BUS.toCategory())) { @@ -153,8 +161,14 @@ public boolean isMetricsEnabled() { @Override public void close() { - if (jvmGcMetrics != null) { - jvmGcMetrics.close(); + for (MeterBinder meterBinder : meterBinders) { + if (meterBinder instanceof AutoCloseable) { + AutoCloseable closeable = (AutoCloseable) meterBinder; + try { + closeable.close(); + } catch (Exception ignored) { + } + } } BackendRegistries.stop(registryName); if (meterCache != null) { diff --git a/src/test/java/io/vertx/micrometer/ExternalConfigurationTest.java b/src/test/java/io/vertx/micrometer/ExternalConfigurationTest.java index 14d7132e..815c226d 100644 --- a/src/test/java/io/vertx/micrometer/ExternalConfigurationTest.java +++ b/src/test/java/io/vertx/micrometer/ExternalConfigurationTest.java @@ -86,10 +86,16 @@ public void testJvmMetricsEnabled(TestContext context) throws Exception { Set metrics = PrometheusTestHelper.getMetricNames(vertx, context, 9999, "localhost", "/metrics", 3000); assertThat(metrics).contains( - "jvm_classes_loaded_classes", // from classloader metrics - "jvm_buffer_count_buffers", // from JVM memory metrics - "system_cpu_count", // from processor metrics - "jvm_threads_live_threads"); // from JVM thread metrics + "jvm_classes_loaded_classes", // from ClassLoaderMetrics + "jvm_compilation_time_ms_total", // from JvmCompilationMetrics + "jvm_gc_memory_promoted_bytes_total", // from JvmGcMetrics + "jvm_gc_overhead_percent", // from JvmHeapPressureMetrics + "jvm_info", // from JvmInfoMetrics + "jvm_buffer_count_buffers", // from JvmMemoryMetrics + "jvm_threads_live_threads", // from JvmThreadMetrics + "system_cpu_count", // from ProcessorMetrics + "process_uptime_seconds" // from UptimeMetrics + ); } private void startVertx(TestContext context, MicrometerMetricsOptions metricsOptions) throws Exception {