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..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,6 +3,7 @@ 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.Labels; import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.BufferPoolMXBean; import java.lang.management.ManagementFactory; @@ -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); } @@ -108,11 +114,17 @@ public static class Builder { 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..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 @@ -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,10 +45,13 @@ 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) { @@ -56,6 +60,7 @@ private void register(PrometheusRegistry registry) { .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); CounterWithCallback.builder(config) @@ -63,6 +68,7 @@ private void register(PrometheusRegistry registry) { .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); CounterWithCallback.builder(config) @@ -71,6 +77,7 @@ private void register(PrometheusRegistry registry) { "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); } @@ -86,11 +93,17 @@ public static class Builder { 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..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,6 +5,7 @@ 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.Labels; import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.CompilationMXBean; import java.lang.management.ManagementFactory; @@ -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) { @@ -57,6 +61,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.SECONDS) .callback( callback -> callback.call(millisToSeconds(compilationBean.getTotalCompilationTime()))) + .constLabels(constLabels) .register(registry); } @@ -72,11 +77,17 @@ public static class Builder { 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..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,6 +3,7 @@ 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 java.lang.management.GarbageCollectorMXBean; @@ -42,11 +43,15 @@ 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) { @@ -66,6 +71,7 @@ private void register(PrometheusRegistry registry) { gc.getName()); } }) + .constLabels(constLabels) .register(registry); } @@ -81,11 +87,17 @@ public static class Builder { 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 +113,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..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,6 +3,7 @@ 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.Labels; import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; @@ -127,12 +128,17 @@ 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) { @@ -141,6 +147,7 @@ private void register(PrometheusRegistry registry) { .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); GaugeWithCallback.builder(config) @@ -153,6 +160,7 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getUsed(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getUsed(), "nonheap"); }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -165,6 +173,7 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getCommitted(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getCommitted(), "nonheap"); }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -177,6 +186,7 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getMax(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getMax(), "nonheap"); }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -189,6 +199,7 @@ private void register(PrometheusRegistry registry) { callback.call(memoryBean.getHeapMemoryUsage().getInit(), "heap"); callback.call(memoryBean.getNonHeapMemoryUsage().getInit(), "nonheap"); }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -197,6 +208,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getUsed)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -205,6 +217,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getCommitted)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -213,6 +226,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getMax)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -221,6 +235,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(poolBeans, MemoryPoolMXBean::getUsage, MemoryUsage::getInit)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -230,6 +245,7 @@ private void register(PrometheusRegistry registry) { .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getUsed)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -240,6 +256,7 @@ private void register(PrometheusRegistry registry) { .callback( makeCallback( poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getCommitted)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -249,6 +266,7 @@ private void register(PrometheusRegistry registry) { .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getMax)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -258,6 +276,7 @@ private void register(PrometheusRegistry registry) { .labelNames("pool") .callback( makeCallback(poolBeans, MemoryPoolMXBean::getCollectionUsage, MemoryUsage::getInit)) + .constLabels(constLabels) .register(registry); } @@ -288,11 +307,17 @@ public static class Builder { 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 +339,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..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 @@ -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,9 +53,12 @@ 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) { @@ -65,6 +69,7 @@ private void register(PrometheusRegistry registry) { "Total bytes allocated in a given JVM memory pool. Only updated after GC, " + "not continuously.") .labelNames("pool") + .constLabels(constLabels) .register(registry); AllocationCountingNotificationListener listener = @@ -152,9 +157,15 @@ 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) { this.garbageCollectorBeans = garbageCollectorBeans; @@ -170,7 +181,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..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 @@ -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; @@ -32,11 +33,18 @@ public static Builder builder(PrometheusProperties config) { public static class Builder { 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..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,6 +3,7 @@ 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.Labels; import io.prometheus.metrics.model.snapshots.Unit; import java.lang.management.ManagementFactory; import java.util.concurrent.atomic.AtomicBoolean; @@ -97,10 +98,13 @@ 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) { @@ -115,6 +119,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(true)) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -125,6 +130,7 @@ private void register(PrometheusRegistry registry) { .unit(Unit.BYTES) .labelNames("pool") .callback(makeCallback(false)) + .constLabels(constLabels) .register(registry); } } @@ -200,6 +206,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 +218,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..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 @@ -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,13 +34,19 @@ 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) { @@ -49,6 +56,7 @@ private void register(PrometheusRegistry registry) { .name(JVM_RUNTIME_INFO) .help("JVM runtime info") .labelNames("version", "vendor", "runtime") + .constLabels(constLabels) .register(registry); jvmInfo.setLabelValues(version, vendor, runtime); @@ -68,11 +76,17 @@ public static class Builder { @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 +118,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..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 @@ -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,12 +75,17 @@ 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) { @@ -88,24 +94,28 @@ private void register(PrometheusRegistry registry) { .name(JVM_THREADS_CURRENT) .help("Current thread count of a JVM") .callback(callback -> callback.call(threadBean.getThreadCount())) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) .name(JVM_THREADS_DAEMON) .help("Daemon thread count of a JVM") .callback(callback -> callback.call(threadBean.getDaemonThreadCount())) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) .name(JVM_THREADS_PEAK) .help("Peak thread count of a JVM") .callback(callback -> callback.call(threadBean.getPeakThreadCount())) + .constLabels(constLabels) .register(registry); 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); if (!isNativeImage) { @@ -116,6 +126,7 @@ private void register(PrometheusRegistry registry) { + "ownable synchronizers") .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findDeadlockedThreads()))) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -124,6 +135,7 @@ private void register(PrometheusRegistry registry) { .callback( callback -> callback.call(nullSafeArrayLength(threadBean.findMonitorDeadlockedThreads()))) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -137,6 +149,7 @@ private void register(PrometheusRegistry registry) { callback.call(entry.getValue(), entry.getKey()); } }) + .constLabels(constLabels) .register(registry); } } @@ -196,11 +209,17 @@ 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) { this.threadBean = threadBean; @@ -222,7 +241,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..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,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 io.prometheus.metrics.model.snapshots.Unit; import java.io.BufferedReader; import java.io.File; @@ -82,17 +83,20 @@ 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) { @@ -118,6 +122,7 @@ private void register(PrometheusRegistry registry) { // Ignored } }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -125,6 +130,7 @@ private void register(PrometheusRegistry registry) { .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); GaugeWithCallback.builder(config) @@ -141,6 +147,7 @@ private void register(PrometheusRegistry registry) { // Ignored } }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -157,6 +164,7 @@ private void register(PrometheusRegistry registry) { // Ignored } }) + .constLabels(constLabels) .register(registry); if (linux) { @@ -174,6 +182,7 @@ private void register(PrometheusRegistry registry) { // Ignored } }) + .constLabels(constLabels) .register(registry); GaugeWithCallback.builder(config) @@ -189,6 +198,7 @@ private void register(PrometheusRegistry registry) { // Ignored } }) + .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..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,6 +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.MetricSnapshot; +import io.prometheus.metrics.model.snapshots.MetricSnapshots; 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();