From a5c17bd082c083ed4030a757904938ff81e3c26b Mon Sep 17 00:00:00 2001 From: Sibasis Padhi Date: Thu, 25 Dec 2025 19:16:41 -0600 Subject: [PATCH 1/2] feat(jvm): add constLabels support to JvmMetrics.builder().register() [Fixes #1694] Signed-off-by: Sibasis Padhi --- .../jvm/JvmBufferPoolMetrics.java | 20 ++++- .../jvm/JvmClassLoadingMetrics.java | 33 +++++--- .../jvm/JvmCompilationMetrics.java | 23 +++-- .../jvm/JvmGarbageCollectorMetrics.java | 22 +++-- .../instrumentation/jvm/JvmMemoryMetrics.java | 84 ++++++++++++------- .../jvm/JvmMemoryPoolAllocationMetrics.java | 30 ++++--- .../instrumentation/jvm/JvmMetrics.java | 30 ++++--- .../jvm/JvmNativeMemoryMetrics.java | 19 ++++- .../jvm/JvmRuntimeInfoMetric.java | 34 +++++--- .../jvm/JvmThreadsMetrics.java | 44 ++++++---- .../instrumentation/jvm/ProcessMetrics.java | 50 +++++++---- .../instrumentation/jvm/JvmMetricsTest.java | 23 +++++ 12 files changed, 287 insertions(+), 125 deletions(-) diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java index 7f45c8303..f9f6d76f3 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java @@ -4,6 +4,7 @@ import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.BufferPoolMXBean; import java.lang.management.ManagementFactory; import java.util.List; @@ -48,11 +49,13 @@ public class JvmBufferPoolMetrics { private final PrometheusProperties config; private final List bufferPoolBeans; + private final Labels constLabels; private JvmBufferPoolMetrics( - List bufferPoolBeans, PrometheusProperties config) { + List bufferPoolBeans, PrometheusProperties config, Labels constLabels) { this.config = config; this.bufferPoolBeans = bufferPoolBeans; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { @@ -68,6 +71,7 @@ private void register(PrometheusRegistry registry) { callback.call(pool.getMemoryUsed(), pool.getName()); } }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -81,6 +85,7 @@ private void register(PrometheusRegistry registry) { callback.call(pool.getTotalCapacity(), pool.getName()); } }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -93,6 +98,7 @@ private void register(PrometheusRegistry registry) { callback.call(pool.getCount(), pool.getName()); } }) + .constLabels(constLabels) .register(registry); } @@ -106,13 +112,19 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private List bufferPoolBeans; + private final PrometheusProperties config; + @Nullable private List bufferPoolBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder bufferPoolBeans(List bufferPoolBeans) { this.bufferPoolBeans = bufferPoolBeans; @@ -128,7 +140,7 @@ public void register(PrometheusRegistry registry) { if (bufferPoolBeans == null) { bufferPoolBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); } - new JvmBufferPoolMetrics(bufferPoolBeans, config).register(registry); + new JvmBufferPoolMetrics(bufferPoolBeans, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java index bc0465a87..8970dfe9f 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java @@ -4,6 +4,7 @@ import io.prometheus.metrics.core.metrics.CounterWithCallback; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; import javax.annotation.Nullable; @@ -44,34 +45,40 @@ public class JvmClassLoadingMetrics { private final PrometheusProperties config; private final ClassLoadingMXBean classLoadingBean; + private final Labels constLabels; - private JvmClassLoadingMetrics(ClassLoadingMXBean classLoadingBean, PrometheusProperties config) { + private JvmClassLoadingMetrics( + ClassLoadingMXBean classLoadingBean, PrometheusProperties config, Labels constLabels) { this.classLoadingBean = classLoadingBean; this.config = config; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_CLASSES_CURRENTLY_LOADED) .help("The number of classes that are currently loaded in the JVM") .callback(callback -> callback.call(classLoadingBean.getLoadedClassCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_CLASSES_LOADED_TOTAL) .help( "The total number of classes that have been loaded since the JVM has started execution") .callback(callback -> callback.call(classLoadingBean.getTotalLoadedClassCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_CLASSES_UNLOADED_TOTAL) .help( "The total number of classes that have been unloaded since the JVM has " + "started execution") .callback(callback -> callback.call(classLoadingBean.getUnloadedClassCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -84,13 +91,19 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private ClassLoadingMXBean classLoadingBean; + private final PrometheusProperties config; + @Nullable private ClassLoadingMXBean classLoadingBean; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder classLoadingBean(ClassLoadingMXBean classLoadingBean) { this.classLoadingBean = classLoadingBean; @@ -106,7 +119,7 @@ public void register(PrometheusRegistry registry) { this.classLoadingBean != null ? this.classLoadingBean : ManagementFactory.getClassLoadingMXBean(); - new JvmClassLoadingMetrics(classLoadingBean, config).register(registry); + new JvmClassLoadingMetrics(classLoadingBean, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java index dfde6c539..9a3d1bade 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java @@ -6,6 +6,7 @@ import io.prometheus.metrics.core.metrics.CounterWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.CompilationMXBean; import java.lang.management.ManagementFactory; import javax.annotation.Nullable; @@ -39,10 +40,13 @@ public class JvmCompilationMetrics { private final PrometheusProperties config; private final CompilationMXBean compilationBean; + private final Labels constLabels; - private JvmCompilationMetrics(CompilationMXBean compilationBean, PrometheusProperties config) { + private JvmCompilationMetrics( + CompilationMXBean compilationBean, PrometheusProperties config, Labels constLabels) { this.compilationBean = compilationBean; this.config = config; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { @@ -51,13 +55,14 @@ private void register(PrometheusRegistry registry) { return; } - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_COMPILATION_TIME_SECONDS_TOTAL) .help("The total time in seconds taken for HotSpot class compilation") .unit(Unit.SECONDS) .callback( callback -> callback.call(millisToSeconds(compilationBean.getTotalCompilationTime()))) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -70,13 +75,19 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private CompilationMXBean compilationBean; + private final PrometheusProperties config; + @Nullable private CompilationMXBean compilationBean; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder compilationBean(CompilationMXBean compilationBean) { this.compilationBean = compilationBean; @@ -92,7 +103,7 @@ public void register(PrometheusRegistry registry) { this.compilationBean != null ? this.compilationBean : ManagementFactory.getCompilationMXBean(); - new JvmCompilationMetrics(compilationBean, config).register(registry); + new JvmCompilationMetrics(compilationBean, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java index 262e2df5f..e2a954b2f 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java @@ -5,6 +5,7 @@ import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Quantiles; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.List; @@ -42,16 +43,18 @@ public class JvmGarbageCollectorMetrics { private final PrometheusProperties config; private final List garbageCollectorBeans; + private final Labels constLabels; private JvmGarbageCollectorMetrics( - List garbageCollectorBeans, PrometheusProperties config) { + List garbageCollectorBeans, PrometheusProperties config, Labels constLabels) { this.config = config; this.garbageCollectorBeans = garbageCollectorBeans; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - SummaryWithCallback.builder(config) + SummaryWithCallback.builder(config) .name(JVM_GC_COLLECTION_SECONDS) .help("Time spent in a given JVM garbage collector in seconds.") .unit(Unit.SECONDS) @@ -66,7 +69,8 @@ private void register(PrometheusRegistry registry) { gc.getName()); } }) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -79,13 +83,19 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private List garbageCollectorBeans; + private final PrometheusProperties config; + @Nullable private List garbageCollectorBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder garbageCollectorBeans(List garbageCollectorBeans) { this.garbageCollectorBeans = garbageCollectorBeans; @@ -101,7 +111,7 @@ public void register(PrometheusRegistry registry) { if (garbageCollectorBeans == null) { garbageCollectorBeans = ManagementFactory.getGarbageCollectorMXBeans(); } - new JvmGarbageCollectorMetrics(garbageCollectorBeans, config).register(registry); + new JvmGarbageCollectorMetrics(garbageCollectorBeans, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java index cecfe3c8a..62bec5627 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java @@ -4,6 +4,7 @@ import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; @@ -127,23 +128,26 @@ public class JvmMemoryMetrics { private final PrometheusProperties config; private final MemoryMXBean memoryBean; private final List poolBeans; + private final Labels constLabels; private JvmMemoryMetrics( - List poolBeans, MemoryMXBean memoryBean, PrometheusProperties config) { + List poolBeans, MemoryMXBean memoryBean, PrometheusProperties config, Labels constLabels) { this.config = config; this.poolBeans = poolBeans; this.memoryBean = memoryBean; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_OBJECTS_PENDING_FINALIZATION) .help("The number of objects waiting in the finalizer queue.") .callback(callback -> callback.call(memoryBean.getObjectPendingFinalizationCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_USED_BYTES) .help("Used bytes of a given JVM memory area.") .unit(Unit.BYTES) @@ -153,9 +157,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getUsed(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getUsed(), "nonheap"); }) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_COMMITTED_BYTES) .help("Committed (bytes) of a given JVM memory area.") .unit(Unit.BYTES) @@ -165,9 +170,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getCommitted(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getCommitted(), "nonheap"); }) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_MAX_BYTES) .help("Max (bytes) of a given JVM memory area.") .unit(Unit.BYTES) @@ -177,9 +183,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getMax(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getMax(), "nonheap"); }) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_INIT_BYTES) .help("Initial bytes of a given JVM memory area.") .unit(Unit.BYTES) @@ -189,50 +196,56 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getInit(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getInit(), "nonheap"); }) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_USED_BYTES) .help("Used bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getUsed)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COMMITTED_BYTES) .help("Committed bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getCommitted)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_MAX_BYTES) .help("Max bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getMax)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_INIT_BYTES) .help("Initial bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getInit)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_USED_BYTES) .help("Used bytes after last collection of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getUsed)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_COMMITTED_BYTES) .help("Committed after last collection bytes of a given JVM memory pool.") .unit(Unit.BYTES) @@ -240,25 +253,28 @@ private void register(PrometheusRegistry registry) { .callback( makeCallback( poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getCommitted)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_MAX_BYTES) .help("Max bytes after last collection of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getMax)) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_INIT_BYTES) .help("Initial after last collection bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getInit)) - .register(registry); + .constLabels(constLabels) + .register(registry); } private Consumer makeCallback( @@ -285,14 +301,20 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private MemoryMXBean memoryBean; - @Nullable private List poolBeans; + private final PrometheusProperties config; + @Nullable private MemoryMXBean memoryBean; + @Nullable private List poolBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder withMemoryBean(MemoryMXBean memoryBean) { this.memoryBean = memoryBean; @@ -314,7 +336,7 @@ public void register(PrometheusRegistry registry) { this.memoryBean != null ? this.memoryBean : ManagementFactory.getMemoryMXBean(); List poolBeans = this.poolBeans != null ? this.poolBeans : ManagementFactory.getMemoryPoolMXBeans(); - new JvmMemoryMetrics(poolBeans, bean, config).register(registry); + new JvmMemoryMetrics(poolBeans, bean, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java index 160469b8b..dd998c873 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java @@ -5,6 +5,7 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.Counter; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryUsage; @@ -52,20 +53,23 @@ public class JvmMemoryPoolAllocationMetrics { "jvm_memory_pool_allocated_bytes_total"; private final List garbageCollectorBeans; + private final Labels constLabels; - private JvmMemoryPoolAllocationMetrics(List garbageCollectorBeans) { + private JvmMemoryPoolAllocationMetrics(List garbageCollectorBeans, Labels constLabels) { this.garbageCollectorBeans = garbageCollectorBeans; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - Counter allocatedCounter = - Counter.builder() - .name(JVM_MEMORY_POOL_ALLOCATED_BYTES_TOTAL) - .help( - "Total bytes allocated in a given JVM memory pool. Only updated after GC, " - + "not continuously.") - .labelNames("pool") - .register(registry); + Counter allocatedCounter = + Counter.builder() + .name(JVM_MEMORY_POOL_ALLOCATED_BYTES_TOTAL) + .help( + "Total bytes allocated in a given JVM memory pool. Only updated after GC, " + + "not continuously.") + .labelNames("pool") + .constLabels(constLabels) + .register(registry); AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter); @@ -152,8 +156,14 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { @Nullable private List garbageCollectorBeans; + private Labels constLabels = Labels.EMPTY; private Builder() {} + + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } /** Package private. For testing only. */ Builder withGarbageCollectorBeans(List garbageCollectorBeans) { @@ -170,7 +180,7 @@ public void register(PrometheusRegistry registry) { if (garbageCollectorBeans == null) { garbageCollectorBeans = ManagementFactory.getGarbageCollectorMXBeans(); } - new JvmMemoryPoolAllocationMetrics(garbageCollectorBeans).register(registry); + new JvmMemoryPoolAllocationMetrics(garbageCollectorBeans, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java index 0f5a56eee..b7efb437d 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java @@ -2,6 +2,7 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -31,12 +32,19 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; + private final PrometheusProperties config; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + /** Set constant labels that will be applied to all JVM metrics registered by this builder. */ + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** * Register all JVM metrics with the default registry. * @@ -55,16 +63,16 @@ public void register() { */ public void register(PrometheusRegistry registry) { if (REGISTERED.add(registry)) { - JvmThreadsMetrics.builder(config).register(registry); - JvmBufferPoolMetrics.builder(config).register(registry); - JvmClassLoadingMetrics.builder(config).register(registry); - JvmCompilationMetrics.builder(config).register(registry); - JvmGarbageCollectorMetrics.builder(config).register(registry); - JvmMemoryPoolAllocationMetrics.builder(config).register(registry); - JvmMemoryMetrics.builder(config).register(registry); - JvmNativeMemoryMetrics.builder(config).register(registry); - JvmRuntimeInfoMetric.builder(config).register(registry); - ProcessMetrics.builder(config).register(registry); + JvmThreadsMetrics.builder(config).constLabels(constLabels).register(registry); + JvmBufferPoolMetrics.builder(config).constLabels(constLabels).register(registry); + JvmClassLoadingMetrics.builder(config).constLabels(constLabels).register(registry); + JvmCompilationMetrics.builder(config).constLabels(constLabels).register(registry); + JvmGarbageCollectorMetrics.builder(config).constLabels(constLabels).register(registry); + JvmMemoryPoolAllocationMetrics.builder(config).constLabels(constLabels).register(registry); + JvmMemoryMetrics.builder(config).constLabels(constLabels).register(registry); + JvmNativeMemoryMetrics.builder(config).constLabels(constLabels).register(registry); + JvmRuntimeInfoMetric.builder(config).constLabels(constLabels).register(registry); + ProcessMetrics.builder(config).constLabels(constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java index 53225edca..79fc142dc 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java @@ -4,6 +4,7 @@ import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.ManagementFactory; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; @@ -97,17 +98,19 @@ public class JvmNativeMemoryMetrics { private final PrometheusProperties config; private final PlatformMBeanServerAdapter adapter; + private final Labels constLabels; - private JvmNativeMemoryMetrics(PrometheusProperties config, PlatformMBeanServerAdapter adapter) { + private JvmNativeMemoryMetrics(PrometheusProperties config, PlatformMBeanServerAdapter adapter, Labels constLabels) { this.config = config; this.adapter = adapter; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { // first call will check if enabled and set the flag vmNativeMemorySummaryInBytesOrEmpty(); if (isEnabled.get()) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_NATIVE_MEMORY_RESERVED_BYTES) .help( "Reserved bytes of a given JVM. Reserved memory represents the total amount of " @@ -115,9 +118,10 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(true)) + .constLabels(constLabels) .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_NATIVE_MEMORY_COMMITTED_BYTES) .help( "Committed bytes of a given JVM. Committed memory represents the amount of " @@ -125,6 +129,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(false)) + .constLabels(constLabels) .register(registry); } } @@ -200,6 +205,7 @@ public static class Builder { private final PrometheusProperties config; private final PlatformMBeanServerAdapter adapter; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this(config, new DefaultPlatformMBeanServerAdapter()); @@ -211,12 +217,17 @@ private Builder(PrometheusProperties config) { this.adapter = adapter; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + public void register() { register(PrometheusRegistry.defaultRegistry); } public void register(PrometheusRegistry registry) { - new JvmNativeMemoryMetrics(config, adapter).register(registry); + new JvmNativeMemoryMetrics(config, adapter, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java index 5ebd61c21..d241970a7 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java @@ -3,6 +3,7 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.Info; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import javax.annotation.Nullable; /** @@ -33,23 +34,26 @@ public class JvmRuntimeInfoMetric { private final String version; private final String vendor; private final String runtime; + private final Labels constLabels; private JvmRuntimeInfoMetric( - String version, String vendor, String runtime, PrometheusProperties config) { + String version, String vendor, String runtime, PrometheusProperties config, Labels constLabels) { this.config = config; this.version = version; this.vendor = vendor; this.runtime = runtime; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - Info jvmInfo = - Info.builder(config) - .name(JVM_RUNTIME_INFO) - .help("JVM runtime info") - .labelNames("version", "vendor", "runtime") - .register(registry); + Info jvmInfo = + Info.builder(config) + .name(JVM_RUNTIME_INFO) + .help("JVM runtime info") + .labelNames("version", "vendor", "runtime") + .constLabels(constLabels) + .register(registry); jvmInfo.setLabelValues(version, vendor, runtime); } @@ -64,15 +68,21 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private String version; - @Nullable private String vendor; - @Nullable private String runtime; + private final PrometheusProperties config; + @Nullable private String version; + @Nullable private String vendor; + @Nullable private String runtime; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + /** Package private. For testing only. */ Builder version(String version) { this.version = version; @@ -104,7 +114,7 @@ public void register(PrometheusRegistry registry) { this.vendor != null ? this.vendor : System.getProperty("java.vm.vendor", "unknown"); String runtime = this.runtime != null ? this.runtime : System.getProperty("java.runtime.name", "unknown"); - new JvmRuntimeInfoMetric(version, vendor, runtime, config).register(registry); + new JvmRuntimeInfoMetric(version, vendor, runtime, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java index d3c69795e..e407bc502 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java @@ -6,6 +6,7 @@ import io.prometheus.metrics.core.metrics.CounterWithCallback; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; @@ -74,57 +75,65 @@ public class JvmThreadsMetrics { private final PrometheusProperties config; private final ThreadMXBean threadBean; private final boolean isNativeImage; + private final Labels constLabels; private JvmThreadsMetrics( - boolean isNativeImage, ThreadMXBean threadBean, PrometheusProperties config) { + boolean isNativeImage, ThreadMXBean threadBean, PrometheusProperties config, Labels constLabels) { this.config = config; this.threadBean = threadBean; this.isNativeImage = isNativeImage; + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_CURRENT) .help("Current thread count of a JVM") .callback(callback -> callback.call(threadBean.getThreadCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DAEMON) .help("Daemon thread count of a JVM") .callback(callback -> callback.call(threadBean.getDaemonThreadCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_PEAK) .help("Peak thread count of a JVM") .callback(callback -> callback.call(threadBean.getPeakThreadCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_THREADS_STARTED_TOTAL) .help("Started thread count of a JVM") .callback(callback -> callback.call(threadBean.getTotalStartedThreadCount())) - .register(registry); + .constLabels(constLabels) + .register(registry); if (!isNativeImage) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DEADLOCKED) .help( "Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or " + "ownable synchronizers") .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findDeadlockedThreads()))) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DEADLOCKED_MONITOR) .help("Cycles of JVM-threads that are in deadlock waiting to acquire object monitors") .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findMonitorDeadlockedThreads()))) - .register(registry); + .constLabels(constLabels) + .register(registry); GaugeWithCallback.builder(config) .name(JVM_THREADS_STATE) @@ -137,6 +146,7 @@ private void register(PrometheusRegistry registry) { callback.call(entry.getValue(), entry.getKey()); } }) + .constLabels(constLabels) .register(registry); } } @@ -196,10 +206,16 @@ public static class Builder { private final PrometheusProperties config; @Nullable private Boolean isNativeImage; @Nullable private ThreadMXBean threadBean; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; } + + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } /** Package private. For testing only. */ Builder threadBean(ThreadMXBean threadBean) { @@ -222,7 +238,7 @@ public void register(PrometheusRegistry registry) { this.threadBean != null ? this.threadBean : ManagementFactory.getThreadMXBean(); boolean isNativeImage = this.isNativeImage != null ? this.isNativeImage : NativeImageChecker.isGraalVmNativeImage; - new JvmThreadsMetrics(isNativeImage, threadBean, config).register(registry); + new JvmThreadsMetrics(isNativeImage, threadBean, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java index e3363c868..ca0312560 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java @@ -5,6 +5,7 @@ import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.Unit; +import io.prometheus.metrics.model.snapshots.Labels; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -82,22 +83,25 @@ public class ProcessMetrics { private final RuntimeMXBean runtimeBean; private final Grepper grepper; private final boolean linux; + private final Labels constLabels; private ProcessMetrics( OperatingSystemMXBean osBean, RuntimeMXBean runtimeBean, Grepper grepper, - PrometheusProperties config) { + PrometheusProperties config, + Labels constLabels) { this.osBean = osBean; this.runtimeBean = runtimeBean; this.grepper = grepper; this.config = config; this.linux = PROC_SELF_STATUS.canRead(); + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(PROCESS_CPU_SECONDS_TOTAL) .help("Total user and system CPU time spent in seconds.") .unit(Unit.SECONDS) @@ -117,17 +121,19 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_START_TIME_SECONDS) .help("Start time of the process since unix epoch in seconds.") .unit(Unit.SECONDS) .callback(callback -> callback.call(Unit.millisToSeconds(runtimeBean.getStartTime()))) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_OPEN_FDS) .help("Number of open file descriptors.") .callback( @@ -140,10 +146,11 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_MAX_FDS) .help("Maximum number of open file descriptors.") .callback( @@ -156,12 +163,13 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); if (linux) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_VIRTUAL_MEMORY_BYTES) .help("Virtual memory size in bytes.") .unit(Unit.BYTES) @@ -174,9 +182,10 @@ private void register(PrometheusRegistry registry) { // Ignored } }) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_RESIDENT_MEMORY_BYTES) .help("Resident memory size in bytes.") .unit(Unit.BYTES) @@ -189,7 +198,8 @@ private void register(PrometheusRegistry registry) { // Ignored } }) - .register(registry); + .constLabels(constLabels) + .register(registry); } } @@ -275,6 +285,7 @@ public static class Builder { @Nullable private OperatingSystemMXBean osBean; @Nullable private RuntimeMXBean runtimeBean; @Nullable private Grepper grepper; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; @@ -298,6 +309,11 @@ Builder grepper(Grepper grepper) { return this; } + public Builder constLabels(Labels constLabels) { + this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; + return this; + } + public void register() { register(PrometheusRegistry.defaultRegistry); } @@ -308,7 +324,7 @@ public void register(PrometheusRegistry registry) { RuntimeMXBean bean = this.runtimeBean != null ? this.runtimeBean : ManagementFactory.getRuntimeMXBean(); Grepper grepper = this.grepper != null ? this.grepper : new FileGrepper(); - new ProcessMetrics(osBean, bean, grepper, config).register(registry); + new ProcessMetrics(osBean, bean, grepper, config, constLabels).register(registry); } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java index 2bb908a37..96c0792b0 100644 --- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java +++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java @@ -4,6 +4,10 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.MetricSnapshots; +import io.prometheus.metrics.model.snapshots.MetricSnapshot; +import io.prometheus.metrics.model.snapshots.DataPointSnapshot; import java.lang.management.ManagementFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -32,6 +36,25 @@ void pool() { .register(); } + @Test + void testConstLabelsApplied() { + PrometheusRegistry registry = new PrometheusRegistry(); + Labels labels = Labels.of("env", "dev"); + JvmMetrics.builder().constLabels(labels).register(registry); + MetricSnapshots snapshots = registry.scrape(); + boolean found = false; + for (MetricSnapshot snapshot : snapshots) { + for (DataPointSnapshot dp : snapshot.getDataPoints()) { + if ("dev".equals(dp.getLabels().get("env"))) { + found = true; + break; + } + } + if (found) break; + } + assertThat(found).isTrue(); + } + @Test void testJvmMetrics() { JvmMetrics.builder(PrometheusProperties.get()).register(); From e2181bb18b39adeef5335ca89206ce044912be75 Mon Sep 17 00:00:00 2001 From: Sibasis Padhi Date: Thu, 25 Dec 2025 19:19:43 -0600 Subject: [PATCH 2/2] style(jvm): apply Google Java Format to all changed files for constLabels support (#1694) Signed-off-by: Sibasis Padhi --- .../jvm/JvmBufferPoolMetrics.java | 8 +- .../jvm/JvmClassLoadingMetrics.java | 24 ++--- .../jvm/JvmCompilationMetrics.java | 14 +-- .../jvm/JvmGarbageCollectorMetrics.java | 18 ++-- .../instrumentation/jvm/JvmMemoryMetrics.java | 93 ++++++++++--------- .../jvm/JvmMemoryPoolAllocationMetrics.java | 23 ++--- .../instrumentation/jvm/JvmMetrics.java | 4 +- .../jvm/JvmNativeMemoryMetrics.java | 9 +- .../jvm/JvmRuntimeInfoMetric.java | 30 +++--- .../jvm/JvmThreadsMetrics.java | 43 +++++---- .../instrumentation/jvm/ProcessMetrics.java | 44 ++++----- .../instrumentation/jvm/JvmMetricsTest.java | 4 +- 12 files changed, 164 insertions(+), 150 deletions(-) diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java index f9f6d76f3..52353722e 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java @@ -3,8 +3,8 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Unit; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.BufferPoolMXBean; import java.lang.management.ManagementFactory; import java.util.List; @@ -112,9 +112,9 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private List bufferPoolBeans; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private List bufferPoolBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java index 8970dfe9f..4cfa917ae 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java @@ -56,29 +56,29 @@ private JvmClassLoadingMetrics( private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_CLASSES_CURRENTLY_LOADED) .help("The number of classes that are currently loaded in the JVM") .callback(callback -> callback.call(classLoadingBean.getLoadedClassCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_CLASSES_LOADED_TOTAL) .help( "The total number of classes that have been loaded since the JVM has started execution") .callback(callback -> callback.call(classLoadingBean.getTotalLoadedClassCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_CLASSES_UNLOADED_TOTAL) .help( "The total number of classes that have been unloaded since the JVM has " + "started execution") .callback(callback -> callback.call(classLoadingBean.getUnloadedClassCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -91,9 +91,9 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private ClassLoadingMXBean classLoadingBean; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private ClassLoadingMXBean classLoadingBean; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java index 9a3d1bade..3c9af6e86 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java @@ -5,8 +5,8 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.CounterWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Unit; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.CompilationMXBean; import java.lang.management.ManagementFactory; import javax.annotation.Nullable; @@ -55,14 +55,14 @@ private void register(PrometheusRegistry registry) { return; } - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_COMPILATION_TIME_SECONDS_TOTAL) .help("The total time in seconds taken for HotSpot class compilation") .unit(Unit.SECONDS) .callback( callback -> callback.call(millisToSeconds(compilationBean.getTotalCompilationTime()))) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -75,9 +75,9 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private CompilationMXBean compilationBean; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private CompilationMXBean compilationBean; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java index e2a954b2f..00daaebfc 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java @@ -3,9 +3,9 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.SummaryWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.Labels; import io.prometheus.metrics.model.snapshots.Quantiles; import io.prometheus.metrics.model.snapshots.Unit; -import io.prometheus.metrics.model.snapshots.Labels; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.List; @@ -46,7 +46,9 @@ public class JvmGarbageCollectorMetrics { private final Labels constLabels; private JvmGarbageCollectorMetrics( - List garbageCollectorBeans, PrometheusProperties config, Labels constLabels) { + List garbageCollectorBeans, + PrometheusProperties config, + Labels constLabels) { this.config = config; this.garbageCollectorBeans = garbageCollectorBeans; this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; @@ -54,7 +56,7 @@ private JvmGarbageCollectorMetrics( private void register(PrometheusRegistry registry) { - SummaryWithCallback.builder(config) + SummaryWithCallback.builder(config) .name(JVM_GC_COLLECTION_SECONDS) .help("Time spent in a given JVM garbage collector in seconds.") .unit(Unit.SECONDS) @@ -69,8 +71,8 @@ private void register(PrometheusRegistry registry) { gc.getName()); } }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); } public static Builder builder() { @@ -83,9 +85,9 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private List garbageCollectorBeans; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private List garbageCollectorBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java index 62bec5627..0db99717b 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java @@ -3,8 +3,8 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Unit; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; @@ -131,7 +131,10 @@ public class JvmMemoryMetrics { private final Labels constLabels; private JvmMemoryMetrics( - List poolBeans, MemoryMXBean memoryBean, PrometheusProperties config, Labels constLabels) { + List poolBeans, + MemoryMXBean memoryBean, + PrometheusProperties config, + Labels constLabels) { this.config = config; this.poolBeans = poolBeans; this.memoryBean = memoryBean; @@ -140,14 +143,14 @@ private JvmMemoryMetrics( private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_OBJECTS_PENDING_FINALIZATION) .help("The number of objects waiting in the finalizer queue.") .callback(callback -> callback.call(memoryBean.getObjectPendingFinalizationCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_USED_BYTES) .help("Used bytes of a given JVM memory area.") .unit(Unit.BYTES) @@ -157,10 +160,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getUsed(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getUsed(), "nonheap"); }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_COMMITTED_BYTES) .help("Committed (bytes) of a given JVM memory area.") .unit(Unit.BYTES) @@ -170,10 +173,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getCommitted(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getCommitted(), "nonheap"); }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_MAX_BYTES) .help("Max (bytes) of a given JVM memory area.") .unit(Unit.BYTES) @@ -183,10 +186,10 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getMax(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getMax(), "nonheap"); }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_INIT_BYTES) .help("Initial bytes of a given JVM memory area.") .unit(Unit.BYTES) @@ -196,56 +199,56 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getInit(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getInit(), "nonheap"); }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_USED_BYTES) .help("Used bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getUsed)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COMMITTED_BYTES) .help("Committed bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getCommitted)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_MAX_BYTES) .help("Max bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getMax)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_INIT_BYTES) .help("Initial bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getInit)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_USED_BYTES) .help("Used bytes after last collection of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getUsed)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_COMMITTED_BYTES) .help("Committed after last collection bytes of a given JVM memory pool.") .unit(Unit.BYTES) @@ -253,28 +256,28 @@ private void register(PrometheusRegistry registry) { .callback( makeCallback( poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getCommitted)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_MAX_BYTES) .help("Max bytes after last collection of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getMax)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_MEMORY_POOL_COLLECTION_INIT_BYTES) .help("Initial after last collection bytes of a given JVM memory pool.") .unit(Unit.BYTES) .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getInit)) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); } private Consumer makeCallback( @@ -301,10 +304,10 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private MemoryMXBean memoryBean; - @Nullable private List poolBeans; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private MemoryMXBean memoryBean; + @Nullable private List poolBeans; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java index dd998c873..3dbd55ea9 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetrics.java @@ -55,21 +55,22 @@ public class JvmMemoryPoolAllocationMetrics { private final List garbageCollectorBeans; private final Labels constLabels; - private JvmMemoryPoolAllocationMetrics(List garbageCollectorBeans, Labels constLabels) { + private JvmMemoryPoolAllocationMetrics( + List garbageCollectorBeans, Labels constLabels) { this.garbageCollectorBeans = garbageCollectorBeans; this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; } private void register(PrometheusRegistry registry) { - Counter allocatedCounter = - Counter.builder() - .name(JVM_MEMORY_POOL_ALLOCATED_BYTES_TOTAL) - .help( - "Total bytes allocated in a given JVM memory pool. Only updated after GC, " - + "not continuously.") - .labelNames("pool") - .constLabels(constLabels) - .register(registry); + Counter allocatedCounter = + Counter.builder() + .name(JVM_MEMORY_POOL_ALLOCATED_BYTES_TOTAL) + .help( + "Total bytes allocated in a given JVM memory pool. Only updated after GC, " + + "not continuously.") + .labelNames("pool") + .constLabels(constLabels) + .register(registry); AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter); @@ -159,7 +160,7 @@ public static class Builder { private Labels constLabels = Labels.EMPTY; private Builder() {} - + public Builder constLabels(Labels constLabels) { this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; return this; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java index b7efb437d..f888b78d1 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java @@ -32,8 +32,8 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java index 79fc142dc..5c3ad0210 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java @@ -3,8 +3,8 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Unit; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.ManagementFactory; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; @@ -100,7 +100,8 @@ public class JvmNativeMemoryMetrics { private final PlatformMBeanServerAdapter adapter; private final Labels constLabels; - private JvmNativeMemoryMetrics(PrometheusProperties config, PlatformMBeanServerAdapter adapter, Labels constLabels) { + private JvmNativeMemoryMetrics( + PrometheusProperties config, PlatformMBeanServerAdapter adapter, Labels constLabels) { this.config = config; this.adapter = adapter; this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; @@ -110,7 +111,7 @@ private void register(PrometheusRegistry registry) { // first call will check if enabled and set the flag vmNativeMemorySummaryInBytesOrEmpty(); if (isEnabled.get()) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_NATIVE_MEMORY_RESERVED_BYTES) .help( "Reserved bytes of a given JVM. Reserved memory represents the total amount of " @@ -121,7 +122,7 @@ private void register(PrometheusRegistry registry) { .constLabels(constLabels) .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_NATIVE_MEMORY_COMMITTED_BYTES) .help( "Committed bytes of a given JVM. Committed memory represents the amount of " diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java index d241970a7..99e87687b 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetric.java @@ -37,7 +37,11 @@ public class JvmRuntimeInfoMetric { private final Labels constLabels; private JvmRuntimeInfoMetric( - String version, String vendor, String runtime, PrometheusProperties config, Labels constLabels) { + String version, + String vendor, + String runtime, + PrometheusProperties config, + Labels constLabels) { this.config = config; this.version = version; this.vendor = vendor; @@ -47,13 +51,13 @@ private JvmRuntimeInfoMetric( private void register(PrometheusRegistry registry) { - Info jvmInfo = - Info.builder(config) - .name(JVM_RUNTIME_INFO) - .help("JVM runtime info") - .labelNames("version", "vendor", "runtime") - .constLabels(constLabels) - .register(registry); + Info jvmInfo = + Info.builder(config) + .name(JVM_RUNTIME_INFO) + .help("JVM runtime info") + .labelNames("version", "vendor", "runtime") + .constLabels(constLabels) + .register(registry); jvmInfo.setLabelValues(version, vendor, runtime); } @@ -68,11 +72,11 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { - private final PrometheusProperties config; - @Nullable private String version; - @Nullable private String vendor; - @Nullable private String runtime; - private Labels constLabels = Labels.EMPTY; + private final PrometheusProperties config; + @Nullable private String version; + @Nullable private String vendor; + @Nullable private String runtime; + private Labels constLabels = Labels.EMPTY; private Builder(PrometheusProperties config) { this.config = config; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java index e407bc502..22ef25f30 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java @@ -78,7 +78,10 @@ public class JvmThreadsMetrics { private final Labels constLabels; private JvmThreadsMetrics( - boolean isNativeImage, ThreadMXBean threadBean, PrometheusProperties config, Labels constLabels) { + boolean isNativeImage, + ThreadMXBean threadBean, + PrometheusProperties config, + Labels constLabels) { this.config = config; this.threadBean = threadBean; this.isNativeImage = isNativeImage; @@ -87,53 +90,53 @@ private JvmThreadsMetrics( private void register(PrometheusRegistry registry) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_CURRENT) .help("Current thread count of a JVM") .callback(callback -> callback.call(threadBean.getThreadCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DAEMON) .help("Daemon thread count of a JVM") .callback(callback -> callback.call(threadBean.getDaemonThreadCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_PEAK) .help("Peak thread count of a JVM") .callback(callback -> callback.call(threadBean.getPeakThreadCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(JVM_THREADS_STARTED_TOTAL) .help("Started thread count of a JVM") .callback(callback -> callback.call(threadBean.getTotalStartedThreadCount())) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); if (!isNativeImage) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DEADLOCKED) .help( "Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or " + "ownable synchronizers") .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findDeadlockedThreads()))) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(JVM_THREADS_DEADLOCKED_MONITOR) .help("Cycles of JVM-threads that are in deadlock waiting to acquire object monitors") .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findMonitorDeadlockedThreads()))) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); GaugeWithCallback.builder(config) .name(JVM_THREADS_STATE) @@ -211,7 +214,7 @@ public static class Builder { private Builder(PrometheusProperties config) { this.config = config; } - + public Builder constLabels(Labels constLabels) { this.constLabels = constLabels == null ? Labels.EMPTY : constLabels; return this; diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java index ca0312560..82c88ca31 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java @@ -4,8 +4,8 @@ import io.prometheus.metrics.core.metrics.CounterWithCallback; import io.prometheus.metrics.core.metrics.GaugeWithCallback; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Unit; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.Unit; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -101,7 +101,7 @@ private ProcessMetrics( private void register(PrometheusRegistry registry) { - CounterWithCallback.builder(config) + CounterWithCallback.builder(config) .name(PROCESS_CPU_SECONDS_TOTAL) .help("Total user and system CPU time spent in seconds.") .unit(Unit.SECONDS) @@ -121,19 +121,19 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .constLabels(constLabels) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_START_TIME_SECONDS) .help("Start time of the process since unix epoch in seconds.") .unit(Unit.SECONDS) .callback(callback -> callback.call(Unit.millisToSeconds(runtimeBean.getStartTime()))) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_OPEN_FDS) .help("Number of open file descriptors.") .callback( @@ -146,11 +146,11 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .constLabels(constLabels) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_MAX_FDS) .help("Maximum number of open file descriptors.") .callback( @@ -163,13 +163,13 @@ private void register(PrometheusRegistry registry) { } catch (Exception ignored) { // Ignored } - }) - .constLabels(constLabels) - .register(registry); + }) + .constLabels(constLabels) + .register(registry); if (linux) { - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_VIRTUAL_MEMORY_BYTES) .help("Virtual memory size in bytes.") .unit(Unit.BYTES) @@ -182,10 +182,10 @@ private void register(PrometheusRegistry registry) { // Ignored } }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); - GaugeWithCallback.builder(config) + GaugeWithCallback.builder(config) .name(PROCESS_RESIDENT_MEMORY_BYTES) .help("Resident memory size in bytes.") .unit(Unit.BYTES) @@ -198,8 +198,8 @@ private void register(PrometheusRegistry registry) { // Ignored } }) - .constLabels(constLabels) - .register(registry); + .constLabels(constLabels) + .register(registry); } } diff --git a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java index 96c0792b0..551ed35c7 100644 --- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java +++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java @@ -4,10 +4,10 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.model.registry.PrometheusRegistry; +import io.prometheus.metrics.model.snapshots.DataPointSnapshot; import io.prometheus.metrics.model.snapshots.Labels; -import io.prometheus.metrics.model.snapshots.MetricSnapshots; import io.prometheus.metrics.model.snapshots.MetricSnapshot; -import io.prometheus.metrics.model.snapshots.DataPointSnapshot; +import io.prometheus.metrics.model.snapshots.MetricSnapshots; import java.lang.management.ManagementFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;