Skip to content

Commit af4266d

Browse files
committed
migrate httpclient5
1 parent ecd2ae2 commit af4266d

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

dd-java-agent/instrumentation/apache-httpclient/apache-httpasyncclient-4.0/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.instrumentation.apachehttpasyncclient;
22

3+
import static datadog.trace.agent.tooling.InstrumenterModule.TargetSystem.CONTEXT_TRACKING;
34
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
45
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
56
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
@@ -12,6 +13,7 @@
1213
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
1314

1415
import datadog.trace.agent.tooling.Instrumenter;
16+
import datadog.trace.agent.tooling.annotation.AppliesOn;
1517
import datadog.trace.api.InstrumenterConfig;
1618
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1719
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
@@ -61,15 +63,16 @@ public ElementMatcher<TypeDescription> hierarchyMatcher() {
6163

6264
@Override
6365
public void methodAdvice(MethodTransformer transformer) {
64-
transformer.applyAdvice(
66+
transformer.applyAdvices(
6567
isMethod()
6668
.and(named("execute"))
6769
.and(takesArguments(4))
6870
.and(takesArgument(0, named("org.apache.http.nio.protocol.HttpAsyncRequestProducer")))
6971
.and(takesArgument(1, named("org.apache.http.nio.protocol.HttpAsyncResponseConsumer")))
7072
.and(takesArgument(2, named("org.apache.http.protocol.HttpContext")))
7173
.and(takesArgument(3, named("org.apache.http.concurrent.FutureCallback"))),
72-
ApacheHttpAsyncClientInstrumentation.class.getName() + "$ClientAdvice");
74+
ApacheHttpAsyncClientInstrumentation.class.getName() + "$ClientAdvice",
75+
ApacheHttpAsyncClientInstrumentation.class.getName() + "$ClientContextPropagationAdvice");
7376
}
7477

7578
public static class ClientAdvice {
@@ -104,4 +107,14 @@ public static void methodExit(
104107
}
105108
}
106109
}
110+
111+
@AppliesOn(CONTEXT_TRACKING)
112+
public static class ClientContextPropagationAdvice {
113+
@Advice.OnMethodEnter(suppress = Throwable.class)
114+
public static void onEnter(@Advice.Argument(0) final HttpAsyncRequestProducer requestProducer) {
115+
if (requestProducer instanceof DelegatingRequestProducer) {
116+
((DelegatingRequestProducer) requestProducer).setInjectContext(true);
117+
}
118+
}
119+
}
107120
}

