Skip to content

Commit d69c081

Browse files
committed
vertx-http-client-4.0
1 parent ebfcd84 commit d69c081

File tree

6 files changed

+95
-60
lines changed

6 files changed

+95
-60
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1313
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1414
import io.vertx.core.Handler;
15+
import javax.annotation.Nullable;
1516
import net.bytebuddy.asm.Advice;
17+
import net.bytebuddy.asm.Advice.AssignReturned;
18+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
1619
import net.bytebuddy.description.type.TypeDescription;
1720
import net.bytebuddy.matcher.ElementMatcher;
1821

@@ -42,28 +45,31 @@ public void transform(TypeTransformer transformer) {
4245

4346
@SuppressWarnings("unused")
4447
public static class GetConnectionArg2Advice {
48+
@Nullable
49+
@AssignReturned.ToArguments(@ToArgument(2))
4550
@Advice.OnMethodEnter(suppress = Throwable.class)
46-
public static void wrapHandler(
47-
@Advice.Argument(value = 2, readOnly = false) Handler<?> handler) {
48-
handler = HandlerWrapper.wrap(handler);
51+
public static Handler<?> wrapHandler(@Advice.Argument(2) @Nullable Handler<?> handler) {
52+
return HandlerWrapper.wrap(handler);
4953
}
5054
}
5155

5256
@SuppressWarnings("unused")
5357
public static class GetConnectionArg3Advice {
58+
@Nullable
59+
@AssignReturned.ToArguments(@ToArgument(3))
5460
@Advice.OnMethodEnter(suppress = Throwable.class)
55-
public static void wrapHandler(
56-
@Advice.Argument(value = 3, readOnly = false) Handler<?> handler) {
57-
handler = HandlerWrapper.wrap(handler);
61+
public static Handler<?> wrapHandler(@Advice.Argument(3) @Nullable Handler<?> handler) {
62+
return HandlerWrapper.wrap(handler);
5863
}
5964
}
6065

6166
@SuppressWarnings("unused")
6267
public static class GetConnectionArg4Advice {
68+
@Nullable
69+
@AssignReturned.ToArguments(@ToArgument(4))
6370
@Advice.OnMethodEnter(suppress = Throwable.class)
64-
public static void wrapHandler(
65-
@Advice.Argument(value = 4, readOnly = false) Handler<?> handler) {
66-
handler = HandlerWrapper.wrap(handler);
71+
public static Handler<?> wrapHandler(@Advice.Argument(4) @Nullable Handler<?> handler) {
72+
return HandlerWrapper.wrap(handler);
6773
}
6874
}
6975
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.context.Context;
99
import io.opentelemetry.context.Scope;
1010
import io.vertx.core.Handler;
11+
import javax.annotation.Nullable;
1112

1213
public class HandlerWrapper<T> implements Handler<T> {
1314
private final Handler<T> delegate;
@@ -18,7 +19,8 @@ private HandlerWrapper(Handler<T> delegate, Context context) {
1819
this.context = context;
1920
}
2021

21-
public static <T> Handler<T> wrap(Handler<T> handler) {
22+
@Nullable
23+
public static <T> Handler<T> wrap(@Nullable Handler<T> handler) {
2224
Context current = Context.current();
2325
if (handler != null && !(handler instanceof HandlerWrapper) && current != Context.root()) {
2426
handler = new HandlerWrapper<>(handler, current);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1515
import io.vertx.core.Handler;
1616
import net.bytebuddy.asm.Advice;
17+
import net.bytebuddy.asm.Advice.AssignReturned;
18+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
1719
import net.bytebuddy.description.type.TypeDescription;
1820
import net.bytebuddy.matcher.ElementMatcher;
1921

@@ -39,10 +41,10 @@ public void transform(TypeTransformer transformer) {
3941

4042
@SuppressWarnings("unused")
4143
public static class CreateStreamAdvice {
44+
@AssignReturned.ToArguments(@ToArgument(1))
4245
@Advice.OnMethodEnter(suppress = Throwable.class)
43-
public static void wrapHandler(
44-
@Advice.Argument(value = 1, readOnly = false) Handler<?> handler) {
45-
handler = HandlerWrapper.wrap(handler);
46+
public static Handler<?> wrapHandler(@Advice.Argument(1) Handler<?> handler) {
47+
return HandlerWrapper.wrap(handler);
4648
}
4749
}
4850
}

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

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

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
10+
import static io.opentelemetry.javaagent.instrumentation.vertx.v4_0.client.VertxClientSingletons.CONTEXTS;
1011
import static io.opentelemetry.javaagent.instrumentation.vertx.v4_0.client.VertxClientSingletons.instrumenter;
1112
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1213
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
@@ -16,7 +17,6 @@
1617

1718
import io.opentelemetry.context.Context;
1819
import io.opentelemetry.context.Scope;
19-
import io.opentelemetry.instrumentation.api.util.VirtualField;
2020
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2222
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -25,7 +25,10 @@
2525
import io.vertx.core.Handler;
2626
import io.vertx.core.http.HttpClientRequest;
2727
import io.vertx.core.http.HttpClientResponse;
28+
import javax.annotation.Nullable;
2829
import net.bytebuddy.asm.Advice;
30+
import net.bytebuddy.asm.Advice.AssignReturned;
31+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
2932
import net.bytebuddy.description.type.TypeDescription;
3033
import net.bytebuddy.matcher.ElementMatcher;
3134

@@ -85,61 +88,71 @@ public void transform(TypeTransformer transformer) {
8588
@SuppressWarnings("unused")
8689
public static class EndRequestAdvice {
8790

91+
public static class AdviceScope {
92+
public final Context context;
93+
public final Scope scope;
94+
95+
public AdviceScope(Context context, Scope scope) {
96+
this.context = context;
97+
this.scope = scope;
98+
}
99+
100+
public void end(HttpClientRequest request, Throwable throwable) {
101+
scope.close();
102+
if (throwable != null) {
103+
instrumenter().end(context, request, null, throwable);
104+
}
105+
}
106+
}
107+
108+
@Nullable
88109
@Advice.OnMethodEnter(suppress = Throwable.class)
89-
public static void attachContext(
90-
@Advice.This HttpClientRequest request,
91-
@Advice.Local("otelContext") Context context,
92-
@Advice.Local("otelScope") Scope scope) {
93-
Context parentContext = Java8BytecodeBridge.currentContext();
110+
public static AdviceScope attachContext(@Advice.This HttpClientRequest request) {
94111

112+
Context parentContext = Java8BytecodeBridge.currentContext();
95113
if (!instrumenter().shouldStart(parentContext, request)) {
96-
return;
114+
return null;
97115
}
98116

99-
context = instrumenter().start(parentContext, request);
117+
Context context = instrumenter().start(parentContext, request);
100118
Contexts contexts = new Contexts(parentContext, context);
101-
VirtualField.find(HttpClientRequest.class, Contexts.class).set(request, contexts);
119+
CONTEXTS.set(request, contexts);
102120

103-
scope = context.makeCurrent();
121+
return new AdviceScope(context, context.makeCurrent());
104122
}
105123

106124
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
107125
public static void endScope(
108126
@Advice.This HttpClientRequest request,
109-
@Advice.Local("otelContext") Context context,
110-
@Advice.Local("otelScope") Scope scope,
111-
@Advice.Thrown Throwable throwable) {
112-
if (scope != null) {
113-
scope.close();
114-
}
115-
if (throwable != null) {
116-
instrumenter().end(context, request, null, throwable);
127+
@Advice.Thrown Throwable throwable,
128+
@Advice.Enter @Nullable AdviceScope adviceScope) {
129+
if (adviceScope != null) {
130+
adviceScope.end(request, throwable);
117131
}
118132
}
119133
}
120134

121135
@SuppressWarnings("unused")
122136
public static class HandleExceptionAdvice {
123137

138+
@Nullable
124139
@Advice.OnMethodEnter(suppress = Throwable.class)
125-
public static void handleException(
126-
@Advice.This HttpClientRequest request,
127-
@Advice.Argument(0) Throwable t,
128-
@Advice.Local("otelScope") Scope scope) {
129-
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
140+
public static Scope handleException(
141+
@Advice.This HttpClientRequest request, @Advice.Argument(0) Throwable t) {
142+
Contexts contexts = CONTEXTS.get(request);
130143

131144
if (contexts == null) {
132-
return;
145+
return null;
133146
}
134147

135148
instrumenter().end(contexts.context, request, null, t);
136149

137150
// Scoping all potential callbacks etc to the parent context
138-
scope = contexts.parentContext.makeCurrent();
151+
return contexts.parentContext.makeCurrent();
139152
}
140153

141154
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
142-
public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
155+
public static void handleResponseExit(@Advice.Enter @Nullable Scope scope) {
143156
if (scope != null) {
144157
scope.close();
145158
}
@@ -149,25 +162,24 @@ public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
149162
@SuppressWarnings("unused")
150163
public static class HandleResponseAdvice {
151164

165+
@Nullable
152166
@Advice.OnMethodEnter(suppress = Throwable.class)
153-
public static void handleResponseEnter(
154-
@Advice.This HttpClientRequest request,
155-
@Advice.Argument(1) HttpClientResponse response,
156-
@Advice.Local("otelScope") Scope scope) {
157-
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
167+
public static Scope handleResponseEnter(
168+
@Advice.This HttpClientRequest request, @Advice.Argument(1) HttpClientResponse response) {
169+
Contexts contexts = CONTEXTS.get(request);
158170

159171
if (contexts == null) {
160-
return;
172+
return null;
161173
}
162174

163175
instrumenter().end(contexts.context, request, response, null);
164176

165177
// Scoping all potential callbacks etc to the parent context
166-
scope = contexts.parentContext.makeCurrent();
178+
return contexts.parentContext.makeCurrent();
167179
}
168180

169181
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
170-
public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
182+
public static void handleResponseExit(@Advice.Enter @Nullable Scope scope) {
171183
if (scope != null) {
172184
scope.close();
173185
}
@@ -177,19 +189,19 @@ public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
177189
@SuppressWarnings("unused")
178190
public static class MountContextAdvice {
179191

192+
@Nullable
180193
@Advice.OnMethodEnter(suppress = Throwable.class)
181-
public static void mountContext(
182-
@Advice.This HttpClientRequest request, @Advice.Local("otelScope") Scope scope) {
183-
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
194+
public static Scope mountContext(@Advice.This HttpClientRequest request) {
195+
Contexts contexts = CONTEXTS.get(request);
184196
if (contexts == null) {
185-
return;
197+
return null;
186198
}
187199

188-
scope = contexts.context.makeCurrent();
200+
return contexts.context.makeCurrent();
189201
}
190202

191203
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
192-
public static void unmountContext(@Advice.Local("otelScope") Scope scope) {
204+
public static void unmountContext(@Advice.Enter @Nullable Scope scope) {
193205
if (scope != null) {
194206
scope.close();
195207
}
@@ -199,15 +211,16 @@ public static void unmountContext(@Advice.Local("otelScope") Scope scope) {
199211
@SuppressWarnings("unused")
200212
public static class ExceptionHandlerAdvice {
201213

214+
@Nullable
215+
@AssignReturned.ToArguments(@ToArgument(0))
202216
@Advice.OnMethodEnter(suppress = Throwable.class)
203-
public static void wrapExceptionHandler(
217+
public static Handler<Throwable> wrapExceptionHandler(
204218
@Advice.This HttpClientRequest request,
205-
@Advice.Argument(value = 0, readOnly = false) Handler<Throwable> handler) {
206-
if (handler != null) {
207-
VirtualField<HttpClientRequest, Contexts> virtualField =
208-
VirtualField.find(HttpClientRequest.class, Contexts.class);
209-
handler = ExceptionHandlerWrapper.wrap(instrumenter(), request, virtualField, handler);
219+
@Advice.Argument(0) @Nullable Handler<Throwable> handler) {
220+
if (handler == null) {
221+
return null;
210222
}
223+
return ExceptionHandlerWrapper.wrap(instrumenter(), request, CONTEXTS, handler);
211224
}
212225
}
213226
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.google.auto.service.AutoService;
1313
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1516
import java.util.List;
1617
import net.bytebuddy.matcher.ElementMatcher;
1718

1819
@AutoService(InstrumentationModule.class)
19-
public class VertxClientInstrumentationModule extends InstrumentationModule {
20+
public class VertxClientInstrumentationModule extends InstrumentationModule
21+
implements ExperimentalInstrumentationModule {
2022

2123
public VertxClientInstrumentationModule() {
2224
super("vertx-http-client", "vertx-http-client-4.0", "vertx");
@@ -37,4 +39,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
3739
new HttpClientConnectionInstrumentation(),
3840
new HttpRequestInstrumentation());
3941
}
42+
43+
@Override
44+
public boolean isIndyReady() {
45+
return true;
46+
}
4047
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.client;
77

88
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
9+
import io.opentelemetry.instrumentation.api.util.VirtualField;
10+
import io.opentelemetry.javaagent.instrumentation.vertx.client.Contexts;
911
import io.opentelemetry.javaagent.instrumentation.vertx.client.VertxClientInstrumenterFactory;
1012
import io.vertx.core.http.HttpClientRequest;
1113
import io.vertx.core.http.HttpClientResponse;
@@ -16,6 +18,9 @@ public final class VertxClientSingletons {
1618
VertxClientInstrumenterFactory.create(
1719
"io.opentelemetry.vertx-http-client-4.0", new Vertx4HttpAttributesGetter());
1820

21+
public static final VirtualField<HttpClientRequest, Contexts> CONTEXTS =
22+
VirtualField.find(HttpClientRequest.class, Contexts.class);
23+
1924
public static Instrumenter<HttpClientRequest, HttpClientResponse> instrumenter() {
2025
return INSTRUMENTER;
2126
}

0 commit comments

Comments
 (0)