Skip to content

Commit f1dc469

Browse files
zeitlingerjaydeluca
authored andcommitted
feat: add typed metric family descriptors (#2114)
Adds typed metric family descriptors and typed metadata support for the model snapshots. This is the typed-descriptor branch for downstreams that want to provide registration-time metadata explicitly. The #1800 Collector/MultiCollector registration metadata hooks are already optional via default methods, so unmodified downstreams should not need this PR just to keep working. This PR now also deprecates the fragmented registration metadata API (`getPrometheusName()`, `getMetricType()`, `getLabelNames()`, and `getMetadata()` plus the `MultiCollector` variants) in favor of `getMetricFamilyDescriptor()` / `getMetricFamilyDescriptors()`. The deprecated methods remain bridged by default implementations for compatibility. Related validation: - #2121 validates unmodified Micrometer independently of #2114, against `main` + #2124. - #2123 validates a Micrometer branch that explicitly uses `MetricFamilyDescriptor` to implement the existing registration metadata hooks without invoking scrape/sample callbacks during registration. --------- Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com> Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent 6a97e41 commit f1dc469

36 files changed

Lines changed: 950 additions & 66 deletions

File tree

examples/example-exporter-multi-target/src/main/java/io/prometheus/metrics/examples/multitarget/SampleMultiCollector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR
7777
return new MetricSnapshots(snaps);
7878
}
7979

80+
/**
81+
* @deprecated Use {@code getMetricFamilyDescriptors()} instead.
82+
*/
8083
@Override
84+
@Deprecated
85+
@SuppressWarnings("InlineMeSuggester")
8186
public List<String> getPrometheusNames() {
8287
List<String> names = new ArrayList<String>();
8388
names.add("x_calls_total");

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,15 @@ protected CounterSnapshot collect(List<Labels> labels, List<DataPoint> metricDat
9393
for (int i = 0; i < labels.size(); i++) {
9494
data.add(metricData.get(i).collect(labels.get(i)));
9595
}
96-
return new CounterSnapshot(getMetadata(), data);
96+
return new CounterSnapshot(metadata, data);
9797
}
9898

99+
/**
100+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
101+
*/
99102
@Override
103+
@Deprecated
104+
@SuppressWarnings("InlineMeSuggester")
100105
public MetricType getMetricType() {
101106
return MetricType.COUNTER;
102107
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CounterWithCallback.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ public CounterSnapshot collect() {
4848
new CounterSnapshot.CounterDataPointSnapshot(
4949
value, makeLabels(labelValues), null, 0L));
5050
});
51-
return new CounterSnapshot(getMetadata(), dataPoints);
51+
return new CounterSnapshot(metadata, dataPoints);
5252
}
5353

54+
/**
55+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
56+
*/
5457
@Override
58+
@Deprecated
59+
@SuppressWarnings("InlineMeSuggester")
5560
public MetricType getMetricType() {
5661
return MetricType.COUNTER;
5762
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Gauge.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ protected GaugeSnapshot collect(List<Labels> labels, List<DataPoint> metricData)
9595
for (int i = 0; i < labels.size(); i++) {
9696
dataPointSnapshots.add(metricData.get(i).collect(labels.get(i)));
9797
}
98-
return new GaugeSnapshot(getMetadata(), dataPointSnapshots);
98+
return new GaugeSnapshot(metadata, dataPointSnapshots);
9999
}
100100

101+
/**
102+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
103+
*/
101104
@Override
105+
@Deprecated
106+
@SuppressWarnings("InlineMeSuggester")
102107
public MetricType getMetricType() {
103108
return MetricType.GAUGE;
104109
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/GaugeWithCallback.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ public GaugeSnapshot collect() {
5252
dataPoints.add(
5353
new GaugeSnapshot.GaugeDataPointSnapshot(value, makeLabels(labelValues), null, 0L));
5454
});
55-
return new GaugeSnapshot(getMetadata(), dataPoints);
55+
return new GaugeSnapshot(metadata, dataPoints);
5656
}
5757