dd-java-agent/instrumentation/apache-httpclient/apache-httpasyncclient-4.0/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/DelegatingRequestProducer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
public class DelegatingRequestProducer implements HttpAsyncRequestProducer {
1818
final AgentSpan span;
1919
final HttpAsyncRequestProducer delegate;
20+
boolean injectContext = false;
2021

2122
public DelegatingRequestProducer(final AgentSpan span, final HttpAsyncRequestProducer delegate) {
2223
this.span = span;
2324
this.delegate = delegate;
2425
}
2526

27+
public void setInjectContext(boolean injectContext) {
28+
this.injectContext = injectContext;
29+
}
30+
2631
@Override
2732
public HttpHost getTarget() {
2833
return delegate.getTarget();
@@ -32,7 +37,9 @@ public HttpHost getTarget() {
3237
public HttpRequest generateRequest() throws IOException, HttpException {
3338
final HttpRequest request = delegate.generateRequest();
3439
DECORATE.onRequest(span, new HostAndRequestAsHttpUriRequest(delegate.getTarget(), request));
35-
DECORATE.injectContext(current().with(span), request, SETTER);
40+
if (injectContext) {
41+
DECORATE.injectContext(current().with(span), request, SETTER);
42+
}
3643
return request;
3744
}
3845

dd-java-agent/instrumentation/apache-httpclient/apache-httpclient-5.0/src/main/java/datadog/trace/instrumentation/apachehttpclient5/ApacheHttpAsyncClientInstrumentation.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.instrumentation.apachehttpclient5;
22

3+
import static datadog.trace.agent.tooling.InstrumenterModule.TargetSystem.CONTEXT_TRACKING;
34
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
45
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
56
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
@@ -14,6 +15,7 @@
1415
import com.google.auto.service.AutoService;
1516
import datadog.trace.agent.tooling.Instrumenter;
1617
import datadog.trace.agent.tooling.InstrumenterModule;
18+
import datadog.trace.agent.tooling.annotation.AppliesOn;
1719
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1820
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1921
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -76,7 +78,7 @@ public String[] helperClassNames() {
7678

7779
@Override
7880
public void methodAdvice(MethodTransformer transformer) {
79-
transformer.applyAdvice(
81+
transformer.applyAdvices(
8082
isMethod()
8183
.and(named("execute"))
8284
.and(takesArguments(5))
@@ -85,7 +87,8 @@ public void methodAdvice(MethodTransformer transformer) {
8587
.and(takesArgument(2, named("org.apache.hc.core5.http.nio.HandlerFactory")))
8688
.and(takesArgument(3, named("org.apache.hc.core5.http.protocol.HttpContext")))
8789
.and(takesArgument(4, named("org.apache.hc.core5.concurrent.FutureCallback"))),
88-
this.getClass().getName() + "$ClientAdvice");
90+
this.getClass().getName() + "$ClientAdvice",
91+
this.getClass().getName() + "$ClientContextPropagationAdvice");
8992
}
9093

9194
public static class ClientAdvice {
@@ -133,4 +136,14 @@ public static void methodExit(
133136
}
134137
}
135138
}
139+
140+
@AppliesOn(CONTEXT_TRACKING)
141+
public static class ClientContextPropagationAdvice {
142+
@Advice.OnMethodEnter(suppress = Throwable.class)
143+
public static void onEnter(@Advice.Argument(0) final AsyncRequestProducer requestProducer) {
144+
if (requestProducer instanceof DelegatingRequestProducer) {
145+
((DelegatingRequestProducer) requestProducer).setInjectContext(true);
146+
}
147+
}
148+
}
136149
}

dd-java-agent/instrumentation/apache-httpclient/apache-httpclient-5.0/src/main/java/datadog/trace/instrumentation/apachehttpclient5/DelegatingRequestChannel.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
public class DelegatingRequestChannel implements RequestChannel {
1616
private final RequestChannel delegate;
1717
private final AgentSpan span;
18+
private final boolean injectContext;
1819

19-
public DelegatingRequestChannel(RequestChannel requestChannel, AgentSpan span) {
20+
public DelegatingRequestChannel(
21+
RequestChannel requestChannel, AgentSpan span, boolean injectContext) {
2022
this.delegate = requestChannel;
2123
this.span = span;
24+
this.injectContext = injectContext;
2225
}
2326

2427
@Override
2528
public void sendRequest(HttpRequest request, EntityDetails entityDetails, HttpContext context)
2629
throws HttpException, IOException {
2730
DECORATE.onRequest(span, request);
28-
DECORATE.injectContext(current().with(span), request, SETTER);
31+
if (injectContext) {
32+
DECORATE.injectContext(current().with(span), request, SETTER);
33+
}
2934
delegate.sendRequest(request, entityDetails, context);
3035
}
3136
}

dd-java-agent/instrumentation/apache-httpclient/apache-httpclient-5.0/src/main/java/datadog/trace/instrumentation/apachehttpclient5/DelegatingRequestProducer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
public class DelegatingRequestProducer implements AsyncRequestProducer {
1212
final AgentSpan span;
1313
final AsyncRequestProducer delegate;
14+
boolean injectContext = false;
1415

1516
public DelegatingRequestProducer(final AgentSpan span, final AsyncRequestProducer delegate) {
1617
this.span = span;
1718
this.delegate = delegate;
1819
}
1920

21+
public void setInjectContext(boolean injectContext) {
22+
this.injectContext = injectContext;
23+
}
24+
2025
@Override
2126
public void failed(final Exception ex) {
2227
delegate.failed(ex);
@@ -25,7 +30,8 @@ public void failed(final Exception ex) {
2530
@Override
2631
public void sendRequest(RequestChannel channel, HttpContext context)
2732
throws HttpException, IOException {
28-
DelegatingRequestChannel requestChannel = new DelegatingRequestChannel(channel, span);
33+
DelegatingRequestChannel requestChannel =
34+
new DelegatingRequestChannel(channel, span, injectContext);
2935
delegate.sendRequest(requestChannel, context);
3036
}
3137

0 commit comments

Comments
 (0)