Skip to content

Commit a95979d

Browse files
committed
make protobuf optional
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent b4f5707 commit a95979d

File tree

8 files changed

+112
-41
lines changed

8 files changed

+112
-41
lines changed

integration-tests/it-common/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<artifactId>it-common</artifactId>
1212

1313
<name>Integration Tests - Common Utilities</name>
14-
<url>http://github.com/prometheus/client_java</url>
1514
<description>
1615
Common utilities for integration tests
1716
</description>

integration-tests/it-exporter/it-exporter-test/src/test/java/io/prometheus/metrics/it/exporter/test/ExporterIT.java

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.assertj.core.api.Assertions.fail;
66

7+
import com.google.common.io.Files;
8+
import com.google.common.io.Resources;
79
import io.prometheus.client.it.common.LogConsumer;
810
import io.prometheus.client.it.common.Volume;
911
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
@@ -25,6 +27,9 @@
2527
import org.apache.commons.io.IOUtils;
2628
import org.junit.jupiter.api.AfterEach;
2729
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.CsvSource;
32+
import org.junit.jupiter.params.provider.ValueSource;
2833
import org.testcontainers.containers.BindMode;
2934
import org.testcontainers.containers.GenericContainer;
3035

@@ -68,8 +73,7 @@ public void testOpenMetricsTextFormat() throws IOException {
6873
assertThat(response.getHeader("Transfer-Encoding")).isNull();
6974
assertThat(response.getHeader("Content-Length"))
7075
.isEqualTo(Integer.toString(response.body.length));
71-
String bodyString = new String(response.body, UTF_8);
72-
assertThat(bodyString)
76+
assertThat(response.stringBody())
7377
.contains("integration_test_info{test_name=\"" + sampleApp + "\"} 1")
7478
.contains("temperature_celsius{location=\"inside\"} 23.0")
7579
.contains("temperature_celsius{location=\"outside\"} 27.0")
@@ -91,8 +95,7 @@ public void testPrometheusTextFormat() throws IOException {
9195
assertThat(response.getHeader("Transfer-Encoding")).isNull();
9296
assertThat(response.getHeader("Content-Length"))
9397
.isEqualTo(Integer.toString(response.body.length));
94-
String bodyString = new String(response.body, UTF_8);
95-
assertThat(bodyString)
98+
assertThat(response.stringBody())
9699
.contains("integration_test_info{test_name=\"" + sampleApp + "\"} 1")
97100
.contains("temperature_celsius{location=\"inside\"} 23.0")
98101
.contains("temperature_celsius{location=\"outside\"} 27.0")
@@ -125,13 +128,31 @@ public void testPrometheusProtobufFormat() throws IOException {
125128
while (in.available() > 0) {
126129
metrics.add(Metrics.MetricFamily.parseDelimitedFrom(in));
127130
}
128-
assertThat(metrics.size()).isEqualTo(3);
131+
assertThat(metrics).hasSize(3);
129132
// metrics are sorted by name
130133
assertThat(metrics.get(0).getName()).isEqualTo("integration_test_info");
131134
assertThat(metrics.get(1).getName()).isEqualTo("temperature_celsius");
132135
assertThat(metrics.get(2).getName()).isEqualTo("uptime_seconds_total");
133136
}
134137

138+
@ParameterizedTest
139+
@CsvSource({
140+
"openmetrics, debug-openmetrics.txt",
141+
"text, debug-text.txt",
142+
"prometheus-protobuf, debug-protobuf.txt",
143+
})
144+
public void testPrometheusProtobufDebugFormat(String format, String expected) throws IOException {
145+
sampleAppContainer
146+
.withCommand("java", "-jar", "/app/" + sampleApp + ".jar", "9400", "success")
147+
.start();
148+
Response response = scrape("GET", "debug=" + format);
149+
assertThat(response.status).isEqualTo(200);
150+
assertContentType(
151+
"text/plain;charset=utf-8",
152+
response.getHeader("Content-Type"));
153+
assertThat(response.stringBody().trim()).isEqualTo(Resources.toString(Resources.getResource(expected), UTF_8).trim());
154+
}
155+
135156
@Test
136157
public void testCompression() throws IOException {
137158
sampleAppContainer
@@ -159,11 +180,7 @@ public void testCompression() throws IOException {
159180
assertContentType(
160181
"application/openmetrics-text; version=1.0.0; charset=utf-8",
161182
response.getHeader("Content-Type"));
162-
String body =
163-
new String(
164-
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(response.body))),
165-
UTF_8);
166-
assertThat(body).contains("uptime_seconds_total 17.0");
183+
assertThat(response.gzipBody()).contains("uptime_seconds_total 17.0");
167184
}
168185

169186
@Test
@@ -173,7 +190,7 @@ public void testErrorHandling() throws IOException {
173190
.start();
174191
Response response = scrape("GET", "");
175192
assertThat(response.status).isEqualTo(500);
176-
assertThat(new String(response.body, UTF_8)).contains("Simulating an error.");
193+
assertThat(response.stringBody()).contains("Simulating an error.");
177194
}
178195

179196
protected boolean headReturnsContentLength() {
@@ -195,7 +212,7 @@ public void testHeadRequest() throws IOException {
195212
} else {
196213
assertThat(headResponse.getHeader("Content-Length")).isNull();
197214
}
198-
assertThat(headResponse.body.length).isZero();
215+
assertThat(headResponse.body).isEmpty();
199216
}
200217

201218
@Test
@@ -206,8 +223,7 @@ public void testDebug() throws IOException {
206223
Response response = scrape("GET", "debug=openmetrics");
207224
assertThat(response.status).isEqualTo(200);
208225
assertContentType("text/plain; charset=utf-8", response.getHeader("Content-Type"));
209-
String bodyString = new String(response.body, UTF_8);
210-
assertThat(bodyString)
226+
assertThat(response.stringBody())
211227
.contains("uptime_seconds_total 17.0")
212228
.contains("# UNIT uptime_seconds seconds");
213229
}
@@ -227,8 +243,7 @@ public void testNameFilter() throws IOException {
227243
assertContentType(
228244
"application/openmetrics-text; version=1.0.0; charset=utf-8",
229245
response.getHeader("Content-Type"));
230-
String bodyString = new String(response.body, UTF_8);
231-
assertThat(bodyString)
246+
assertThat(response.stringBody())
232247
.contains("integration_test_info{test_name=\"" + sampleApp + "\"} 1")
233248
.contains("uptime_seconds_total 17.0")
234249
.doesNotContain("temperature_celsius");
@@ -251,7 +266,7 @@ public void testEmptyResponseOpenMetrics() throws IOException {
251266
response.getHeader("Content-Type"));
252267
assertThat(response.getHeader("Content-Length"))
253268
.isEqualTo(Integer.toString(response.body.length));
254-
assertThat(new String(response.body, UTF_8)).isEqualTo("# EOF\n");
269+
assertThat(response.stringBody()).isEqualTo("# EOF\n");
255270
}
256271

257272
@Test
@@ -267,7 +282,7 @@ public void testEmptyResponseText() throws IOException {
267282
!= null) { // HTTPServer does not send a zero content length, which is ok
268283
assertThat(response.getHeader("Content-Length")).isEqualTo("0");
269284
}
270-
assertThat(response.body.length).isZero();
285+
assertThat(response.body).isEmpty();
271286
}
272287

