Skip to content
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
@@ -0,0 +1,62 @@
package nl.vpro.monitoring.binder;

import io.micrometer.common.lang.NonNull;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.BaseUnits;
import io.micrometer.core.instrument.binder.MeterBinder;

import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicLong;

import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.VMOption;

import static java.util.Collections.emptyList;

public class JvmMaxDirectMemorySize implements MeterBinder {

private static final String MAX_DIRECT_MEMORY_SIZE = "MaxDirectMemorySize";
private final Iterable<Tag> tags;
private final AtomicLong maxDirectMemorySize = new AtomicLong();

public JvmMaxDirectMemorySize() {
this(emptyList());
}

public JvmMaxDirectMemorySize(Iterable<Tag> tags) {
this.tags = tags;
}


@Override
public void bindTo(@NonNull MeterRegistry registry) {
setMaxDirectMemorySize();
Iterable<Tag> tagsWithId = Tags.concat(tags, "id", "direct");

Gauge.builder("jvm.buffer.memory.max.bytes", maxDirectMemorySize, AtomicLong::get)
.tags(tagsWithId)
.description("The maximum number of bytes that can be allocated to direct memory")
.baseUnit(BaseUnits.BYTES)
.strongReference(true)
.register(registry);

}

private void setMaxDirectMemorySize() {

HotSpotDiagnosticMXBean diagnosticMXBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
maxDirectMemorySize.set(0);
if (diagnosticMXBean != null) {
VMOption option = diagnosticMXBean.getVMOption(MAX_DIRECT_MEMORY_SIZE);
if (option != null) {
try {
maxDirectMemorySize.set(Long.parseLong(option.getValue()));
} catch (NumberFormatException ignored) {
}
}
}
if (maxDirectMemorySize.get() == 0) {
maxDirectMemorySize.set(Runtime.getRuntime().maxMemory());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nl.vpro.monitoring.config;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.cache.JCacheMetrics;
import io.micrometer.core.instrument.binder.db.PostgreSQLDatabaseMetrics;
import io.micrometer.core.instrument.binder.jvm.*;
Expand Down Expand Up @@ -39,6 +37,7 @@

import nl.vpro.logging.Slf4jHelper;
import nl.vpro.logging.simple.Level;
import nl.vpro.monitoring.binder.JvmMaxDirectMemorySize;
import nl.vpro.util.locker.ObjectLocker;
import nl.vpro.util.locker.ObjectLockerAdmin;

Expand Down Expand Up @@ -154,6 +153,7 @@ protected void configure(MeterRegistry registry) {
}
if (properties.isMeterJvmMemory()) {
new JvmMemoryMetrics().bindTo(registry);
new JvmMaxDirectMemorySize().bindTo(registry);
}
if (properties.isMeterJvmThread()) {
new JvmThreadMetrics().bindTo(registry);
Expand Down