Skip to content

Commit 1c3f140

Browse files
authored
Properly finish spans and support latest apache httpclient5 (#8272)
1 parent 1efc71f commit 1c3f140

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.bytebuddy.matcher.ElementMatcher;
2222
import org.apache.hc.core5.concurrent.FutureCallback;
2323
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
24+
import org.apache.hc.core5.http.protocol.BasicHttpContext;
2425
import org.apache.hc.core5.http.protocol.HttpContext;
2526

2627
@AutoService(InstrumenterModule.class)
@@ -91,14 +92,18 @@ public static class ClientAdvice {
9192
@Advice.OnMethodEnter(suppress = Throwable.class)
9293
public static AgentScope methodEnter(
9394
@Advice.Argument(value = 0, readOnly = false) AsyncRequestProducer requestProducer,
94-
@Advice.Argument(3) HttpContext context,
95+
@Advice.Argument(value = 3, readOnly = false) HttpContext context,
9596
@Advice.Argument(value = 4, readOnly = false) FutureCallback<?> futureCallback) {
9697

9798
final AgentScope parentScope = activeScope();
9899
final AgentSpan clientSpan = startSpan(HTTP_REQUEST);
99100
final AgentScope clientScope = activateSpan(clientSpan);
100101
DECORATE.afterStart(clientSpan);
101102

103+
if (context == null) {
104+
context = new BasicHttpContext();
105+
}
106+
102107
requestProducer = new DelegatingRequestProducer(clientSpan, requestProducer);
103108
futureCallback =
104109
new TraceContinuedFutureCallback<>(parentScope, clientSpan, context, futureCallback);

dd-java-agent/instrumentation/apache-httpclient-5/src/main/java/datadog/trace/instrumentation/apachehttpclient5/TraceContinuedFutureCallback.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TraceContinuedFutureCallback(
3535

3636
@Override
3737
public void completed(final T result) {
38-
DECORATE.onResponse(clientSpan, getResponseFromHttpContext());
38+
DECORATE.onResponse(clientSpan, extractHttpResponse(result));
3939
DECORATE.beforeFinish(clientSpan);
4040
clientSpan.finish(); // Finish span before calling delegate
4141

@@ -51,7 +51,7 @@ public void completed(final T result) {
5151

5252
@Override
5353
public void failed(final Exception ex) {
54-
DECORATE.onResponse(clientSpan, getResponseFromHttpContext());
54+
DECORATE.onResponse(clientSpan, extractHttpResponse(null));
5555
DECORATE.onError(clientSpan, ex);
5656
DECORATE.beforeFinish(clientSpan);
5757
clientSpan.finish(); // Finish span before calling delegate
@@ -68,7 +68,7 @@ public void failed(final Exception ex) {
6868

6969
@Override
7070
public void cancelled() {
71-
DECORATE.onResponse(clientSpan, getResponseFromHttpContext());
71+
DECORATE.onResponse(clientSpan, extractHttpResponse(null));
7272
DECORATE.beforeFinish(clientSpan);
7373
clientSpan.finish(); // Finish span before calling delegate
7474

@@ -101,7 +101,16 @@ private void cancelDelegate() {
101101
}
102102

103103
@Nullable
104-
private HttpResponse getResponseFromHttpContext() {
105-
return (HttpResponse) context.getAttribute(HttpCoreContext.HTTP_RESPONSE);
104+
private HttpResponse extractHttpResponse(Object futureResult) {
105+
if (context != null) {
106+
Object fromContext = context.getAttribute(HttpCoreContext.HTTP_RESPONSE);
107+
if (fromContext instanceof HttpResponse) {
108+
return (HttpResponse) fromContext;
109+
}
110+
}
111+
if (futureResult instanceof HttpResponse) {
112+
return (HttpResponse) futureResult;
113+
}
114+
return null;
106115
}
107116
}

dd-java-agent/instrumentation/apache-httpclient-5/src/test/groovy/ApacheHttpAsyncClient5Test.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class ApacheHttpAsyncClient5Test<T extends HttpRequest> extends HttpCli
5757
}
5858
}
5959

60-
class ApacheHttpAsyncClient5NamingV0ForkedTest extends ApacheHttpAsyncClient5Test implements TestingGenericHttpNamingConventions.ClientV0 {
60+
class ApacheHttpAsyncClient5NamingV0Test extends ApacheHttpAsyncClient5Test implements TestingGenericHttpNamingConventions.ClientV0 {
6161
}
6262

6363
class ApacheHttpAsyncClient5NamingV1ForkedTest extends ApacheHttpAsyncClient5Test implements TestingGenericHttpNamingConventions.ClientV1 {

dd-java-agent/instrumentation/apache-httpclient-5/src/test/groovy/ApacheHttpClientTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ abstract class ApacheClientResponseHandlerAll extends ApacheHttpClientTest<Class
208208
}
209209

210210
@Timeout(5)
211-
class ApacheClientResponseHandlerAllV0ForkedTest extends ApacheClientResponseHandlerAll {
211+
class ApacheClientResponseHandlerAllV0Test extends ApacheClientResponseHandlerAll {
212212
}
213213

214214
@Timeout(5)

0 commit comments

Comments
 (0)