Skip to content

Commit cadb2ba

Browse files
committed
fix: keep typed descriptors opt-in for legacy snapshots
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 64933d2 commit cadb2ba

5 files changed

Lines changed: 13 additions & 33 deletions

File tree

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ public Builder dataPoint(CounterDataPointSnapshot dataPoint) {
179179
return this;
180180
}
181181

182-
@Override
183-
protected MetricMetadata buildMetadata() {
184-
if (name == null) {
185-
throw new IllegalArgumentException("Missing required field: name is null");
186-
}
187-
return MetricMetadataSupport.counterMetadata(name, help, unit);
188-
}
189-
190182
@Override
191183
public CounterSnapshot build() {
192184
return new CounterSnapshot(buildMetadata(), dataPoints);

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/InfoSnapshot.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ public Builder unit(@Nullable Unit unit) {
118118
throw new IllegalArgumentException("Info metric cannot have a unit.");
119119
}
120120

121-
@Override
122-
protected MetricMetadata buildMetadata() {
123-
if (name == null) {
124-
throw new IllegalArgumentException("Missing required field: name is null");
125-
}
126-
return MetricMetadataSupport.infoMetadata(name, help);
127-
}
128-
129121
@Override
130122
public InfoSnapshot build() {
131123
return new InfoSnapshot(buildMetadata(), dataPoints);

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricSnapshot.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ private static <T extends DataPointSnapshot> void validateLabels(
5555

5656
public abstract static class Builder<T extends Builder<T>> {
5757

58-
@Nullable protected String name;
59-
@Nullable protected String help;
60-
@Nullable protected Unit unit;
58+
@Nullable private String name;
59+
@Nullable private String help;
60+
@Nullable private Unit unit;
6161

6262
/**
6363
* The name is required. If the name is missing or invalid, {@code build()} will throw an {@link
@@ -85,7 +85,7 @@ protected MetricMetadata buildMetadata() {
8585
if (name == null) {
8686
throw new IllegalArgumentException("Missing required field: name is null");
8787
}
88-
return MetricMetadataSupport.metricMetadata(name, help, unit);
88+
return new MetricMetadata(name, help, unit);
8989
}
9090

9191
protected abstract T self();

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/CounterSnapshotTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,14 @@ void testEmptyCounter() {
9494
@Test
9595
void testTotalSuffixPresent() {
9696
CounterSnapshot snapshot = CounterSnapshot.builder().name("test_total").build();
97-
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("test");
98-
SnapshotTestUtil.assertDerivedMetadata(snapshot, "test", "test_total", "test_total");
97+
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("test_total");
98+
SnapshotTestUtil.assertDerivedMetadata(snapshot, "test_total", "test_total", "test_total");
9999
}
100100

101101
@Test
102-
void testCounterUnitDerivedFromTypedBuilder() {
103-
CounterSnapshot snapshot =
104-
CounterSnapshot.builder().name("test_total").unit(Unit.SECONDS).build();
105-
106-
SnapshotTestUtil.assertMetadata(snapshot, "test_seconds", null, "seconds");
107-
SnapshotTestUtil.assertDerivedMetadata(
108-
snapshot, "test_seconds", "test_total_seconds", "test_total");
102+
void testCounterBuilderKeepsLegacyUnitValidation() {
103+
assertThatExceptionOfType(IllegalArgumentException.class)
104+
.isThrownBy(() -> CounterSnapshot.builder().name("test_total").unit(Unit.SECONDS).build());
109105
}
110106

111107
@Test

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/InfoSnapshotTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ void testDataImmutable() {
6262
@Test
6363
void testNameMayIncludeSuffix() {
6464
InfoSnapshot snapshot = InfoSnapshot.builder().name("jvm_info").build();
65-
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("jvm");
66-
SnapshotTestUtil.assertDerivedMetadata(snapshot, "jvm", "jvm_info", "jvm_info");
65+
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("jvm_info");
66+
SnapshotTestUtil.assertDerivedMetadata(snapshot, "jvm_info", "jvm_info", "jvm_info");
6767
}
6868

6969
@Test
7070
void testNameMayIncludeSuffixDot() {
7171
InfoSnapshot snapshot = InfoSnapshot.builder().name("jvm.info").build();
72-
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("jvm");
73-
SnapshotTestUtil.assertDerivedMetadata(snapshot, "jvm", "jvm.info", "jvm.info");
72+
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("jvm_info");
73+
SnapshotTestUtil.assertDerivedMetadata(snapshot, "jvm.info", "jvm.info", "jvm.info");
7474
}
7575
}

0 commit comments

Comments
 (0)