diff --git a/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9Instrumentation.java b/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9Instrumentation.java new file mode 100644 index 000000000000..47e730d2abf6 --- /dev/null +++ b/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9Instrumentation.java @@ -0,0 +1,87 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2; + +import static io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyClientWrapUtil.wrapResponseListeners; +import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2.JettyHttpClientSingletons.instrumenter; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyHttpClient9TracingInterceptor; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import java.util.List; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.eclipse.jetty.client.HttpRequest; +import org.eclipse.jetty.client.api.Response; + +public class JettyHttpClient9Instrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("org.eclipse.jetty.client.HttpClient"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isMethod() + .and(named("send")) + .and(takesArgument(0, named("org.eclipse.jetty.client.HttpRequest"))) + .and(takesArgument(1, List.class)), + JettyHttpClient9Instrumentation.class.getName() + "$JettyHttpClient9Advice"); + } + + @SuppressWarnings("unused") + public static class JettyHttpClient9Advice { + + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void addTracingEnter( + @Advice.Argument(value = 0) HttpRequest httpRequest, + @Advice.Argument(value = 1, readOnly = false) List listeners, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + Context parentContext = currentContext(); + if (!instrumenter().shouldStart(parentContext, httpRequest)) { + return; + } + + // First step is to attach the tracer to the Jetty request. Request listeners are wrapped here + JettyHttpClient9TracingInterceptor requestInterceptor = + new JettyHttpClient9TracingInterceptor(parentContext, instrumenter()); + requestInterceptor.attachToRequest(httpRequest); + + // Second step is to wrap all the important result callback + listeners = wrapResponseListeners(parentContext, listeners); + + context = requestInterceptor.getContext(); + scope = context.makeCurrent(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) + public static void exitTracingInterceptor( + @Advice.Argument(value = 0) HttpRequest httpRequest, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + + if (scope == null) { + return; + } + + // not ending span here unless error, span ended in the interceptor + scope.close(); + if (throwable != null) { + instrumenter().end(context, httpRequest, null, throwable); + } + } + } +} diff --git a/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9InstrumentationModule.java b/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9InstrumentationModule.java index bc8e12713e14..2321e5c9c072 100644 --- a/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9InstrumentationModule.java +++ b/instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9InstrumentationModule.java @@ -5,27 +5,14 @@ package io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2; -import static io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyClientWrapUtil.wrapResponseListeners; import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed; -import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext; -import static io.opentelemetry.javaagent.instrumentation.jetty.httpclient.v9_2.JettyHttpClientSingletons.instrumenter; import static java.util.Collections.singletonList; -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.named; import com.google.auto.service.AutoService; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.instrumentation.jetty.httpclient.v9_2.internal.JettyHttpClient9TracingInterceptor; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; -import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import java.util.List; -import net.bytebuddy.asm.Advice; -import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; -import org.eclipse.jetty.client.HttpRequest; -import org.eclipse.jetty.client.api.Response; @AutoService(InstrumentationModule.class) public class JettyHttpClient9InstrumentationModule extends InstrumentationModule { @@ -44,63 +31,4 @@ public ElementMatcher.Junction classLoaderMatcher() { // AbstractTypedContentProvider showed up in version Jetty Client 9.2 on to 10.x return hasClassesNamed("org.eclipse.jetty.client.util.AbstractTypedContentProvider"); } - - public static class JettyHttpClient9Instrumentation implements TypeInstrumentation { - - @Override - public ElementMatcher typeMatcher() { - return named("org.eclipse.jetty.client.HttpClient"); - } - - @Override - public void transform(TypeTransformer transformer) { - transformer.applyAdviceToMethod( - isMethod().and(named("send")), - JettyHttpClient9InstrumentationModule.class.getName() + "$JettyHttpClient9Advice"); - } - } - - public static class JettyHttpClient9Advice { - - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void addTracingEnter( - @Advice.Argument(value = 0) HttpRequest httpRequest, - @Advice.Argument(value = 1, readOnly = false) List listeners, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = currentContext(); - if (!instrumenter().shouldStart(parentContext, httpRequest)) { - return; - } - - // First step is to attach the tracer to the Jetty request. Request listeners are wrapped here - JettyHttpClient9TracingInterceptor requestInterceptor = - new JettyHttpClient9TracingInterceptor(parentContext, instrumenter()); - requestInterceptor.attachToRequest(httpRequest); - - // Second step is to wrap all the important result callback - listeners = wrapResponseListeners(parentContext, listeners); - - context = requestInterceptor.getContext(); - scope = context.makeCurrent(); - } - - @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) - public static void exitTracingInterceptor( - @Advice.Argument(value = 0) HttpRequest httpRequest, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - if (scope == null) { - return; - } - - // not ending span here unless error, span ended in the interceptor - scope.close(); - if (throwable != null) { - instrumenter().end(context, httpRequest, null, throwable); - } - } - } }