273288
@Test
@@ -285,7 +300,7 @@ public void testEmptyResponseProtobuf() throws IOException {
285300
assertContentType(
286301
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited",
287302
response.getHeader("Content-Type"));
288-
assertThat(response.body.length).isZero();
303+
assertThat(response.body).isEmpty();
289304
}
290305

291306
@Test
@@ -303,11 +318,7 @@ public void testEmptyResponseGzipOpenMetrics() throws IOException {
303318
"gzip");
304319
assertThat(response.status).isEqualTo(200);
305320
assertThat(response.getHeader("Content-Encoding")).isEqualTo("gzip");
306-
String body =
307-
new String(
308-
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(response.body))),
309-
UTF_8);
310-
assertThat(body).isEqualTo("# EOF\n");
321+
assertThat(response.gzipBody()).isEqualTo("# EOF\n");
311322
}
312323

313324
@Test
@@ -318,11 +329,7 @@ public void testEmptyResponseGzipText() throws IOException {
318329
Response response = scrape("GET", nameParam("none_existing"), "Accept-Encoding", "gzip");
319330
assertThat(response.status).isEqualTo(200);
320331
assertThat(response.getHeader("Content-Encoding")).isEqualTo("gzip");
321-
String body =
322-
new String(
323-
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(response.body))),
324-
UTF_8);
325-
assertThat(body.length()).isZero();
332+
assertThat(response.gzipBody()).isEmpty();
326333
}
327334

