Skip to content

Commit f01093a

Browse files
committed
jetty-httpclient
1 parent 04b3353 commit f01093a

File tree

5 files changed

+96
-67
lines changed

5 files changed

+96
-67
lines changed

instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyClient12ResponseListenersInstrumentation.java

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.opentelemetry.context.Scope;
1717
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1818
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
19+
import javax.annotation.Nullable;
1920
import net.bytebuddy.asm.Advice;
2021
import net.bytebuddy.description.type.TypeDescription;
2122
import net.bytebuddy.matcher.ElementMatcher;
@@ -54,55 +55,47 @@ public void transform(TypeTransformer transformer) {
5455

5556
@SuppressWarnings("unused")
5657
public static class JettyHttpClient12RespListenersNotifyAdvice {
58+
59+
@Nullable
5760
@Advice.OnMethodEnter(suppress = Throwable.class)
58-
public static void onEnterNotify(
59-
@Advice.Argument(0) Response response,
60-
@Advice.Local("otelContext") Context context,
61-
@Advice.Local("otelScope") Scope scope) {
62-
context = (Context) response.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
63-
if (context != null) {
64-
scope = context.makeCurrent();
65-
}
61+
public static Scope onEnterNotify(@Advice.Argument(0) Response response) {
62+
63+
Context context =
64+
(Context) response.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
65+
return context == null ? null : context.makeCurrent();
6666
}
6767

6868
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
6969
public static void onExitNotify(
7070
@Advice.Argument(0) Response response,
7171
@Advice.Thrown Throwable throwable,
72-
@Advice.Local("otelContext") Context context,
73-
@Advice.Local("otelScope") Scope scope) {
74-
if (scope == null) {
75-
return;
76-
}
72+
@Advice.Enter @Nullable Scope scope) {
7773

78-
scope.close();
74+
if (scope != null) {
75+
scope.close();
76+
}
7977
}
8078
}
8179

8280
@SuppressWarnings("unused")
8381
public static class JettyHttpClient12CompleteListenersNotifyAdvice {
82+
8483
@Advice.OnMethodEnter(suppress = Throwable.class)
85-
public static void onEnterComplete(
86-
@Advice.Argument(0) Result result,
87-
@Advice.Local("otelContext") Context context,
88-
@Advice.Local("otelScope") Scope scope) {
89-
context = (Context) result.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
90-
if (context != null) {
91-
scope = context.makeCurrent();
92-
}
84+
public static Scope onEnterComplete(@Advice.Argument(0) Result result) {
85+
86+
Context context = (Context) result.getRequest().getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
87+
return context == null ? null : context.makeCurrent();
9388
}
9489

9590
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
9691
public static void onExitComplete(
9792
@Advice.Argument(0) Result result,
9893
@Advice.Thrown Throwable throwable,
99-
@Advice.Local("otelContext") Context context,
100-
@Advice.Local("otelScope") Scope scope) {
101-
if (scope == null) {
102-
return;
103-
}
94+
@Advice.Enter @Nullable Scope scope) {
10495

105-
scope.close();
96+
if (scope != null) {
97+
scope.close();
98+
}
10699
}
107100
}
108101
}

instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12Instrumentation.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyClientTracingListener;
1818
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
20+
import javax.annotation.Nullable;
2021
import net.bytebuddy.asm.Advice;
2122
import net.bytebuddy.description.type.TypeDescription;
2223
import net.bytebuddy.matcher.ElementMatcher;
@@ -45,63 +46,70 @@ public void transform(TypeTransformer transformer) {
4546
@SuppressWarnings("unused")
4647
public static class JettyHttpClient12SendAdvice {
4748

49+
public static class AdviceLocals {
50+
public final Context context;
51+
public final Scope scope;
52+
53+
public AdviceLocals(Context context, Scope scope) {
54+
this.context = context;
55+
this.scope = scope;
56+
}
57+
}
58+
59+
@Nullable
4860
@Advice.OnMethodEnter(suppress = Throwable.class)
49-
public static void onEnterSend(
50-
@Advice.This HttpRequest request,
51-
@Advice.Local("otelContext") Context context,
52-
@Advice.Local("otelScope") Scope scope) {
61+
public static AdviceLocals onEnterSend(@Advice.This HttpRequest request) {
62+
5363
// start span
5464
Context parentContext = Context.current();
55-
context = JettyClientTracingListener.handleRequest(parentContext, request, instrumenter());
65+
Context context =
66+
JettyClientTracingListener.handleRequest(parentContext, request, instrumenter());
5667
if (context == null) {
57-
return;
68+
return null;
5869
}
5970
// set context for responseListeners
6071
request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext);
6172

62-
scope = context.makeCurrent();
73+
return new AdviceLocals(context, context.makeCurrent());
6374
}
6475

6576
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
6677
public static void onExitSend(
6778
@Advice.This HttpRequest request,
6879
@Advice.Thrown Throwable throwable,
69-
@Advice.Local("otelContext") Context context,
70-
@Advice.Local("otelScope") Scope scope) {
71-
if (scope == null) {
80+
@Advice.Enter @Nullable AdviceLocals locals) {
81+
82+
if (locals == null) {
7283
return;
7384
}
7485

7586
// not ending span here unless error, span ended in the interceptor
76-
scope.close();
87+
locals.scope.close();
7788
if (throwable != null) {
78-
instrumenter().end(context, request, null, throwable);
89+
instrumenter().end(locals.context, request, null, throwable);
7990
}
8091
}
8192
}
8293

8394
@SuppressWarnings("unused")
8495
public static class JettyHttpClient12NotifyAdvice {
96+
97+
@Nullable
8598
@Advice.OnMethodEnter(suppress = Throwable.class)
86-
public static void onEnterNotify(
87-
@Advice.This HttpRequest request,
88-
@Advice.Local("otelContext") Context context,
89-
@Advice.Local("otelScope") Scope scope) {
90-
context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
99+
public static Scope onEnterNotify(@Advice.This HttpRequest request) {
100+
101+
Context context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY);
91102
if (context == null) {
92-
return;
103+
return null;
93104
}
94-
scope = context.makeCurrent();
105+
return context.makeCurrent();
95106
}
96107

97108
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
98-
public static void onExitNotify(
99-
@Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) {
100-
if (scope == null) {
101-
return;
109+
public static void onExitNotify(@Advice.Enter Scope scope) {
110+
if (scope != null) {
111+
scope.close();
102112
}
103-
104-
scope.close();
105113
}
106114
}
107115
}

instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12InstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class JettyHttpClient12InstrumentationModule extends InstrumentationModule {
17+
public class JettyHttpClient12InstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719
public JettyHttpClient12InstrumentationModule() {
1820
super("jetty-httpclient", "jetty-httpclient-12.0");
1921
}
@@ -24,4 +26,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
2426
new JettyHttpClient12Instrumentation(),
2527
new JettyClient12ResponseListenersInstrumentation());
2628
}
29+
30+
@Override
31+
public boolean isIndyReady() {
32+
return true;
33+
}
2734
}

instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9Instrumentation.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2020
import java.util.List;
2121
import net.bytebuddy.asm.Advice;
22+
import net.bytebuddy.asm.Advice.AssignReturned;
23+
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
2224
import net.bytebuddy.description.type.TypeDescription;
2325
import net.bytebuddy.matcher.ElementMatcher;
2426
import org.eclipse.jetty.client.HttpRequest;
@@ -43,38 +45,50 @@ public void transform(TypeTransformer transformer) {
4345
@SuppressWarnings("unused")
4446
public static class JettyHttpClient9Advice {
4547

48+
public static class AdviceLocals {
49+
public final Context context;
50+
public final Scope scope;
51+
52+
public AdviceLocals(Context context, Scope scope) {
53+
this.context = context;
54+
this.scope = scope;
55+
}
56+
}
57+
58+
@AssignReturned.ToArguments(@ToArgument(value = 1, index = 1))
4659
@Advice.OnMethodEnter(suppress = Throwable.class)
47-
public static void addTracingEnter(
60+
public static Object[] addTracingEnter(
4861
@Advice.Argument(value = 0) HttpRequest httpRequest,
49-
@Advice.Argument(value = 1, readOnly = false) List<Response.ResponseListener> listeners,
50-
@Advice.Local("otelContext") Context context,
51-
@Advice.Local("otelScope") Scope scope) {
62+
@Advice.Argument(1) List<Response.ResponseListener> originalListeners) {
63+
64+
List<Response.ResponseListener> listeners = originalListeners;
5265
Context parentContext = currentContext();
53-
context =
66+
Context context =
5467
JettyClientTracingListener.handleRequest(parentContext, httpRequest, instrumenter());
5568
if (context == null) {
56-
return;
69+
return new Object[] {null, listeners};
5770
}
5871

5972
listeners = wrapResponseListeners(parentContext, listeners);
60-
scope = context.makeCurrent();
73+
return new Object[] {new AdviceLocals(context, context.makeCurrent()), listeners};
6174
}
6275

6376
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
6477
public static void exitTracingInterceptor(
6578
@Advice.Argument(value = 0) HttpRequest httpRequest,
6679
@Advice.Thrown Throwable throwable,
67-
@Advice.Local("otelContext") Context context,
68-
@Advice.Local("otelScope") Scope scope) {
80+
@Advice.Enter Object[] enterResult) {
81+
82+
AdviceLocals locals = (AdviceLocals) enterResult[0];
6983

70-
if (scope == null) {
84+
if (locals == null) {
7185
return;
7286
}
7387

7488
// not ending span here unless error, span ended in the interceptor
75-
scope.close();
89+
locals.scope.close();
7690
if (throwable != null) {
77-
instrumenter().end(context, httpRequest, null, throwable);
91+
instrumenter().end(locals.context, httpRequest, null, throwable);
7892
}
7993
}
8094
}

instrumentation/jetty-httpclient/jetty-httpclient-9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v9_2/JettyHttpClient9InstrumentationModule.java

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

1718
@AutoService(InstrumentationModule.class)
18-
public class JettyHttpClient9InstrumentationModule extends InstrumentationModule {
19+
public class JettyHttpClient9InstrumentationModule extends InstrumentationModule
20+
implements ExperimentalInstrumentationModule {
1921

2022
public JettyHttpClient9InstrumentationModule() {
2123
super("jetty-httpclient", "jetty-httpclient-9.2");
@@ -31,4 +33,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
3133
// AbstractTypedContentProvider showed up in version Jetty Client 9.2 on to 10.x
3234
return hasClassesNamed("org.eclipse.jetty.client.util.AbstractTypedContentProvider");
3335
}
36+
37+
@Override
38+
public boolean isIndyReady() {
39+
return true;
40+
}
3441
}

0 commit comments

Comments
 (0)