Skip to content

Commit 1dffd50

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

26 files changed

Lines changed: 372 additions & 56 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR
7878
}
7979

8080
@Override
81+
@Deprecated
82+
@SuppressWarnings("InlineMeSuggester")
8183
public List<String> getPrometheusNames() {
8284
List<String> names = new ArrayList<String>();
8385
names.add("x_calls_total");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ 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

9696
@Override
97+
@Deprecated
98+
@SuppressWarnings("InlineMeSuggester")
9799
public MetricType getMetricType() {
98100
return MetricType.COUNTER;
99101
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ 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

5454
@Override
55+
@Deprecated
56+
@SuppressWarnings("InlineMeSuggester")
5557
public MetricType getMetricType() {
5658
return MetricType.COUNTER;
5759
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ 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

9898
@Override
99+
@Deprecated
100+
@SuppressWarnings("InlineMeSuggester")
99101
public MetricType getMetricType() {
100102
return MetricType.GAUGE;
101103
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ 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

5858
@Override
59+
@Deprecated
60+
@SuppressWarnings("InlineMeSuggester")
5961
public MetricType getMetricType() {
6062
return MetricType.GAUGE;
6163
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,12 @@ 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

653653
@Override
654+
@Deprecated
655+
@SuppressWarnings("InlineMeSuggester")
654656
public MetricType getMetricType() {
655657
return MetricType.HISTOGRAM;
656658
}

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

Lines changed: 6 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,12 @@ 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

109109
@Override
110+
@Deprecated
111+
@SuppressWarnings("InlineMeSuggester")
110112
public MetricType getMetricType() {
111113
return MetricType.INFO;
112114
}

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

Lines changed: 24 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,19 @@ 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+
@Override
53+
@Deprecated
54+
@SuppressWarnings("InlineMeSuggester")
4055
public MetricMetadata getMetadata() {
4156
return metadata;
4257
}
@@ -66,12 +81,20 @@ private String makeExpositionBaseName(@Nullable String expositionBaseName, @Null
6681
}
6782

6883
@Override
84+
@Deprecated
85+
@SuppressWarnings("InlineMeSuggester")
6986
public String getPrometheusName() {
7087
return metadata.getPrometheusName();
7188
}
7289

7390
@Override
91+
@Deprecated
92+
@SuppressWarnings("InlineMeSuggester")
7493
public Set<String> getLabelNames() {
94+
return getPrometheusLabels();
95+
}
96+
97+
private Set<String> getPrometheusLabels() {
7598
Set<String> names = new HashSet<>();
7699
for (String labelName : labelNames) {
77100
names.add(PrometheusNaming.prometheusName(labelName));

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

Lines changed: 4 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,12 @@ 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

8888
@Override
89+
@Deprecated
90+
@SuppressWarnings("InlineMeSuggester")
8991
public MetricType getMetricType() {
9092
return MetricType.STATESET;
9193
}

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)