Skip to content

Commit 5a1823c

Browse files
committed
feat(jvm): add constLabels support to JvmMetrics.builder().register() [Fixes #1694]
1 parent e8147de commit 5a1823c

File tree

12 files changed

+287
-125
lines changed

12 files changed

+287
-125
lines changed

prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetrics.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.prometheus.metrics.core.metrics.GaugeWithCallback;
55
import io.prometheus.metrics.model.registry.PrometheusRegistry;
66
import io.prometheus.metrics.model.snapshots.Unit;
7+
import io.prometheus.metrics.model.snapshots.Labels;
78
import java.lang.management.BufferPoolMXBean;
89
import java.lang.management.ManagementFactory;
910
import java.util.List;
@@ -48,11 +49,13 @@ public class JvmBufferPoolMetrics {
4849

4950
private final PrometheusProperties config;
5051
private final List<BufferPoolMXBean> bufferPoolBeans;
52+
private final Labels constLabels;
5153

5254
private JvmBufferPoolMetrics(
53-
List<BufferPoolMXBean> bufferPoolBeans, PrometheusProperties config) {
55+
List<BufferPoolMXBean> bufferPoolBeans, PrometheusProperties config, Labels constLabels) {
5456
this.config = config;
5557
this.bufferPoolBeans = bufferPoolBeans;
58+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
5659
}
5760

5861
private void register(PrometheusRegistry registry) {
@@ -68,6 +71,7 @@ private void register(PrometheusRegistry registry) {
6871
callback.call(pool.getMemoryUsed(), pool.getName());
6972
}
7073
})
74+
.constLabels(constLabels)
7175
.register(registry);
7276

7377
GaugeWithCallback.builder(config)
@@ -81,6 +85,7 @@ private void register(PrometheusRegistry registry) {
8185
callback.call(pool.getTotalCapacity(), pool.getName());
8286
}
8387
})
88+
.constLabels(constLabels)
8489
.register(registry);
8590

8691
GaugeWithCallback.builder(config)
@@ -93,6 +98,7 @@ private void register(PrometheusRegistry registry) {
9398
callback.call(pool.getCount(), pool.getName());
9499
}
95100
})
101+
.constLabels(constLabels)
96102
.register(registry);
97103
}
98104

