Skip to content

Commit

Permalink
Use metric-schema to report JVM Buffer Pool metrics (#2018)
Browse files Browse the repository at this point in the history
Use metric-schema to report JVM Buffer Pool metrics
  • Loading branch information
mpritham authored Sep 12, 2024
1 parent fd442b9 commit 772880f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2018.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Use metric-schema to report JVM Buffer Pool metrics
links:
- https://github.com/palantir/tritium/pull/2018
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.codahale.metrics.Gauge;
import com.codahale.metrics.RatioGauge;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.ThreadDeadlockDetector;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Suppliers;
Expand All @@ -31,6 +30,7 @@
import com.palantir.tritium.metrics.jvm.InternalJvmMetrics.AttributeUptime_EnablePreview;
import com.palantir.tritium.metrics.jvm.InternalJvmMetrics.DnsCacheTtlSeconds_Cache;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.lang.management.BufferPoolMXBean;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
Expand Down Expand Up @@ -69,8 +69,7 @@ public static void register(TaggedMetricRegistry registry) {
OperatingSystemMetrics.register(registry);
SafepointMetrics.register(registry);
registerAttributes(metrics);
MetricRegistries.registerAll(
registry, "jvm.buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()));
registerJvmBufferPools(registry);
registerClassLoading(metrics);
registerJvmMemory(registry);
registerThreads(metrics);
Expand Down Expand Up @@ -216,6 +215,22 @@ protected RatioGauge.Ratio getRatio() {
});
}

private static void registerJvmBufferPools(TaggedMetricRegistry registry) {
JvmBuffersMetrics jvmBuffersMetrics = JvmBuffersMetrics.of(registry);
for (BufferPoolMXBean pool : ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class)) {
String poolName = pool.getName();
if ("direct".equals(poolName)) {
jvmBuffersMetrics.directCount(nonNegative(pool::getCount));
jvmBuffersMetrics.directUsed(nonNegative(pool::getMemoryUsed));
jvmBuffersMetrics.directCapacity(nonNegative(pool::getTotalCapacity));
} else if ("mapped".equals(poolName)) {
jvmBuffersMetrics.mappedCount(nonNegative(pool::getCount));
jvmBuffersMetrics.mappedUsed(nonNegative(pool::getMemoryUsed));
jvmBuffersMetrics.mappedCapacity(nonNegative(pool::getTotalCapacity));
}
}
}

/**
* Computes the total heap + non-heap result of applying the specified function.
* If either value is negative, returns null to avoid misleading metric data.
Expand Down
23 changes: 22 additions & 1 deletion tritium-metrics-jvm/src/main/metrics/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,25 @@ namespaces:
docs: Amount of committed memory in bytes for the JVM non-heap (e.g. direct memory).
non-heap.usage:
type: gauge
docs: Ratio of `jvm.memory.non-heap.used` to `jvm.memory.non-heap.max`.
docs: Ratio of `jvm.memory.non-heap.used` to `jvm.memory.non-heap.max`.
jvm.buffers:
docs: Java virtual machine buffer pool metrics.
metrics:
direct.count:
type: gauge
docs: Number of buffers in the direct buffer pool.
direct.used:
type: gauge
docs: Total memory used by the direct buffer pool.
direct.capacity:
type: gauge
docs: Total capacity of the direct buffer pool.
mapped.count:
type: gauge
docs: Number of buffers in the mapped buffer pool.
mapped.used:
type: gauge
docs: Total memory used by the mapped buffer pool.
mapped.capacity:
type: gauge
docs: Total capacity of the mapped buffer pool.

0 comments on commit 772880f

Please sign in to comment.