Skip to content

Commit b76ce72

Browse files
committed
start on code review updates
Signed-off-by: Jay DeLuca <[email protected]>
1 parent 5208fb1 commit b76ce72

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,5 @@ default String getPrometheusName() {
9494
* @return the metric type, or {@code null} if unknown
9595
*/
9696
@Nullable
97-
default MetricType getMetricType() {
98-
return null;
99-
}
97+
MetricType getMetricType();
10098
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
package io.prometheus.metrics.model.registry;
22

33
/**
4-
* Represents the type of a Prometheus metric.
4+
* Represents the type of Prometheus metric.
55
*
66
* <p>This enum is used for early validation when registering collectors with duplicate Prometheus
77
* names. All collectors with the same Prometheus name must have the same metric type.
88
*/
99
public enum MetricType {
10-
/** Counter metric type - monotonically increasing value. */
1110
COUNTER,
1211

13-
/** Gauge metric type - value that can go up or down. */
1412
GAUGE,
1513

16-
/** Histogram metric type - samples observations and counts them in buckets. */
1714
HISTOGRAM,
1815

19-
/** Summary metric type - samples observations and calculates quantiles. */
2016
SUMMARY,
2117

22-
/** Info metric type - key-value pairs providing information about the entity. */
2318
INFO,
2419

25-
/** StateSet metric type - represents a set of boolean states. */
2620
STATESET,
2721

28-
/** Unknown metric type - for custom or legacy collectors. */
2922
UNKNOWN
3023
}

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MultiCollector.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import javax.annotation.Nullable;
99

1010
/** Like {@link Collector}, but collecting multiple Snapshots at once. */
11-
@FunctionalInterface
1211
public interface MultiCollector {
1312

1413
/** Called when the Prometheus server scrapes metrics. */
@@ -86,7 +85,5 @@ default List<String> getPrometheusNames() {
8685
* @return the metric type, or {@code null} if unknown
8786
*/
8887
@Nullable
89-
default MetricType getMetricType(String prometheusName) {
90-
return null;
91-
}
88+
MetricType getMetricType(String prometheusName);
9289
}

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/PrometheusRegistry.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.concurrent.ConcurrentHashMap;
77
import java.util.concurrent.CopyOnWriteArrayList;
88
import java.util.function.Predicate;
9+
import java.util.stream.Collectors;
910
import javax.annotation.Nullable;
1011

1112
public class PrometheusRegistry {
@@ -26,6 +27,7 @@ public void register(Collector collector) {
2627
public void register(MultiCollector collector) {
2728
validateTypeConsistency(collector);
2829
multiCollectors.add(collector);
30+
cacheMetricIdentifier(collector);
2931
}
3032

3133
/**
@@ -108,6 +110,19 @@ private void cacheMetricIdentifier(Collector collector) {
108110
}
109111
}
110112

113+
/**
114+
* Caches the metric identifier for lookup during future registrations.
115+
*/
116+
private void cacheMetricIdentifier(MultiCollector collector) {
117+
for (String name : collector.getPrometheusNames()) {
118+
MetricType type = collector.getMetricType(name);
119+
120+
if (name != null) {
121+
registeredMetrics.putIfAbsent(name, new MetricIdentifier(name, type));
122+
}
123+
}
124+
}
125+
111126
public void unregister(Collector collector) {
112127
collectors.remove(collector);
113128
// Note: We don't remove from cache because another collector with the same name might exist

0 commit comments

Comments
 (0)