@@ -106,13 +112,19 @@ public static Builder builder(PrometheusProperties config) {
106112

107113
public static class Builder {
108114

109-
private final PrometheusProperties config;
110-
@Nullable private List<BufferPoolMXBean> bufferPoolBeans;
115+
private final PrometheusProperties config;
116+
@Nullable private List<BufferPoolMXBean> bufferPoolBeans;
117+
private Labels constLabels = Labels.EMPTY;
111118

112119
private Builder(PrometheusProperties config) {
113120
this.config = config;
114121
}
115122

123+
public Builder constLabels(Labels constLabels) {
124+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
125+
return this;
126+
}
127+
116128
/** Package private. For testing only. */
117129
Builder bufferPoolBeans(List<BufferPoolMXBean> bufferPoolBeans) {
118130
this.bufferPoolBeans = bufferPoolBeans;
@@ -128,7 +140,7 @@ public void register(PrometheusRegistry registry) {
128140
if (bufferPoolBeans == null) {
129141
bufferPoolBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
130142
}
131-
new JvmBufferPoolMetrics(bufferPoolBeans, config).register(registry);
143+
new JvmBufferPoolMetrics(bufferPoolBeans, config, constLabels).register(registry);
132144
}
133145
}
134146
}

prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetrics.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.prometheus.metrics.core.metrics.CounterWithCallback;
55
import io.prometheus.metrics.core.metrics.GaugeWithCallback;
66
import io.prometheus.metrics.model.registry.PrometheusRegistry;
7+
import io.prometheus.metrics.model.snapshots.Labels;
78
import java.lang.management.ClassLoadingMXBean;
89
import java.lang.management.ManagementFactory;
910
import javax.annotation.Nullable;
@@ -44,34 +45,40 @@ public class JvmClassLoadingMetrics {
4445

4546
private final PrometheusProperties config;
4647
private final ClassLoadingMXBean classLoadingBean;
48+
private final Labels constLabels;
4749

48-
private JvmClassLoadingMetrics(ClassLoadingMXBean classLoadingBean, PrometheusProperties config) {
50+
private JvmClassLoadingMetrics(
51+
ClassLoadingMXBean classLoadingBean, PrometheusProperties config, Labels constLabels) {
4952
this.classLoadingBean = classLoadingBean;
5053
this.config = config;
54+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
5155
}
5256

5357
private void register(PrometheusRegistry registry) {
5458

55-
GaugeWithCallback.builder(config)
59+
GaugeWithCallback.builder(config)
5660
.name(JVM_CLASSES_CURRENTLY_LOADED)
5761
.help("The number of classes that are currently loaded in the JVM")
5862
.callback(callback -> callback.call(classLoadingBean.getLoadedClassCount()))
59-
.register(registry);
63+
.constLabels(constLabels)
64+
.register(registry);
6065

61-
CounterWithCallback.builder(config)
66+
CounterWithCallback.builder(config)
6267
.name(JVM_CLASSES_LOADED_TOTAL)
6368
.help(
6469
"The total number of classes that have been loaded since the JVM has started execution")
6570
.callback(callback -> callback.call(classLoadingBean.getTotalLoadedClassCount()))
66-
.register(registry);
71+
.constLabels(constLabels)
72+
.register(registry);
6773

68-
CounterWithCallback.builder(config)
74+
CounterWithCallback.builder(config)
6975
.name(JVM_CLASSES_UNLOADED_TOTAL)
7076
.help(
7177
"The total number of classes that have been unloaded since the JVM has "
7278
+ "started execution")
7379
.callback(callback -> callback.call(classLoadingBean.getUnloadedClassCount()))
74-
.register(registry);
80+
.constLabels(constLabels)
81+
.register(registry);
7582
}
7683

7784
public static Builder builder() {
@@ -84,13 +91,19 @@ public static Builder builder(PrometheusProperties config) {
8491

8592
public static class Builder {
8693

87-
private final PrometheusProperties config;
88-
@Nullable private ClassLoadingMXBean classLoadingBean;
94+
private final PrometheusProperties config;
95+
@Nullable private ClassLoadingMXBean classLoadingBean;
96+
private Labels constLabels = Labels.EMPTY;
8997

9098
private Builder(PrometheusProperties config) {
9199
this.config = config;
92100
}
93101

102+
public Builder constLabels(Labels constLabels) {
103+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
104+
return this;
105+
}
106+
94107
/** Package private. For testing only. */
95108
Builder classLoadingBean(ClassLoadingMXBean classLoadingBean) {
96109
this.classLoadingBean = classLoadingBean;
@@ -106,7 +119,7 @@ public void register(PrometheusRegistry registry) {
106119
this.classLoadingBean != null
107120
? this.classLoadingBean
108121
: ManagementFactory.getClassLoadingMXBean();
109-
new JvmClassLoadingMetrics(classLoadingBean, config).register(registry);
122+
new JvmClassLoadingMetrics(classLoadingBean, config, constLabels).register(registry);
110123
}
111124
}
112125
}

prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetrics.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.prometheus.metrics.core.metrics.CounterWithCallback;
77
import io.prometheus.metrics.model.registry.PrometheusRegistry;
88
import io.prometheus.metrics.model.snapshots.Unit;
9+
import io.prometheus.metrics.model.snapshots.Labels;
910
import java.lang.management.CompilationMXBean;
1011
import java.lang.management.ManagementFactory;
1112
import javax.annotation.Nullable;
@@ -39,10 +40,13 @@ public class JvmCompilationMetrics {
3940

4041
private final PrometheusProperties config;
4142
private final CompilationMXBean compilationBean;
43+
private final Labels constLabels;
4244

43-
private JvmCompilationMetrics(CompilationMXBean compilationBean, PrometheusProperties config) {
45+
private JvmCompilationMetrics(
46+
CompilationMXBean compilationBean, PrometheusProperties config, Labels constLabels) {
4447
this.compilationBean = compilationBean;
4548
this.config = config;
49+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
4650
}
4751

4852
private void register(PrometheusRegistry registry) {
@@ -51,13 +55,14 @@ private void register(PrometheusRegistry registry) {
5155
return;
5256
}
5357

54-
CounterWithCallback.builder(config)
58+
CounterWithCallback.builder(config)
5559
.name(JVM_COMPILATION_TIME_SECONDS_TOTAL)
5660
.help("The total time in seconds taken for HotSpot class compilation")
5761
.unit(Unit.SECONDS)
5862
.callback(
5963
callback -> callback.call(millisToSeconds(compilationBean.getTotalCompilationTime())))
60-
.register(registry);
64+
.constLabels(constLabels)
65+
.register(registry);
6166
}
6267

6368
public static Builder builder() {
@@ -70,13 +75,19 @@ public static Builder builder(PrometheusProperties config) {
7075

7176
public static class Builder {
7277

73-
private final PrometheusProperties config;
74-
@Nullable private CompilationMXBean compilationBean;
78+
private final PrometheusProperties config;
79+
@Nullable private CompilationMXBean compilationBean;
80+
private Labels constLabels = Labels.EMPTY;
7581

7682
private Builder(PrometheusProperties config) {
7783
this.config = config;
7884
}
7985

86+
public Builder constLabels(Labels constLabels) {
87+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
88+
return this;
89+
}
90+
8091
/** Package private. For testing only. */
8192
Builder compilationBean(CompilationMXBean compilationBean) {
8293
this.compilationBean = compilationBean;
@@ -92,7 +103,7 @@ public void register(PrometheusRegistry registry) {
92103
this.compilationBean != null
93104
? this.compilationBean
94105
: ManagementFactory.getCompilationMXBean();
95-
new JvmCompilationMetrics(compilationBean, config).register(registry);
106+
new JvmCompilationMetrics(compilationBean, config, constLabels).register(registry);
96107
}
97108
}
98109
}

prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetrics.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.prometheus.metrics.model.registry.PrometheusRegistry;
66
import io.prometheus.metrics.model.snapshots.Quantiles;
77
import io.prometheus.metrics.model.snapshots.Unit;
8+
import io.prometheus.metrics.model.snapshots.Labels;
89
import java.lang.management.GarbageCollectorMXBean;
910
import java.lang.management.ManagementFactory;
1011
import java.util.List;
@@ -42,16 +43,18 @@ public class JvmGarbageCollectorMetrics {
4243

4344
private final PrometheusProperties config;
4445
private final List<GarbageCollectorMXBean> garbageCollectorBeans;
46+
private final Labels constLabels;
4547

4648
private JvmGarbageCollectorMetrics(
47-
List<GarbageCollectorMXBean> garbageCollectorBeans, PrometheusProperties config) {
49+
List<GarbageCollectorMXBean> garbageCollectorBeans, PrometheusProperties config, Labels constLabels) {
4850
this.config = config;
4951
this.garbageCollectorBeans = garbageCollectorBeans;
52+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
5053
}
5154

5255
private void register(PrometheusRegistry registry) {
5356

54-
SummaryWithCallback.builder(config)
57+
SummaryWithCallback.builder(config)
5558
.name(JVM_GC_COLLECTION_SECONDS)
5659
.help("Time spent in a given JVM garbage collector in seconds.")
5760
.unit(Unit.SECONDS)
@@ -66,7 +69,8 @@ private void register(PrometheusRegistry registry) {
6669
gc.getName());
6770
}
6871
})
69-
.register(registry);
72+
.constLabels(constLabels)
73+
.register(registry);
7074
}
7175

7276
public static Builder builder() {
@@ -79,13 +83,19 @@ public static Builder builder(PrometheusProperties config) {
7983

8084
public static class Builder {
8185

82-
private final PrometheusProperties config;
83-
@Nullable private List<GarbageCollectorMXBean> garbageCollectorBeans;
86+
private final PrometheusProperties config;
87+
@Nullable private List<GarbageCollectorMXBean> garbageCollectorBeans;
88+
private Labels constLabels = Labels.EMPTY;
8489

8590
private Builder(PrometheusProperties config) {
8691
this.config = config;
8792
}
8893

94+
public Builder constLabels(Labels constLabels) {
95+
this.constLabels = constLabels == null ? Labels.EMPTY : constLabels;
96+
return this;
97+
}
98+
8999
/** Package private. For testing only. */
90100
Builder garbageCollectorBeans(List<GarbageCollectorMXBean> garbageCollectorBeans) {
91101
this.garbageCollectorBeans = garbageCollectorBeans;
@@ -101,7 +111,7 @@ public void register(PrometheusRegistry registry) {
101111
if (garbageCollectorBeans == null) {
102112
garbageCollectorBeans = ManagementFactory.getGarbageCollectorMXBeans();
103113
}
104-
new JvmGarbageCollectorMetrics(garbageCollectorBeans, config).register(registry);
114+
new JvmGarbageCollectorMetrics(garbageCollectorBeans, config, constLabels).register(registry);
105115
}
106116
}
107117
}

0 commit comments

Comments
 (0)