|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.client; |
| 7 | + |
| 8 | +import static java.util.Collections.emptySet; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; |
| 16 | +import io.vertx.core.Future; |
| 17 | +import io.vertx.core.Vertx; |
| 18 | +import io.vertx.core.VertxOptions; |
| 19 | +import io.vertx.core.http.HttpClient; |
| 20 | +import io.vertx.core.http.HttpClientOptions; |
| 21 | +import io.vertx.core.http.HttpClientRequest; |
| 22 | +import io.vertx.core.http.HttpMethod; |
| 23 | +import io.vertx.core.http.RequestOptions; |
| 24 | +import java.net.URI; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 30 | + |
| 31 | +class VertxHttpClientTest extends AbstractHttpClientTest<Future<HttpClientRequest>> { |
| 32 | + |
| 33 | + @RegisterExtension |
| 34 | + static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); |
| 35 | + |
| 36 | + private final HttpClient httpClient = buildClient(); |
| 37 | + |
| 38 | + private static HttpClient buildClient() { |
| 39 | + Vertx vertx = Vertx.vertx(new VertxOptions()); |
| 40 | + HttpClientOptions clientOptions = |
| 41 | + new HttpClientOptions().setConnectTimeout(Math.toIntExact(CONNECTION_TIMEOUT.toMillis())); |
| 42 | + return vertx.createHttpClient(clientOptions); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Future<HttpClientRequest> buildRequest( |
| 47 | + String method, URI uri, Map<String, String> headers) { |
| 48 | + RequestOptions requestOptions = |
| 49 | + new RequestOptions().setMethod(HttpMethod.valueOf(method)).setAbsoluteURI(uri.toString()); |
| 50 | + headers.forEach(requestOptions::putHeader); |
| 51 | + return httpClient.request(requestOptions); |
| 52 | + } |
| 53 | + |
| 54 | + private static CompletableFuture<Integer> sendRequest(Future<HttpClientRequest> request) { |
| 55 | + CompletableFuture<Integer> future = new CompletableFuture<>(); |
| 56 | + |
| 57 | + request |
| 58 | + .compose( |
| 59 | + req -> |
| 60 | + req.send() |
| 61 | + .onComplete( |
| 62 | + asyncResult -> { |
| 63 | + if (asyncResult.succeeded()) { |
| 64 | + future.complete(asyncResult.result().statusCode()); |
| 65 | + } else { |
| 66 | + future.completeExceptionally(asyncResult.cause()); |
| 67 | + } |
| 68 | + })) |
| 69 | + .onFailure(future::completeExceptionally); |
| 70 | + |
| 71 | + return future; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int sendRequest( |
| 76 | + Future<HttpClientRequest> request, String method, URI uri, Map<String, String> headers) |
| 77 | + throws Exception { |
| 78 | + // Vertx doesn't seem to provide any synchronous API so bridge through a callback |
| 79 | + return sendRequest(request).get(30, TimeUnit.SECONDS); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void sendRequestWithCallback( |
| 84 | + Future<HttpClientRequest> request, |
| 85 | + String method, |
| 86 | + URI uri, |
| 87 | + Map<String, String> headers, |
| 88 | + HttpClientResult httpClientResult) { |
| 89 | + sendRequest(request) |
| 90 | + .whenComplete((status, throwable) -> httpClientResult.complete(() -> status, throwable)); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { |
| 95 | + optionsBuilder.disableTestRedirects(); |
| 96 | + optionsBuilder.disableTestReusedRequest(); |
| 97 | + optionsBuilder.disableTestHttps(); |
| 98 | + optionsBuilder.disableTestReadTimeout(); |
| 99 | + optionsBuilder.setHttpAttributes(VertxHttpClientTest::getHttpAttributes); |
| 100 | + optionsBuilder.setExpectedClientSpanNameMapper(VertxHttpClientTest::getExpectedClientSpanName); |
| 101 | + |
| 102 | + optionsBuilder.setSingleConnectionFactory(VertxSingleConnection::new); |
| 103 | + } |
| 104 | + |
| 105 | + private static Set<AttributeKey<?>> getHttpAttributes(URI uri) { |
| 106 | + String uriString = uri.toString(); |
| 107 | + // http://localhost:61/ => unopened port, http://192.0.2.1/ => non routable address |
| 108 | + if ("http://localhost:61/".equals(uriString) || "http://192.0.2.1/".equals(uriString)) { |
| 109 | + return emptySet(); |
| 110 | + } |
| 111 | + return HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES; |
| 112 | + } |
| 113 | + |
| 114 | + private static String getExpectedClientSpanName(URI uri, String method) { |
| 115 | + switch (uri.toString()) { |
| 116 | + case "http://localhost:61/": // unopened port |
| 117 | + case "http://192.0.2.1/": // non routable address |
| 118 | + return "CONNECT"; |
| 119 | + default: |
| 120 | + return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments