Skip to content

Commit 8456b73

Browse files
committed
Refactor integration tests; Add PushGateway test
1 parent 65ca8bd commit 8456b73

File tree

37 files changed

+926
-367
lines changed

37 files changed

+926
-367
lines changed

OTEL_EXEMPLARS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ If you want to see this in action, you can run the example from the `ExemplarsCl
88

99
```
1010
./mvnw package
11-
cd integration_tests/exemplars_otel_agent/target/
11+
cd integration_tests/it_exemplars_otel_agent/target/
1212
curl -LO https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.2.0/opentelemetry-javaagent-all.jar
13-
java -Dotel.traces.exporter=logging -Dotel.metrics.exporter=none -javaagent:./opentelemetry-javaagent-all.jar -jar ./sample-rest-application.jar
13+
java -Dotel.traces.exporter=logging -Dotel.metrics.exporter=none -javaagent:./opentelemetry-javaagent-all.jar -jar ./example-spring-boot-app.jar
1414
```
1515

1616
Now you have a Spring REST service running on [http://localhost:8080/hello](http://localhost:8080/hello) that is instrumented with the OpenTelemetry Java agent.
@@ -55,7 +55,7 @@ you can exclude the corresponding dependencies in your `pom.xml`:
5555
<artifactId>simpleclient</artifactId>
5656
<version>0.12.0</version>
5757
<exclusions>
58-
<!-- The following will disable OpenTelemetry exemplars when your application uses OpenTelemetry directly -->
58+
<!-- The following will disable OpenTelemetry exemplars when your application uses the OpenTelemetry API directly -->
5959
<exclusion>
6060
<groupId>io.prometheus</groupId>
6161
<artifactId>simpleclient_tracer_otel</artifactId>

integration_tests/exemplars_otel/src/test/java/io/prometheus/client/it/exemplars_otel/ExemplarsOtelIT.java

-103
This file was deleted.

integration_tests/exemplars_otel/src/test/resources/Dockerfile

-5
This file was deleted.

integration_tests/exemplars_otel_agent/src/test/resources/Dockerfile

-10
This file was deleted.

integration_tests/java_versions/pom.xml integration_tests/it_common/pom.xml

+10-20
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@
88
<version>0.12.1-SNAPSHOT</version>
99
</parent>
1010

11-
<artifactId>java_versions</artifactId>
12-
<name>Integration Test - Java Versions Smoke Test</name>
11+
<artifactId>it_common</artifactId>
12+
<name>Integration Tests - Common Utilities</name>
1313

1414
<dependencies>
1515
<dependency>
1616
<groupId>org.testcontainers</groupId>
1717
<artifactId>testcontainers</artifactId>
1818
<scope>test</scope>
1919
</dependency>
20-
<dependency>
21-
<groupId>ch.qos.logback</groupId>
22-
<artifactId>logback-classic</artifactId>
23-
<version>1.2.3</version>
24-
<scope>test</scope>
25-
</dependency>
2620
<dependency>
2721
<groupId>com.squareup.okhttp3</groupId>
2822
<artifactId>okhttp</artifactId>
29-
<version>4.9.1</version>
3023
<scope>test</scope>
3124
</dependency>
3225
</dependencies>
3326

3427
<build>
28+
<testResources>
29+
<testResource>
30+
<directory>src/test/resources</directory>
31+
<filtering>true</filtering>
32+
</testResource>
33+
</testResources>
3534
<plugins>
3635
<plugin>
3736
<groupId>org.apache.maven.plugins</groupId>
@@ -43,20 +42,11 @@
4342
</plugin>
4443
<plugin>
4544
<groupId>org.apache.maven.plugins</groupId>
46-
<artifactId>maven-failsafe-plugin</artifactId>
45+
<artifactId>maven-jar-plugin</artifactId>
4746
<executions>
4847
<execution>
49-
<id>integration-test</id>
50-
<phase>integration-test</phase>
51-
<goals>
52-
<goal>integration-test</goal>
53-
</goals>
54-
</execution>
55-
<execution>
56-
<id>verify</id>
57-
<phase>verify</phase>
5848
<goals>
59-
<goal>verify</goal>
49+
<goal>test-jar</goal>
6050
</goals>
6151
</execution>
6252
</executions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.prometheus.client.it.common;
2+
3+
import okhttp3.OkHttpClient;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
7+
import java.io.*;
8+
import java.net.URISyntaxException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
13+
public class Downloader {
14+
15+
/**
16+
* Download a file from an HTTP URL and store it in the target/ directory.
17+
*/
18+
public static void downloadToTarget(String url, String fileName) throws IOException, URISyntaxException {
19+
Path destination = Paths.get(Downloader.class.getResource("/").toURI()).getParent().resolve(fileName);
20+
if (Files.exists(destination)) {
21+
return; // only download once
22+
}
23+
System.out.println("Downloading " + url);
24+
OkHttpClient client = new OkHttpClient();
25+
Request request = new Request.Builder().url(url).build();
26+
Response response = client.newCall(request).execute();
27+
InputStream responseBody = response.body().byteStream();
28+
OutputStream output = new FileOutputStream(destination.toFile());
29+
byte[] data = new byte[1024];
30+
int count;
31+
while ((count = responseBody.read(data)) != -1) {
32+
output.write(data, 0, count);
33+
}
34+
output.close();
35+
responseBody.close();
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.prometheus.client.it.common;
2+
3+
import org.testcontainers.containers.output.OutputFrame;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Print Docker logs from TestContainers to stdout or stderr.
9+
*/
10+
public class LogConsumer implements Consumer<OutputFrame> {
11+
12+
private final String prefix;
13+
14+
private LogConsumer(String prefix) {
15+
this.prefix = prefix;
16+
}
17+
18+
public static LogConsumer withPrefix(String prefix) {
19+
return new LogConsumer(prefix);
20+
}
21+
22+
@Override
23+
public void accept(OutputFrame outputFrame) {
24+
switch (outputFrame.getType()) {
25+
case STDOUT:
26+
System.out.print(prefix + " - " + outputFrame.getUtf8String());
27+
break;
28+
case END:
29+
System.out.println(prefix + " - END");
30+
break;
31+
default: // STDERR or unexpected
32+
System.err.print(prefix + " - " + outputFrame.getUtf8String());
33+
break;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.prometheus.client.it.common;
2+
3+
import okhttp3.Credentials;
4+
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
import org.junit.Assert;
8+
9+
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
/**
14+
* Scrape metrics via HTTP
15+
*/
16+
public class Scraper {
17+
18+
/**
19+
* Scrape metrics from url without authentication.
20+
*/
21+
public static List<String> scrape(String url, long timeoutMillis) {
22+
return scrape(url, null, null, timeoutMillis);
23+
}
24+
25+
/**
26+
* Scrape metrics from url with HTTP basic authentication.
27+
*/
28+
public static List<String> scrape(String url, String user, String password, long timeoutMillis) {
29+
OkHttpClient client = new OkHttpClient();
30+
long start = System.currentTimeMillis();
31+
Exception exception = null;
32+
while (System.currentTimeMillis() - start < timeoutMillis) {
33+
try {
34+
Request.Builder requestBuilder = new Request.Builder()
35+
.header("Accept", "application/openmetrics-text; version=1.0.0; charset=utf-8")
36+
.url(url);
37+
if (user != null && password != null) {
38+
requestBuilder.header("Authorization", Credentials.basic(user, password));
39+
}
40+
Request request = requestBuilder.build();
41+
try (Response response = client.newCall(request).execute()) {
42+
if (response.code() != 200) {
43+
throw new IOException("Received HTTP Status " + response.code() + ": " + response.body().string());
44+
}
45+
return Arrays.asList(response.body().string().split("\\n"));
46+
}
47+
} catch (Exception e) {
48+
exception = e;
49+
try {
50+
Thread.sleep(100);
51+
} catch (InterruptedException ignored) {
52+
}
53+
}
54+
}
55+
if (exception != null) {
56+
exception.printStackTrace();
57+
}
58+
Assert.fail("timeout while getting metrics from " + url);
59+
return null; // will not happen
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.prometheus.client.it.common;
2+
3+
import java.io.IOException;
4+
import java.util.Properties;
5+
6+
/**
7+
* Utility to get the project version, like <tt>0.12.1-SNAPSHOT</tt>
8+
*/
9+
public class Version {
10+
11+
public static String loadProjectVersion() throws IOException {
12+
Properties properties = new Properties();
13+
properties.load(Volume.class.getResourceAsStream("/project_version.properties"));
14+
return properties.getProperty("project.version");
15+
}
16+
}

0 commit comments

Comments
 (0)