File tree Expand file tree Collapse file tree 4 files changed +18
-15
lines changed
prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry Expand file tree Collapse file tree 4 files changed +18
-15
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11package 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 */
99public 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}
Original file line number Diff line number Diff line change 88import javax .annotation .Nullable ;
99
1010/** Like {@link Collector}, but collecting multiple Snapshots at once. */
11- @ FunctionalInterface
1211public 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}
Original file line number Diff line number Diff line change 66import java .util .concurrent .ConcurrentHashMap ;
77import java .util .concurrent .CopyOnWriteArrayList ;
88import java .util .function .Predicate ;
9+ import java .util .stream .Collectors ;
910import javax .annotation .Nullable ;
1011
1112public 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
You can’t perform that action at this time.
0 commit comments