Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

package io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client;

import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.HTTP_CLIENT_OPTIONS;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.vertx.core.http.HttpClientOptions;
Expand Down Expand Up @@ -36,7 +36,7 @@ public static class AttachStateAdvice {
public static void attachHttpClientOptions(
@Advice.This HttpClientImpl client,
@Advice.FieldValue("options") HttpClientOptions options) {
VirtualField.find(HttpClientImpl.class, HttpClientOptions.class).set(client, options);
HTTP_CLIENT_OPTIONS.set(client, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client;

import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.HTTP_CLIENT_OPTIONS;
import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.REQUEST_INFO;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.vertx.core.http.HttpClientOptions;
Expand Down Expand Up @@ -53,13 +54,11 @@ public static void attachRequestInfo(
@Advice.Argument(0) HttpClientImpl client,
@Advice.Argument(2) String host,
@Advice.Argument(3) int port) {
HttpClientOptions httpClientOptions =
VirtualField.find(HttpClientImpl.class, HttpClientOptions.class).get(client);
VirtualField.find(HttpClientRequest.class, VertxRequestInfo.class)
.set(
request,
VertxRequestInfo.create(
httpClientOptions != null && httpClientOptions.isSsl(), host, port));
HttpClientOptions httpClientOptions = HTTP_CLIENT_OPTIONS.get(client);
REQUEST_INFO.set(
request,
VertxRequestInfo.create(
httpClientOptions != null && httpClientOptions.isSsl(), host, port));
}
}

Expand All @@ -71,8 +70,7 @@ public static void attachRequestInfo(
@Advice.Argument(1) boolean ssl,
@Advice.Argument(3) String host,
@Advice.Argument(4) int port) {
VirtualField.find(HttpClientRequest.class, VertxRequestInfo.class)
.set(request, VertxRequestInfo.create(ssl, host, port));
REQUEST_INFO.set(request, VertxRequestInfo.create(ssl, host, port));
}
}

Expand All @@ -84,8 +82,7 @@ public static void attachRequestInfo(
@Advice.Argument(1) boolean ssl,
@Advice.Argument(4) String host,
@Advice.Argument(5) int port) {
VirtualField.find(HttpClientRequest.class, VertxRequestInfo.class)
.set(request, VertxRequestInfo.create(ssl, host, port));
REQUEST_INFO.set(request, VertxRequestInfo.create(ssl, host, port));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.CONTEXTS;
import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.REQUEST_INFO;
import static io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client.VertxClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
Expand All @@ -16,16 +18,17 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.vertx.client.Contexts;
import io.opentelemetry.javaagent.instrumentation.vertx.client.ExceptionHandlerWrapper;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned;
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

Expand Down Expand Up @@ -83,67 +86,76 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class EndRequestAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void attachContext(
@Advice.This HttpClientRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static class AdviceScope {
private final Context context;
private final Scope scope;

VertxRequestInfo requestInfo =
VirtualField.find(HttpClientRequest.class, VertxRequestInfo.class).get(request);
if (requestInfo == null) {
return;
private AdviceScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

Context parentContext = Java8BytecodeBridge.currentContext();
if (!instrumenter().shouldStart(parentContext, request)) {
return;
@Nullable
public static AdviceScope startAndAttachContext(HttpClientRequest request) {
VertxRequestInfo requestInfo = REQUEST_INFO.get(request);
if (requestInfo == null) {
return null;
}

Context parentContext = Context.current();
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
Context context = instrumenter().start(parentContext, request);
CONTEXTS.set(request, new Contexts(parentContext, context));
return new AdviceScope(context, context.makeCurrent());
}

context = instrumenter().start(parentContext, request);
Contexts contexts = new Contexts(parentContext, context);
VirtualField.find(HttpClientRequest.class, Contexts.class).set(request, contexts);
public void end(@Nullable Throwable throwable, HttpClientRequest request) {
scope.close();
if (throwable != null) {
instrumenter().end(context, request, null, throwable);
}
}
}

scope = context.makeCurrent();
@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope attachContext(@Advice.This HttpClientRequest request) {
return AdviceScope.startAndAttachContext(request);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void endScope(
@Advice.This HttpClientRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Thrown Throwable throwable) {
if (scope != null) {
scope.close();
}
if (throwable != null) {
instrumenter().end(context, request, null, throwable);
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable, request);
}
}
}

@SuppressWarnings("unused")
public static class HandleExceptionAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void handleException(
@Advice.This HttpClientRequest request,
@Advice.Argument(0) Throwable t,
@Advice.Local("otelScope") Scope scope) {
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
public static Scope handleException(
@Advice.This HttpClientRequest request, @Advice.Argument(0) Throwable t) {

Contexts contexts = CONTEXTS.get(request);
if (contexts == null) {
return;
return null;
}

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

// Scoping all potential callbacks etc to the parent context
scope = contexts.parentContext.makeCurrent();
return contexts.parentContext.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
public static void handleResponseExit(@Advice.Enter @Nullable Scope scope) {
if (scope != null) {
scope.close();
}
Expand All @@ -153,25 +165,23 @@ public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
@SuppressWarnings("unused")
public static class HandleResponseAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void handleResponseEnter(
@Advice.This HttpClientRequest request,
@Advice.Argument(0) HttpClientResponse response,
@Advice.Local("otelScope") Scope scope) {
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
public static Scope handleResponseEnter(
@Advice.This HttpClientRequest request, @Advice.Argument(0) HttpClientResponse response) {

Contexts contexts = CONTEXTS.get(request);
if (contexts == null) {
return;
return null;
}

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

// Scoping all potential callbacks etc to the parent context
scope = contexts.parentContext.makeCurrent();
return contexts.parentContext.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
public static void handleResponseExit(@Advice.Enter @Nullable Scope scope) {
if (scope != null) {
scope.close();
}
Expand All @@ -181,19 +191,18 @@ public static void handleResponseExit(@Advice.Local("otelScope") Scope scope) {
@SuppressWarnings("unused")
public static class MountContextAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void mountContext(
@Advice.This HttpClientRequest request, @Advice.Local("otelScope") Scope scope) {
Contexts contexts = VirtualField.find(HttpClientRequest.class, Contexts.class).get(request);
public static Scope mountContext(@Advice.This HttpClientRequest request) {
Contexts contexts = CONTEXTS.get(request);
if (contexts == null) {
return;
return null;
}

scope = contexts.context.makeCurrent();
return contexts.context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void unmountContext(@Advice.Local("otelScope") Scope scope) {
public static void unmountContext(@Advice.Enter @Nullable Scope scope) {
if (scope != null) {
scope.close();
}
Expand All @@ -203,15 +212,16 @@ public static void unmountContext(@Advice.Local("otelScope") Scope scope) {
@SuppressWarnings("unused")
public static class ExceptionHandlerAdvice {

@Nullable
@AssignReturned.ToArguments(@ToArgument(0))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrapExceptionHandler(
public static Handler<Throwable> wrapExceptionHandler(
@Advice.This HttpClientRequest request,
@Advice.Argument(value = 0, readOnly = false) Handler<Throwable> handler) {
if (handler != null) {
VirtualField<HttpClientRequest, Contexts> virtualField =
VirtualField.find(HttpClientRequest.class, Contexts.class);
handler = ExceptionHandlerWrapper.wrap(instrumenter(), request, virtualField, handler);
@Advice.Argument(0) @Nullable Handler<Throwable> handler) {
if (handler == null) {
return null;
}
return ExceptionHandlerWrapper.wrap(instrumenter(), request, CONTEXTS, handler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class VertxClientInstrumentationModule extends InstrumentationModule {
public class VertxClientInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public VertxClientInstrumentationModule() {
super("vertx-http-client", "vertx-http-client-3.0", "vertx");
Expand All @@ -34,4 +36,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new HttpRequestImplInstrumentation(),
new HttpRequestInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
package io.opentelemetry.javaagent.instrumentation.vertx.v3_0.client;

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.instrumentation.vertx.client.Contexts;
import io.opentelemetry.javaagent.instrumentation.vertx.client.VertxClientInstrumenterFactory;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.impl.HttpClientImpl;

public final class VertxClientSingletons {

private static final Instrumenter<HttpClientRequest, HttpClientResponse> INSTRUMENTER =
VertxClientInstrumenterFactory.create(
"io.opentelemetry.vertx-http-client-3.0", new Vertx3HttpAttributesGetter());

public static final VirtualField<HttpClientRequest, Contexts> CONTEXTS =
VirtualField.find(HttpClientRequest.class, Contexts.class);

public static final VirtualField<HttpClientRequest, VertxRequestInfo> REQUEST_INFO =
VirtualField.find(HttpClientRequest.class, VertxRequestInfo.class);

public static final VirtualField<HttpClientImpl, HttpClientOptions> HTTP_CLIENT_OPTIONS =
VirtualField.find(HttpClientImpl.class, HttpClientOptions.class);

public static Instrumenter<HttpClientRequest, HttpClientResponse> instrumenter() {
return INSTRUMENTER;
}
Expand Down
Loading