Skip to content

Commit 7b68f3b

Browse files
committed
fix instrumetnation for vert.x 5.1
1 parent 3886f3c commit 7b68f3b

7 files changed

Lines changed: 87 additions & 13 deletions

File tree

instrumentation/vertx/vertx-http-client/vertx-http-client-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v4_0/VertxClientInstrumentationModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
2828
// removed in 4.0
2929
return not(hasClassesNamed("io.vertx.core.Starter"))
3030
// added in 5.0
31-
.and(not(hasClassesNamed("io.vertx.core.http.impl.HttpClientConnectionInternal")));
31+
.and(not(hasClassesNamed("io.vertx.core.http.HttpClientConnection")));
3232
}
3333

3434
@Override

instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/ResourceManagerInstrumentation.java renamed to instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/HttpClientImplInstrumentation.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static net.bytebuddy.matcher.ElementMatchers.named;
99
import static net.bytebuddy.matcher.ElementMatchers.returns;
10+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
1011

1112
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1213
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -16,23 +17,24 @@
1617
import net.bytebuddy.description.type.TypeDescription;
1718
import net.bytebuddy.matcher.ElementMatcher;
1819

19-
/** Propagate context to connection established callback. */
20-
class ResourceManagerInstrumentation implements TypeInstrumentation {
20+
class HttpClientImplInstrumentation implements TypeInstrumentation {
2121

2222
@Override
2323
public ElementMatcher<TypeDescription> typeMatcher() {
24-
return named("io.vertx.core.internal.resource.ResourceManager");
24+
return named("io.vertx.core.http.impl.HttpClientImpl");
2525
}
2626

2727
@Override
2828
public void transform(TypeTransformer transformer) {
2929
transformer.applyAdviceToMethod(
30-
named("withResourceAsync").and(returns(named("io.vertx.core.Future"))),
31-
getClass().getName() + "$WithResourceAsyncAdvice");
30+
named("request")
31+
.and(takesArgument(0, named("io.vertx.core.http.RequestOptions")))
32+
.and(returns(named("io.vertx.core.Future"))),
33+
getClass().getName() + "$WrapFutureAdvice");
3234
}
3335

3436
@SuppressWarnings("unused")
35-
public static class WithResourceAsyncAdvice {
37+
public static class WrapFutureAdvice {
3638
@AssignReturned.ToReturned
3739
@Advice.OnMethodExit(suppress = Throwable.class, inline = false)
3840
public static Future<?> wrapFuture(@Advice.Return Future<?> future) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.vertx.httpclient.v5_0;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
9+
import static net.bytebuddy.matcher.ElementMatchers.named;
10+
11+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
12+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
13+
import io.vertx.core.http.HttpClientResponse;
14+
import io.vertx.core.http.HttpConnection;
15+
import net.bytebuddy.asm.Advice;
16+
import net.bytebuddy.asm.Advice.AssignReturned;
17+
import net.bytebuddy.description.type.TypeDescription;
18+
import net.bytebuddy.matcher.ElementMatcher;
19+
20+
class HttpClientResponseImplInstrumentation implements TypeInstrumentation {
21+
22+
@Override
23+
public ElementMatcher<TypeDescription> typeMatcher() {
24+
return named("io.vertx.core.http.impl.HttpClientResponseImpl");
25+
}
26+
27+
@Override
28+
public void transform(TypeTransformer transformer) {
29+
transformer.applyAdviceToMethod(isConstructor(), getClass().getName() + "$ConstructorAdvice");
30+
}
31+
32+
@SuppressWarnings("unused")
33+
public static class ConstructorAdvice {
34+
@AssignReturned.ToReturned
35+
@Advice.OnMethodExit(suppress = Throwable.class, inline = false)
36+
public static void setConnection(
37+
@Advice.This HttpClientResponse response,
38+
@Advice.FieldValue("conn") HttpConnection connection) {
39+
VertxClientSingletons.setConnection(response, connection);
40+
}
41+
}
42+
}

instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/Vertx5HttpAttributesGetter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.javaagent.instrumentation.vertx.httpclient.common.v3_0.AbstractVertxHttpAttributesGetter;
99
import io.vertx.core.http.HttpClientRequest;
1010
import io.vertx.core.http.HttpClientResponse;
11+
import io.vertx.core.http.HttpConnection;
1112
import io.vertx.core.http.HttpVersion;
1213
import io.vertx.core.net.HostAndPort;
1314
import io.vertx.core.net.SocketAddress;
@@ -79,7 +80,11 @@ public String getNetworkPeerAddress(
7980
if (response == null) {
8081
return null;
8182
}
82-
SocketAddress socketAddress = response.netSocket().remoteAddress();
83+
HttpConnection connection = VertxClientSingletons.getConnection(response);
84+
if (connection == null) {
85+
return null;
86+
}
87+
SocketAddress socketAddress = connection.remoteAddress();
8388
return socketAddress == null ? null : socketAddress.hostAddress();
8489
}
8590

@@ -90,7 +95,11 @@ public Integer getNetworkPeerPort(
9095
if (response == null) {
9196
return null;
9297
}
93-
SocketAddress socketAddress = response.netSocket().remoteAddress();
98+
HttpConnection connection = VertxClientSingletons.getConnection(response);
99+
if (connection == null) {
100+
return null;
101+
}
102+
SocketAddress socketAddress = connection.remoteAddress();
94103
return socketAddress == null ? null : socketAddress.port();
95104
}
96105
}

instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/VertxClientInstrumentationModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ public VertxClientInstrumentationModule() {
2525
@Override
2626
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
2727
// added in 5.0
28-
return hasClassesNamed("io.vertx.core.http.impl.HttpClientConnectionInternal");
28+
return hasClassesNamed("io.vertx.core.http.HttpClientConnection");
2929
}
3030

3131
@Override
3232
public List<TypeInstrumentation> typeInstrumentations() {
3333
return asList(
3434
new HttpRequestInstrumentation(),
3535
new HttpClientRequestBaseInstrumentation(),
36-
new ResourceManagerInstrumentation(),
36+
new HttpClientResponseImplInstrumentation(),
37+
new HttpClientImplInstrumentation(),
3738
new TaskQueueInstrumentation());
3839
}
3940
}

instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/VertxClientSingletons.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.vertx.core.Future;
1515
import io.vertx.core.http.HttpClientRequest;
1616
import io.vertx.core.http.HttpClientResponse;
17+
import io.vertx.core.http.HttpConnection;
1718
import io.vertx.core.net.HostAndPort;
1819
import java.util.concurrent.CompletableFuture;
1920
import javax.annotation.Nullable;
@@ -30,6 +31,9 @@ public class VertxClientSingletons {
3031
public static final VirtualField<HttpClientRequest, Contexts> CONTEXTS =
3132
VirtualField.find(HttpClientRequest.class, Contexts.class);
3233

34+
private static final VirtualField<HttpClientResponse, HttpConnection> CONNECTION_FIELD =
35+
VirtualField.find(HttpClientResponse.class, HttpConnection.class);
36+
3337
public static Instrumenter<HttpClientRequest, HttpClientResponse> instrumenter() {
3438
return instrumenter;
3539
}
@@ -43,6 +47,16 @@ public static HostAndPort getAuthority(HttpClientRequest request) {
4347
return AUTHORITY_FIELD.get(request);
4448
}
4549

50+
public static void setConnection(
51+
HttpClientResponse response, @Nullable HttpConnection connection) {
52+
CONNECTION_FIELD.set(response, connection);
53+
}
54+
55+
@Nullable
56+
public static HttpConnection getConnection(HttpClientResponse response) {
57+
return CONNECTION_FIELD.get(response);
58+
}
59+
4660
public static <T> Future<T> wrapFuture(Future<T> future) {
4761
Context context = Context.current();
4862
CompletableFuture<T> result = new CompletableFuture<>();

instrumentation/vertx/vertx-http-client/vertx-http-client-5.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/httpclient/v5_0/VertxSingleConnection.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.javaagent.instrumentation.vertx.httpclient.v5_0;
77

88
import static java.util.Objects.requireNonNull;
9+
import static java.util.concurrent.TimeUnit.SECONDS;
910

1011
import io.opentelemetry.instrumentation.testing.junit.http.SingleConnection;
1112
import io.vertx.core.Future;
@@ -19,6 +20,7 @@
1920
import io.vertx.core.http.RequestOptions;
2021
import java.util.Map;
2122
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.TimeoutException;
2224

2325
class VertxSingleConnection implements SingleConnection {
2426

@@ -37,14 +39,18 @@ class VertxSingleConnection implements SingleConnection {
3739

3840
@Override
3941
public int doRequest(String path, Map<String, String> headers)
40-
throws ExecutionException, InterruptedException {
42+
throws ExecutionException, InterruptedException, TimeoutException {
4143
String requestId = requireNonNull(headers.get(REQUEST_ID_HEADER));
4244
RequestOptions requestOptions = new RequestOptions().setHost(host).setPort(port).setURI(path);
4345
headers.forEach(requestOptions::putHeader);
4446
Future<HttpClientRequest> request = httpClient.request(requestOptions);
4547

4648
HttpClientResponse response =
47-
request.compose(HttpClientRequest::send).toCompletionStage().toCompletableFuture().get();
49+
request
50+
.compose(HttpClientRequest::send)
51+
.toCompletionStage()
52+
.toCompletableFuture()
53+
.get(10, SECONDS);
4854

4955
String responseId = response.getHeader(REQUEST_ID_HEADER);
5056
if (!requestId.equals(responseId)) {

0 commit comments

Comments
 (0)