58+
/**
59+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
60+
*/
5861
@Override
62+
@Deprecated
63+
@SuppressWarnings("InlineMeSuggester")
5964
public MetricType getMetricType() {
6065
return MetricType.GAUGE;
6166
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Histogram.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,15 @@ protected HistogramSnapshot collect(List<Labels> labels, List<DataPoint> metricD
650650
for (int i = 0; i < labels.size(); i++) {
651651
data.add(metricData.get(i).collect(labels.get(i)));
652652
}
653-
return new HistogramSnapshot(getMetadata(), data);
653+
return new HistogramSnapshot(metadata, data);
654654
}
655655

656+
/**
657+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
658+
*/
656659
@Override
660+
@Deprecated
661+
@SuppressWarnings("InlineMeSuggester")
657662
public MetricType getMetricType() {
658663
return MetricType.HISTOGRAM;
659664
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Info.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void setLabelValues(String... labelValues) {
4848
throw new IllegalArgumentException(
4949
getClass().getSimpleName()
5050
+ " "
51-
+ getMetadata().getName()
51+
+ metadata.getName()
5252
+ " was created with "
5353
+ labelNames.length
5454
+ " label names, but you called setLabelValues() with "
@@ -66,7 +66,7 @@ public void addLabelValues(String... labelValues) {
6666
throw new IllegalArgumentException(
6767
getClass().getSimpleName()
6868
+ " "
69-
+ getMetadata().getName()
69+
+ metadata.getName()
7070
+ " was created with "
7171
+ labelNames.length
7272
+ " label names, but you called addLabelValues() with "
@@ -82,7 +82,7 @@ public void remove(String... labelValues) {
8282
throw new IllegalArgumentException(
8383
getClass().getSimpleName()
8484
+ " "
85-
+ getMetadata().getName()
85+
+ metadata.getName()
8686
+ " was created with "
8787
+ labelNames.length
8888
+ " label names, but you called remove() with "
@@ -103,10 +103,15 @@ public InfoSnapshot collect() {
103103
data.add(new InfoSnapshot.InfoDataPointSnapshot(label.merge(constLabels)));
104104
}
105105
}
106-
return new InfoSnapshot(getMetadata(), data);
106+
return new InfoSnapshot(metadata, data);
107107
}
108108

109+
/**
110+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
111+
*/
109112
@Override
113+
@Deprecated
114+
@SuppressWarnings("InlineMeSuggester")
110115
public MetricType getMetricType() {
111116
return MetricType.INFO;
112117
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/MetricWithFixedMetadata.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.prometheus.metrics.core.metrics;
22

33
import io.prometheus.metrics.config.PrometheusProperties;
4+
import io.prometheus.metrics.model.registry.MetricType;
45
import io.prometheus.metrics.model.snapshots.Label;
56
import io.prometheus.metrics.model.snapshots.Labels;
7+
import io.prometheus.metrics.model.snapshots.MetricFamilyDescriptor;
68
import io.prometheus.metrics.model.snapshots.MetricMetadata;
79
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
810
import io.prometheus.metrics.model.snapshots.Unit;
@@ -20,7 +22,7 @@
2022
*/
2123
public abstract class MetricWithFixedMetadata extends Metric {
2224

23-
private final MetricMetadata metadata;
25+
protected final MetricMetadata metadata;
2426
protected final String[] labelNames;
2527

2628
protected MetricWithFixedMetadata(Builder<?, ?> builder) {
@@ -37,6 +39,22 @@ protected MetricWithFixedMetadata(Builder<?, ?> builder) {
3739
}
3840

3941
@Override
42+
@Nullable
43+
@SuppressWarnings("deprecation")
44+
public MetricFamilyDescriptor getMetricFamilyDescriptor() {
45+
MetricType metricType = getMetricType();
46+
if (metricType == null) {
47+
return null;
48+
}
49+
return MetricFamilyDescriptor.of(metricType, metadata, getPrometheusLabels());
50+
}
51+
52+
/**
53+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
54+
*/
55+
@Override
56+
@Deprecated
57+
@SuppressWarnings("InlineMeSuggester")
4058
public MetricMetadata getMetadata() {
4159
return metadata;
4260
}
@@ -65,13 +83,27 @@ private String makeExpositionBaseName(@Nullable String expositionBaseName, @Null
6583
return expositionBaseName;
6684
}
6785

86+
/**
87+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
88+
*/
6889
@Override
90+
@Deprecated
91+
@SuppressWarnings("InlineMeSuggester")
6992
public String getPrometheusName() {
7093
return metadata.getPrometheusName();
7194
}
7295

96+
/**
97+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
98+
*/
7399
@Override
100+
@Deprecated
101+
@SuppressWarnings("InlineMeSuggester")
74102
public Set<String> getLabelNames() {
103+
return getPrometheusLabels();
104+
}
105+
106+
private Set<String> getPrometheusLabels() {
75107
Set<String> names = new HashSet<>();
76108
for (String labelName : labelNames) {
77109
names.add(PrometheusNaming.prometheusName(labelName));

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StateSet.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private StateSet(Builder builder, String[] names) {
6060
super(builder);
6161
this.names = names;
6262
for (String name : names) {
63-
if (this.getMetadata().getPrometheusName().equals(prometheusName(name))) {
63+
if (metadata.getPrometheusName().equals(prometheusName(name))) {
6464
throw new IllegalArgumentException(
6565
"Label name "
6666
+ name
@@ -82,10 +82,15 @@ protected StateSetSnapshot collect(List<Labels> labels, List<DataPoint> metricDa
8282
new StateSetSnapshot.StateSetDataPointSnapshot(
8383
names, metricDataList.get(i).values, labels.get(i)));
8484
}
85-
return new StateSetSnapshot(getMetadata(), data);
85+
return new StateSetSnapshot(metadata, data);
8686
}
8787

88+
/**
89+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
90+
*/
8891
@Override
92+
@Deprecated
93+
@SuppressWarnings("InlineMeSuggester")
8994
public MetricType getMetricType() {
9095
return MetricType.STATESET;
9196
}

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StatefulMetric.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public D labelValues(String... labelValues) {
106106
throw new IllegalArgumentException(
107107
getClass().getSimpleName()
108108
+ " "
109-
+ getMetadata().getName()
109+
+ metadata.getName()
110110
+ " was created with label names, so you must call labelValues(...)"
111111
+ " when using it.");
112112
} else {
@@ -121,7 +121,7 @@ public D labelValues(String... labelValues) {
121121
if (l.get(i) == null) {
122122
throw new IllegalArgumentException(
123123
"null label value for metric "
124-
+ getMetadata().getName()
124+
+ metadata.getName()
125125
+ " and label "
126126
+ labelNames[i]);
127127
}
@@ -172,7 +172,7 @@ protected MetricsProperties[] getMetricProperties(
172172
if (Objects.equals(builder.exemplarsEnabled, false)) {
173173
properties.add(MetricsProperties.builder().exemplarsEnabled(false).build());
174174
}
175-
String metricName = getMetadata().getName();
175+
String metricName = metadata.getName();
176176
if (prometheusProperties.getMetricProperties(metricName) != null) {
177177
properties.add(prometheusProperties.getMetricProperties(metricName));
178178
}

0 commit comments

Comments
 (0)