328335
private String nameParam(String name) throws UnsupportedEncodingException {
@@ -411,5 +418,15 @@ private String getHeader(String name) {
411418
// HTTP headers are case-insensitive
412419
return headers.get(name.toLowerCase(Locale.ROOT));
413420
}
421+
422+
private String stringBody() {
423+
return new String(body, UTF_8);
424+
}
425+
426+
private String gzipBody() throws IOException {
427+
return new String(
428+
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))),
429+
UTF_8);
430+
}
414431
}
415432
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TYPE integration_test info
2+
# HELP integration_test Info metric on this integration test
3+
integration_test_info{test_name="exporter-servlet-tomcat-sample"} 1
4+
# TYPE temperature_celsius gauge
5+
# UNIT temperature_celsius celsius
6+
# HELP temperature_celsius Temperature in Celsius
7+
temperature_celsius{location="inside"} 23.0
8+
temperature_celsius{location="outside"} 27.0
9+
# TYPE uptime_seconds counter
10+
# UNIT uptime_seconds seconds
11+
# HELP uptime_seconds total number of seconds since this application was started
12+
uptime_seconds_total 17.0
13+
# EOF
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: "integration_test_info"
2+
help: "Info metric on this integration test"
3+
type: GAUGE
4+
metric {
5+
label {
6+
name: "test_name"
7+
value: "exporter-servlet-tomcat-sample"
8+
}
9+
gauge {
10+
value: 1.0
11+
}
12+
}
13+
name: "temperature_celsius"
14+
help: "Temperature in Celsius"
15+
type: GAUGE
16+
metric {
17+
label {
18+
name: "location"
19+
value: "inside"
20+
}
21+
gauge {
22+
value: 23.0
23+
}
24+
}
25+
metric {
26+
label {
27+
name: "location"
28+
value: "outside"
29+
}
30+
gauge {
31+
value: 27.0
32+
}
33+
}
34+
name: "uptime_seconds_total"
35+
help: "total number of seconds since this application was started"
36+
type: COUNTER
37+
metric {
38+
counter {
39+
value: 17.0
40+
}
41+
}
42+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# HELP integration_test_info Info metric on this integration test
2+
# TYPE integration_test_info gauge
3+
integration_test_info{test_name="exporter-servlet-tomcat-sample"} 1
4+
# HELP temperature_celsius Temperature in Celsius
5+
# TYPE temperature_celsius gauge
6+
temperature_celsius{location="inside"} 23.0
7+
temperature_celsius{location="outside"} 27.0
8+
# HELP uptime_seconds_total total number of seconds since this application was started
9+
# TYPE uptime_seconds_total counter
10+
uptime_seconds_total 17.0

integration-tests/it-exporter/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@
1212
<packaging>pom</packaging>
1313

1414
<name>Integration Tests - Exporter</name>
15-
<url>http://github.com/prometheus/client_java</url>
1615
<description>
1716
Integration tests for the Exporter modules
1817
</description>
1918

20-
<developers>
21-
<developer>
22-
<id>fstab</id>
23-
<name>Fabian Stäber</name>
24-
<email>[email protected]</email>
25-
</developer>
26-
</developers>
27-
2819
<modules>
2920
<module>it-exporter-servlet-tomcat-sample</module>
3021
<module>it-exporter-servlet-jetty-sample</module>

integration-tests/it-pushgateway/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<artifactId>it-pushgateway</artifactId>
1212

1313
<name>Integration Test - Pushgateway</name>
14-
<url>http://github.com/prometheus/client_java</url>
1514
<description>
1615
Integration tests for the Pushgateway Exporter
1716
</description>

integration-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<packaging>pom</packaging>
1414

1515
<name>Integration Tests</name>
16-
<url>http://github.com/prometheus/client_java</url>
1716
<description>
1817
Integration tests for the Exporter modules
1918
</description>
@@ -25,6 +24,7 @@
2524
<modules>
2625
<module>it-common</module>
2726
<module>it-exporter</module>
27+
<module>it-no-protobuf</module>
2828
<module>it-pushgateway</module>
2929
</modules>
3030

0 commit comments

Comments
 (0)