Skip to content

Commit d72ba58

Browse files
committed
smoke test
1 parent f1b6adc commit d72ba58

File tree

9 files changed

+192
-18
lines changed

9 files changed

+192
-18
lines changed

.github/workflows/native-tests.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: OpenTelemetry Acceptance Tests
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
acceptance-tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Set up JDK
15+
uses: actions/setup-java@v4
16+
with:
17+
java-version: 17
18+
distribution: graalvm
19+
cache: 'maven'
20+
- name: Run the Maven verify phase
21+
run: ./scripts/run-native-tests.sh

integration-tests/it-common/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
Common utilities for integration tests
1717
</description>
1818

19+
<dependencies>
20+
<dependency>
21+
<groupId>io.prometheus</groupId>
22+
<artifactId>prometheus-metrics-exposition-formats</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
</dependencies>
26+
1927
<build>
2028
<testResources>
2129
<testResource>

integration-tests/it-common/src/test/java/io/prometheus/client/it/common/ExporterTest.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66

77
import java.io.ByteArrayInputStream;
88
import java.io.IOException;
9+
import java.io.InputStream;
910
import java.net.HttpURLConnection;
1011
import java.net.URISyntaxException;
1112
import java.net.URL;
13+
import java.util.ArrayList;
1214
import java.util.HashMap;
1315
import java.util.List;
1416
import java.util.Locale;
1517
import java.util.Map;
1618
import java.util.concurrent.TimeUnit;
1719
import java.util.zip.GZIPInputStream;
20+
21+
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
1822
import org.apache.commons.io.IOUtils;
1923
import org.junit.jupiter.api.AfterEach;
2024
import org.testcontainers.containers.BindMode;
@@ -55,21 +59,23 @@ public void tearDown() throws IOException {
5559
sampleAppVolume.remove();
5660
}
5761

58-
protected void assertContentType(String expected, String actual) {
62+
public static void assertContentType(String expected, String actual) {
5963
if (!expected.replace(" ", "").equals(actual)) {
6064
assertThat(actual).isEqualTo(expected);
6165
}
6266
}
6367

6468
protected Response scrape(String method, String queryString, String... requestHeaders)
6569
throws IOException {
70+
return scrape(method,new URL("http://localhost:"
71+
+ sampleAppContainer.getMappedPort(9400)
72+
+ "/metrics?"
73+
+ queryString), requestHeaders);
74+
}
75+
76+
public static Response scrape(String method, URL url, String... requestHeaders)
77+
throws IOException {
6678
long timeoutMillis = TimeUnit.SECONDS.toMillis(5);
67-
URL url =
68-
new URL(
69-
"http://localhost:"
70-
+ sampleAppContainer.getMappedPort(9400)
71-
+ "/metrics?"
72-
+ queryString);
7379
HttpURLConnection con = (HttpURLConnection) url.openConnection();
7480
con.setRequestMethod(method);
7581
for (int i = 0; i < requestHeaders.length; i += 2) {
@@ -106,7 +112,7 @@ protected Response scrape(String method, String queryString, String... requestHe
106112
return null; // will not happen
107113
}
108114

109-
protected static class Response {
115+
public static class Response {
110116
public final int status;
111117
private final Map<String, String> headers;
112118
public final byte[] body;
@@ -136,5 +142,14 @@ public String gzipBody() throws IOException {
136142
return new String(
137143
IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))), UTF_8);
138144
}
145+
146+
public List<Metrics.MetricFamily> protoBody() throws IOException {
147+
List<Metrics.MetricFamily> metrics = new ArrayList<>();
148+
InputStream in = new ByteArrayInputStream(body);
149+
while (in.available() > 0) {
150+
metrics.add(Metrics.MetricFamily.parseDelimitedFrom(in));
151+
}
152+
return metrics;
153+
}
139154
}
140155
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
</description>
1818

1919
<dependencies>
20-
<dependency>
21-
<groupId>io.prometheus</groupId>
22-
<artifactId>prometheus-metrics-exposition-formats</artifactId>
23-
<version>${project.version}</version>
24-
</dependency>
2520
<dependency>
2621
<groupId>io.prometheus</groupId>
2722
<artifactId>it-common</artifactId>

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ public void testPrometheusProtobufFormat() throws IOException {
8282
assertThat(response.getHeader("Transfer-Encoding")).isNull();
8383
assertThat(response.getHeader("Content-Length"))
8484
.isEqualTo(Integer.toString(response.body.length));
85-
List<Metrics.MetricFamily> metrics = new ArrayList<>();
86-
InputStream in = new ByteArrayInputStream(response.body);
87-
while (in.available() > 0) {
88-
metrics.add(Metrics.MetricFamily.parseDelimitedFrom(in));
89-
}
85+
List<Metrics.MetricFamily> metrics = response.protoBody();
9086
assertThat(metrics).hasSize(3);
9187
// metrics are sorted by name
9288
assertThat(metrics.get(0).getName()).isEqualTo("integration_test_info");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.3.5</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
14+
<groupId>io.prometheus</groupId>
15+
<artifactId>it-spring-boot-smoke-test</artifactId>
16+
<version>0.1</version>
17+
18+
<name>Integration Test - Spring Smoke Tests</name>
19+
<description>
20+
Spring Smoke Tests
21+
</description>
22+
<properties>
23+
<java.version>17</java.version>
24+
</properties>
25+
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>io.prometheus</groupId>
30+
<artifactId>prometheus-metrics-bom</artifactId>
31+
<version>${project.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
</dependencies>
36+
</dependencyManagement>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-actuator</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.micrometer</groupId>
49+
<artifactId>micrometer-registry-prometheus</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.prometheus</groupId>
60+
<artifactId>it-common</artifactId>
61+
<type>test-jar</type>
62+
<version>${project.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.graalvm.buildtools</groupId>
71+
<artifactId>native-maven-plugin</artifactId>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-maven-plugin</artifactId>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.prometheus.metrics.it.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.prometheus.metrics.it.springboot;
2+
3+
import io.prometheus.client.it.common.ExporterTest;
4+
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
import java.io.IOException;
9+
import java.net.URL;
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
@SpringBootTest
16+
class ApplicationTest {
17+
@Test
18+
public void testPrometheusProtobufFormat() throws IOException {
19+
ExporterTest.Response response =
20+
ExporterTest.scrape(
21+
"GET",
22+
new URL("http://localhost:8080/actuator/prometheus"),
23+
"Accept",
24+
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited");
25+
assertThat(response.status).isEqualTo(200);
26+
27+
List<Metrics.MetricFamily> metrics = response.protoBody();
28+
Optional<Metrics.MetricFamily> metric = metrics.stream()
29+
.filter(m -> m.getName().equals("application_started_time_seconds"))
30+
.findFirst();
31+
assertThat(metric).isPresent();
32+
}
33+
}

scripts/run-native-tests.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
./mvnw clean install -DskipTests
6+
7+
PROJECT_VERSION=$(sed -nE 's/<version>(.*)<\/version>/\1/p' pom.xml | head -1 | xargs)
8+
9+
cd integration-tests/it-spring-boot-smoke-test
10+
11+
# replace the version in the pom.xml
12+
sed -i "s/<version>0.1<\/version>/<version>$PROJECT_VERSION<\/version>/" pom.xml
13+
14+
../../mvnw test -PnativeTest

0 commit comments

Comments
 (0)