|
| 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 io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics; |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.net.HttpURLConnection; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.net.URL; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Locale; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.zip.GZIPInputStream; |
| 21 | +import org.apache.commons.io.IOUtils; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.testcontainers.containers.BindMode; |
| 24 | +import org.testcontainers.containers.GenericContainer; |
| 25 | + |
| 26 | +public abstract class ExporterTest { |
| 27 | + private final GenericContainer<?> sampleAppContainer; |
| 28 | + private final Volume sampleAppVolume; |
| 29 | + protected final String sampleApp; |
| 30 | + |
| 31 | + public ExporterTest(String sampleApp) throws IOException, URISyntaxException { |
| 32 | + this.sampleApp = sampleApp; |
| 33 | + this.sampleAppVolume = |
| 34 | + Volume.create("it-exporter") |
| 35 | + .copy("../../it-" + sampleApp + "/target/" + sampleApp + ".jar"); |
| 36 | + this.sampleAppContainer = |
| 37 | + new GenericContainer<>("openjdk:17") |
| 38 | + .withFileSystemBind(sampleAppVolume.getHostPath(), "/app", BindMode.READ_ONLY) |
| 39 | + .withWorkingDirectory("/app") |
| 40 | + .withLogConsumer(LogConsumer.withPrefix(sampleApp)) |
| 41 | + .withExposedPorts(9400); |
| 42 | + } |
| 43 | + |
| 44 | + // @BeforeEach? |
| 45 | + protected void start() { |
| 46 | + start("success"); |
| 47 | + } |
| 48 | + |
| 49 | + protected void start(String outcome) { |
| 50 | + sampleAppContainer |
| 51 | + .withCommand("java", "-jar", "/app/" + sampleApp + ".jar", "9400", outcome) |
| 52 | + .start(); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterEach |
| 56 | + public void tearDown() throws IOException { |
| 57 | + sampleAppContainer.stop(); |
| 58 | + sampleAppVolume.remove(); |
| 59 | + } |
| 60 | + |
| 61 | + public static void assertContentType(String expected, String actual) { |
| 62 | + if (!expected.replace(" ", "").equals(actual)) { |
| 63 | + assertThat(actual).isEqualTo(expected); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected Response scrape(String method, String queryString, String... requestHeaders) |
| 68 | + throws IOException { |
| 69 | + return scrape( |
| 70 | + method, |
| 71 | + new URL( |
| 72 | + "http://localhost:" |
| 73 | + + sampleAppContainer.getMappedPort(9400) |
| 74 | + + "/metrics?" |
| 75 | + + queryString), |
| 76 | + requestHeaders); |
| 77 | + } |
| 78 | + |
| 79 | + public static Response scrape(String method, URL url, String... requestHeaders) |
| 80 | + throws IOException { |
| 81 | + long timeoutMillis = TimeUnit.SECONDS.toMillis(5); |
| 82 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 83 | + con.setRequestMethod(method); |
| 84 | + for (int i = 0; i < requestHeaders.length; i += 2) { |
| 85 | + con.setRequestProperty(requestHeaders[i], requestHeaders[i + 1]); |
| 86 | + } |
| 87 | + long start = System.currentTimeMillis(); |
| 88 | + Exception exception = null; |
| 89 | + while (System.currentTimeMillis() - start < timeoutMillis) { |
| 90 | + try { |
| 91 | + if (con.getResponseCode() == 200) { |
| 92 | + return new Response( |
| 93 | + con.getResponseCode(), |
| 94 | + con.getHeaderFields(), |
| 95 | + IOUtils.toByteArray(con.getInputStream())); |
| 96 | + } else { |
| 97 | + return new Response( |
| 98 | + con.getResponseCode(), |
| 99 | + con.getHeaderFields(), |
| 100 | + IOUtils.toByteArray(con.getErrorStream())); |
| 101 | + } |
| 102 | + } catch (Exception e) { |
| 103 | + exception = e; |
| 104 | + try { |
| 105 | + Thread.sleep(100); |
| 106 | + } catch (InterruptedException ignored) { |
| 107 | + // ignore |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + if (exception != null) { |
| 112 | + exception.printStackTrace(); |
| 113 | + } |
| 114 | + fail("timeout while getting metrics from " + url); |
| 115 | + return null; // will not happen |
| 116 | + } |
| 117 | + |
| 118 | + public static class Response { |
| 119 | + public final int status; |
| 120 | + private final Map<String, String> headers; |
| 121 | + public final byte[] body; |
| 122 | + |
| 123 | + private Response(int status, Map<String, List<String>> headers, byte[] body) { |
| 124 | + this.status = status; |
| 125 | + this.headers = new HashMap<>(headers.size()); |
| 126 | + this.body = body; |
| 127 | + for (Map.Entry<String, List<String>> entry : headers.entrySet()) { |
| 128 | + if (entry.getKey() |
| 129 | + != null) { // HttpUrlConnection uses pseudo key "null" for the status line |
| 130 | + this.headers.put(entry.getKey().toLowerCase(Locale.ROOT), entry.getValue().get(0)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public String getHeader(String name) { |
| 136 | + // HTTP headers are case-insensitive |
| 137 | + return headers.get(name.toLowerCase(Locale.ROOT)); |
| 138 | + } |
| 139 | + |
| 140 | + public String stringBody() { |
| 141 | + return new String(body, UTF_8); |
| 142 | + } |
| 143 | + |
| 144 | + public String gzipBody() throws IOException { |
| 145 | + return new String( |
| 146 | + IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))), UTF_8); |
| 147 | + } |
| 148 | + |
| 149 | + public List<Metrics.MetricFamily> protoBody() throws IOException { |
| 150 | + List<Metrics.MetricFamily> metrics = new ArrayList<>(); |
| 151 | + InputStream in = new ByteArrayInputStream(body); |
| 152 | + while (in.available() > 0) { |
| 153 | + metrics.add(Metrics.MetricFamily.parseDelimitedFrom(in)); |
| 154 | + } |
| 155 | + return metrics; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments