Skip to content

Commit 21bc821

Browse files
committed
feat: deprecate fragmented metric metadata API
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 64933d2 commit 21bc821

27 files changed

Lines changed: 425 additions & 56 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
@@ -90,10 +90,15 @@ protected CounterSnapshot collect(List<Labels> labels, List<DataPoint> metricDat
9090
for (int i = 0; i < labels.size(); i++) {
9191
data.add(metricData.get(i).collect(labels.get(i)));
9292
}
93-
return new CounterSnapshot(getMetadata(), data);
93+
return new CounterSnapshot(metadata, data);
9494
}
9595

96+
/**
97+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
98+
*/
9699
@Override
100+
@Deprecated
101+
@SuppressWarnings("InlineMeSuggester")
97102
public MetricType getMetricType() {
98103
return MetricType.COUNTER;
99104
}

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
@@ -92,10 +92,15 @@ protected GaugeSnapshot collect(List<Labels> labels, List<DataPoint> metricData)
9292
for (int i = 0; i < labels.size(); i++) {
9393
dataPointSnapshots.add(metricData.get(i).collect(labels.get(i)));
9494
}
95-
return new GaugeSnapshot(getMetadata(), dataPointSnapshots);
95+
return new GaugeSnapshot(metadata, dataPointSnapshots);
9696
}
9797

98+
/**
99+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
100+
*/
98101
@Override
102+
@Deprecated
103+
@SuppressWarnings("InlineMeSuggester")
99104
public MetricType getMetricType() {
100105
return MetricType.GAUGE;
101106
}

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
@@ -647,10 +647,15 @@ protected HistogramSnapshot collect(List<Labels> labels, List<DataPoint> metricD
647647
for (int i = 0; i < labels.size(); i++) {
648648
data.add(metricData.get(i).collect(labels.get(i)));
649649
}
650-
return new HistogramSnapshot(getMetadata(), data);
650+
return new HistogramSnapshot(metadata, data);
651651
}
652652

653+
/**
654+
* @deprecated Use {@link #getMetricFamilyDescriptor()} instead.
655+
*/
653656
@Override
657+
@Deprecated
658+
@SuppressWarnings("InlineMeSuggester")
654659
public MetricType getMetricType() {
655660
return MetricType.HISTOGRAM;
656661
}

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
@@ -105,7 +105,7 @@ public D labelValues(String... labelValues) {
105105
throw new IllegalArgumentException(
106106
getClass().getSimpleName()
107107
+ " "
108-
+ getMetadata().getName()
108+
+ metadata.getName()
109109
+ " was created with label names, so you must call labelValues(...)"
110110
+ " when using it.");
111111
} else {
@@ -120,7 +120,7 @@ public D labelValues(String... labelValues) {
120120
if (l.get(i) == null) {
121121
throw new IllegalArgumentException(
122122
"null label value for metric "
123-
+ getMetadata().getName()
123+
+ metadata.getName()
124124
+ " and label "
125125
+ labelNames[i]);
126126
}
@@ -171,7 +171,7 @@ protected MetricsProperties[] getMetricProperties(
171171
if (Objects.equals(builder.exemplarsEnabled, false)) {
172172
properties.add(MetricsProperties.builder().exemplarsEnabled(false).build());
173173
}
174-
String metricName = getMetadata().getName();
174+
String metricName = metadata.getName();
175175
if (prometheusProperties.getMetricProperties(metricName) != null) {
176176
properties.add(prometheusProperties.getMetricProperties(metricName));
177177
}

0 commit comments

Comments
 (0)