|
6 | 6 |
|
7 | 7 | import java.io.ByteArrayInputStream; |
8 | 8 | import java.io.IOException; |
| 9 | +import java.io.InputStream; |
9 | 10 | import java.net.HttpURLConnection; |
10 | 11 | import java.net.URISyntaxException; |
11 | 12 | import java.net.URL; |
| 13 | +import java.util.ArrayList; |
12 | 14 | import java.util.HashMap; |
13 | 15 | import java.util.List; |
14 | 16 | import java.util.Locale; |
15 | 17 | import java.util.Map; |
16 | 18 | import java.util.concurrent.TimeUnit; |
17 | 19 | import java.util.zip.GZIPInputStream; |
| 20 | + |
| 21 | +import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics; |
18 | 22 | import org.apache.commons.io.IOUtils; |
19 | 23 | import org.junit.jupiter.api.AfterEach; |
20 | 24 | import org.testcontainers.containers.BindMode; |
@@ -55,21 +59,23 @@ public void tearDown() throws IOException { |
55 | 59 | sampleAppVolume.remove(); |
56 | 60 | } |
57 | 61 |
|
58 | | - protected void assertContentType(String expected, String actual) { |
| 62 | + public static void assertContentType(String expected, String actual) { |
59 | 63 | if (!expected.replace(" ", "").equals(actual)) { |
60 | 64 | assertThat(actual).isEqualTo(expected); |
61 | 65 | } |
62 | 66 | } |
63 | 67 |
|
64 | 68 | protected Response scrape(String method, String queryString, String... requestHeaders) |
65 | 69 | 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 { |
66 | 78 | long timeoutMillis = TimeUnit.SECONDS.toMillis(5); |
67 | | - URL url = |
68 | | - new URL( |
69 | | - "http://localhost:" |
70 | | - + sampleAppContainer.getMappedPort(9400) |
71 | | - + "/metrics?" |
72 | | - + queryString); |
73 | 79 | HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
74 | 80 | con.setRequestMethod(method); |
75 | 81 | for (int i = 0; i < requestHeaders.length; i += 2) { |
@@ -106,7 +112,7 @@ protected Response scrape(String method, String queryString, String... requestHe |
106 | 112 | return null; // will not happen |
107 | 113 | } |
108 | 114 |
|
109 | | - protected static class Response { |
| 115 | + public static class Response { |
110 | 116 | public final int status; |
111 | 117 | private final Map<String, String> headers; |
112 | 118 | public final byte[] body; |
@@ -136,5 +142,14 @@ public String gzipBody() throws IOException { |
136 | 142 | return new String( |
137 | 143 | IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))), UTF_8); |
138 | 144 | } |
| 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 | + } |
139 | 154 | } |
140 | 155 | } |
0 commit comments