Skip to content

Commit

Permalink
Add more JVM metrics (#203)
Browse files Browse the repository at this point in the history
Closes #202

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont authored Oct 17, 2023
1 parent 28f3fe5 commit c6c1d35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
50 changes: 32 additions & 18 deletions src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand All @@ -53,7 +50,8 @@ public class VertxMetricsImpl extends AbstractMetrics implements VertxMetrics {
private final BackendRegistry backendRegistry;
private final String registryName;
private final Set<String> disabledCategories;
private final JvmGcMetrics jvmGcMetrics;
private final boolean bindJvmMetrics;
private final List<MeterBinder> meterBinders;
private final Function<HttpRequest, Iterable<Tag>> serverRequestTagsProvider;
private final Function<HttpRequest, Iterable<Tag>> clientRequestTagsProvider;

Expand All @@ -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())) {
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/io/vertx/micrometer/ExternalConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ public void testJvmMetricsEnabled(TestContext context) throws Exception {

Set<String> 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 {
Expand Down

0 comments on commit c6c1d35

Please sign in to comment.