Skip to content

Commit b8e46db

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

File tree

10 files changed

+391
-188
lines changed

10 files changed

+391
-188
lines changed

integration-tests/it-common/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.prometheus.client.it.common;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.assertj.core.api.Assertions.fail;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.net.HttpURLConnection;
10+
import java.net.URISyntaxException;
11+
import java.net.URL;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Locale;
15+
import java.util.Map;
16+
import java.util.concurrent.TimeUnit;
17+
import java.util.zip.GZIPInputStream;
18+
import org.apache.commons.io.IOUtils;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.testcontainers.containers.BindMode;
21+
import org.testcontainers.containers.GenericContainer;
22+
23+
public abstract class ExporterTest {
24+
private final GenericContainer<?> sampleAppContainer;
25+
private final Volume sampleAppVolume;
26+
protected final String sampleApp;
27+
28+
public ExporterTest(String sampleApp) throws IOException, URISyntaxException {
29+
this.sampleApp = sampleApp;
30+
this.sampleAppVolume =
31+
Volume.create("it-exporter")
32+
.copy("../../it-" + sampleApp + "/target/" + sampleApp + ".jar");
33+
this.sampleAppContainer =
34+
new GenericContainer<>("openjdk:17")
35+
.withFileSystemBind(sampleAppVolume.getHostPath(), "/app", BindMode.READ_ONLY)
36+
.withWorkingDirectory("/app")
37+
.withLogConsumer(LogConsumer.withPrefix(sampleApp))
38+
.withExposedPorts(9400);
39+
}
40+
41+
// @BeforeEach?
42+
protected void start() {
43+
start("success");
44+
}
45+
46+
protected void start(String outcome) {
47+
sampleAppContainer
48+
.withCommand("java", "-jar", "/app/" + sampleApp + ".jar", "9400", outcome)
49+
.start();
50+
}
51+
52+
@AfterEach
53+
public void tearDown() throws IOException {
54+
sampleAppContainer.stop();
55+
sampleAppVolume.remove();
56+
}
57+
58+
protected void assertContentType(String expected, String actual) {
59+
if (!expected.replace(" ", "").equals(actual)) {
60+
assertThat(actual).isEqualTo(expected);
61+
}
62+
}
63+
64+
protected Response scrape(String method, String queryString, String... requestHeaders)
65+
throws IOException {
66+
long timeoutMillis = TimeUnit.SECONDS.toMillis(5);
67+
URL url =
68+
new URL(
69+
"http://localhost:"
70+
+ sampleAppContainer.getMappedPort(9400)
71+
+ "/metrics?"
72+
+ queryString);
73+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
74+
con.setRequestMethod(method);
75+
for (int i = 0; i < requestHeaders.length; i += 2) {
76+
con.setRequestProperty(requestHeaders[i], requestHeaders[i + 1]);
77+
}
78+
long start = System.currentTimeMillis();
79+
Exception exception = null;
80+
while (System.currentTimeMillis() - start < timeoutMillis) {
81+
try {
82+
if (con.getResponseCode() == 200) {
83+
return new Response(
84+
con.getResponseCode(),
85+
con.getHeaderFields(),
86+
IOUtils.toByteArray(con.getInputStream()));
87+
} else {
88+
return new Response(
89+
con.getResponseCode(),
90+
con.getHeaderFields(),
91+
IOUtils.toByteArray(con.getErrorStream()));
92+
}
93+
} catch (Exception e) {
94+
exception = e;
95+
try {
96+
Thread.sleep(100);
97+
} catch (InterruptedException ignored) {
98+
// ignore
99+
}
100+
}
101+
}
102+
if (exception != null) {
103+
exception.printStackTrace();
104+
}
105+
fail("timeout while getting metrics from " + url);
106+
return null; // will not happen
107+
}
108+
109+
protected static class Response {
110+
public final int status;
111+
private final Map<String, String> headers;
112+
public final byte[] body;
113+
114+
private Response(int status, Map<String, List<String>> headers, byte[] body) {
115+
this.status = status;
116+
this.headers = new HashMap<>(headers.size());
117+
this.body = body;
118+
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
119+
if (entry.getKey()
120+
!= null) { // HttpUrlConnection uses pseudo key "null" for the status line
121+
this.headers.put(entry.getKey().toLowerCase(Locale.ROOT), entry.getValue().get(0));
122+
}
123+
}
124+
}
125+
126+
public String getHeader(String name) {
127+
// HTTP headers are case-insensitive
128+
return headers.get(name.toLowerCase(Locale.ROOT));
129+
}
130+
131+
public String stringBody() {
132+
return new String(body, UTF_8);
133+
}
134+
135+
public String gzipBody() throws IOException {
136+
return new String(
137+
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))), UTF_8);
138+
}
139+
}
140+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>io.prometheus</groupId>
8+
<artifactId>it-exporter</artifactId>
9+
<version>1.3.2</version>
10+
</parent>
11+
12+
<artifactId>it-exporter-no-protobuf</artifactId>
13+
14+
<name>Integration Tests - HTTPServer Exporter Sample - no protobuf</name>
15+
<description>
16+
HTTPServer Sample for the Exporter Integration Test without protobuf
17+
</description>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.prometheus</groupId>
22+
<artifactId>prometheus-metrics-exporter-httpserver</artifactId>
23+
<version>${project.version}</version>
24+
<exclusions>
25+
<exclusion>
26+
<groupId>io.prometheus</groupId>
27+
<artifactId>prometheus-metrics-exposition-formats</artifactId>
28+
</exclusion>
29+
</exclusions>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.prometheus</groupId>
33+
<artifactId>prometheus-metrics-core</artifactId>
34+
<version>${project.version}</version>
35+
<exclusions>
36+
<exclusion>
37+
<groupId>io.prometheus</groupId>
38+
<artifactId>prometheus-metrics-exposition-formats</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<finalName>exporter-no-protobuf</finalName>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-shade-plugin</artifactId>
50+
<executions>
51+
<execution>
52+
<phase>package</phase>
53+
<goals>
54+
<goal>shade</goal>
55+
</goals>
56+
<configuration>
57+
<transformers>
58+
<transformer
59+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
60+
<mainClass>io.prometheus.metrics.it.exporter.httpserver.HTTPServerSample</mainClass>
61+
</transformer>
62+
</transformers>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.prometheus.metrics.it.exporter.httpserver;
2+
3+
import io.prometheus.metrics.core.metrics.Counter;
4+
import io.prometheus.metrics.core.metrics.Gauge;
5+
import io.prometheus.metrics.core.metrics.Info;
6+
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
7+
import io.prometheus.metrics.model.registry.Collector;
8+
import io.prometheus.metrics.model.registry.PrometheusRegistry;
9+
import io.prometheus.metrics.model.snapshots.Unit;
10+
import java.io.IOException;
11+
12+
public class HTTPServerSample {
13+
14+
enum Mode {
15+
success,
16+
error
17+
}
18+
19+
public static void main(String[] args) throws IOException, InterruptedException {
20+
21+
if (args.length != 2) {
22+
System.err.println("Usage: java -jar exporter-httpserver-sample.jar <port> <mode>");
23+
System.err.println("Where mode is \"success\" or \"error\".");
24+
System.exit(1);
25+
}
26+
27+
int port = parsePortOrExit(args[0]);
28+
Mode mode = parseModeOrExit(args[1]);
29+
30+
Counter counter =
31+
Counter.builder()
32+
.name("uptime_seconds_total")
33+
.help("total number of seconds since this application was started")
34+
.unit(Unit.SECONDS)
35+
.register();
36+
counter.inc(17);
37+
38+
Info info =
39+
Info.builder()
40+
.name("integration_test_info")
41+
.help("Info metric on this integration test")
42+
.labelNames("test_name")
43+
.register();
44+
info.addLabelValues("exporter-httpserver-sample");
45+
46+
Gauge gauge =
47+
Gauge.builder()
48+
.name("temperature_celsius")
49+
.help("Temperature in Celsius")
50+
.unit(Unit.CELSIUS)
51+
.labelNames("location")
52+
.register();
53+
gauge.labelValues("inside").set(23.0);
54+
gauge.labelValues("outside").set(27.0);
55+
56+
if (mode == Mode.error) {
57+
Collector failingCollector =
58+
() -> {
59+
throw new RuntimeException("Simulating an error.");
60+
};
61+
62+
PrometheusRegistry.defaultRegistry.register(failingCollector);
63+
}
64+
65+
HTTPServer server = HTTPServer.builder().port(port).buildAndStart();
66+
67+
System.out.println(
68+
"HTTPServer listening on port http://localhost:" + server.getPort() + "/metrics");
69+
Thread.currentThread().join(); // wait forever
70+
}
71+
72+
private static int parsePortOrExit(String port) {
73+
try {
74+
return Integer.parseInt(port);
75+
} catch (NumberFormatException e) {
76+
System.err.println("\"" + port + "\": Invalid port number.");
77+
System.exit(1);
78+
}
79+
return 0; // this won't happen
80+
}
81+
82+
private static Mode parseModeOrExit(String mode) {
83+
try {
84+
return Mode.valueOf(mode);
85+
} catch (IllegalArgumentException e) {
86+
System.err.println(
87+
"\"" + mode + "\": Invalid mode. Legal values are \"success\" and \"error\".");
88+
System.exit(1);
89+
}
90+
return null; // this won't happen
91+
}
92+
}

integration-tests/it-exporter/it-exporter-test/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
<artifactId>prometheus-metrics-exposition-formats</artifactId>
2323
<version>${project.version}</version>
2424
</dependency>
25-
<dependency>
26-
<groupId>commons-io</groupId>
27-
<artifactId>commons-io</artifactId>
28-
<version>2.17.0</version>
29-
<scope>test</scope>
30-
</dependency>
3125
<dependency>
3226
<groupId>io.prometheus</groupId>
3327
<artifactId>it-common</artifactId>

0 commit comments

Comments
 (0)