Skip to content

Commit 7f2c52a

Browse files
committed
fix tests
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 203361e commit 7f2c52a

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.AfterEach;
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
2628

2729
class CounterTest {
2830

@@ -104,22 +106,21 @@ public void testLabels() {
104106
assertThat(getValue(labels, "l", "b")).isCloseTo(3.0, offset(.001));
105107
}
106108

107-
@Test
108-
public void testTotalStrippedFromName() {
109-
for (String name :
110-
new String[] {
111-
"my_counter_total", "my.counter.total",
112-
"my_counter_seconds_total", "my.counter.seconds.total",
113-
"my_counter", "my.counter",
114-
"my_counter_seconds", "my.counter.seconds"
115-
}) {
116-
Counter counter = Counter.builder().name(name).unit(Unit.SECONDS).build();
117-
Metrics.MetricFamily protobufData =
118-
new PrometheusProtobufWriterImpl().convert(counter.collect());
119-
assertThat(ProtobufUtil.shortDebugString(protobufData))
120-
.isEqualTo(
121-
"name: \"my_counter_seconds_total\" type: COUNTER metric { counter { value: 0.0 } }");
122-
}
109+
@ParameterizedTest
110+
@ValueSource(
111+
strings = {
112+
"my_counter_total",
113+
"my_counter_seconds_total",
114+
"my_counter",
115+
"my_counter_seconds",
116+
})
117+
public void testTotalStrippedFromName(String name) {
118+
Counter counter = Counter.builder().name(name).unit(Unit.SECONDS).build();
119+
Metrics.MetricFamily protobufData =
120+
new PrometheusProtobufWriterImpl().convert(counter.collect());
121+
assertThat(ProtobufUtil.shortDebugString(protobufData))
122+
.isEqualTo(
123+
"name: \"my_counter_seconds_total\" type: COUNTER metric { counter { value: 0.0 } }");
123124
}
124125

125126
@Test

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,12 +1254,6 @@ public void testIllegalLabelNamePrefix() {
12541254
.isThrownBy(() -> Histogram.builder().name("test").labelNames("__hello"));
12551255
}
12561256

1257-
@Test
1258-
public void testIllegalName() {
1259-
assertThatExceptionOfType(IllegalArgumentException.class)
1260-
.isThrownBy(() -> Histogram.builder().name("my_namespace/server.durations"));
1261-
}
1262-
12631257
@Test
12641258
public void testNoName() {
12651259
assertThatExceptionOfType(IllegalArgumentException.class)

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/InfoTest.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515
import java.io.IOException;
1616
import java.nio.charset.StandardCharsets;
1717
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.ValueSource;
1820

1921
class InfoTest {
2022

21-
@Test
22-
public void testInfoStrippedFromName() {
23-
for (String name :
24-
new String[] {
25-
"jvm.runtime", "jvm_runtime",
26-
"jvm.runtime.info", "jvm_runtime_info"
27-
}) {
28-
for (String labelName : new String[] {"my.key", "my_key"}) {
29-
Info info = Info.builder().name(name).labelNames(labelName).build();
30-
info.addLabelValues("value");
31-
Metrics.MetricFamily protobufData =
32-
new PrometheusProtobufWriterImpl().convert(info.collect());
33-
assertThat(ProtobufUtil.shortDebugString(protobufData))
34-
.isEqualTo(
35-
"name: \"jvm_runtime_info\" type: GAUGE metric { label { name: \"my_key\" value:"
36-
+ " \"value\" } gauge { value: 1.0 } }");
37-
}
38-
}
23+
@ParameterizedTest
24+
@ValueSource(strings = {"jvm.runtime", "jvm.runtime.info"})
25+
public void testInfoStrippedFromName(String name) {
26+
Info info = Info.builder().name(name).labelNames("my.key").build();
27+
info.addLabelValues("value");
28+
Metrics.MetricFamily protobufData = new PrometheusProtobufWriterImpl().convert(info.collect());
29+
assertThat(ProtobufUtil.shortDebugString(protobufData))
30+
.isEqualTo(
31+
"name: \"jvm.runtime_info\" type: GAUGE metric { label { name: \"my.key\" value:"
32+
+ " \"value\" } gauge { value: 1.0 } }");
3933
}
4034

4135
@Test
@@ -127,7 +121,8 @@ public void testConstLabelsDuplicate2() {
127121
private void assertTextFormat(String expected, Info info) throws IOException {
128122
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true);
129123
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
130-
writer.write(outputStream, MetricSnapshots.of(info.collect()), EscapingScheme.NO_ESCAPING);
124+
writer.write(
125+
outputStream, MetricSnapshots.of(info.collect()), EscapingScheme.UNDERSCORE_ESCAPING);
131126
String result = outputStream.toString(StandardCharsets.UTF_8.name());
132127
if (!result.contains(expected)) {
133128
throw new AssertionError(expected + " is not contained in the following output:\n" + result);

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void write(
6363
}
6464
}
6565

66-
Metrics.MetricFamily convert(MetricSnapshot snapshot) {
66+
public Metrics.MetricFamily convert(MetricSnapshot snapshot) {
6767
Metrics.MetricFamily.Builder builder = Metrics.MetricFamily.newBuilder();
6868
if (snapshot instanceof CounterSnapshot) {
6969
for (CounterDataPointSnapshot data : ((CounterSnapshot) snapshot).getDataPoints()) {

prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ProtobufExpositionFormatsTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
66
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
77
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
8+
import io.prometheus.metrics.model.snapshots.EscapingScheme;
89
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
10+
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
911

1012
class ProtobufExpositionFormatsTest extends ExpositionFormatsTest {
1113

1214
@Override
1315
protected void assertPrometheusProtobuf(String expected, MetricSnapshot snapshot) {
1416
PrometheusProtobufWriterImpl writer = new PrometheusProtobufWriterImpl();
15-
Metrics.MetricFamily protobufData = writer.convert(snapshot);
17+
Metrics.MetricFamily protobufData =
18+
writer.convert(
19+
PrometheusNaming.escapeMetricSnapshot(snapshot, EscapingScheme.UNDERSCORE_ESCAPING));
1620
String actual = ProtobufUtil.shortDebugString(protobufData);
1721
assertThat(actual).isEqualTo(expected);
1822
}

0 commit comments

Comments
 (0)