Skip to content

Commit 8deaaae

Browse files
authored
Avoid duplicate instrumentation in jdk http client sendAsyncMethod (#8127)
There are 2 `sendAsync` methods https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html#sendAsync(java.net.http.HttpRequest,java.net.http.HttpResponse.BodyHandler) and one of them calls the other.
1 parent b33cadd commit 8deaaae

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpClientInstrumentation.java

+13
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import io.opentelemetry.context.Context;
2121
import io.opentelemetry.context.Scope;
22+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
2223
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2324
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
25+
import java.net.http.HttpClient;
2426
import java.net.http.HttpRequest;
2527
import java.net.http.HttpResponse;
2628
import java.util.concurrent.CompletableFuture;
@@ -102,9 +104,15 @@ public static class SendAsyncAdvice {
102104
public static void methodEnter(
103105
@Advice.Argument(value = 0) HttpRequest httpRequest,
104106
@Advice.Argument(value = 1, readOnly = false) HttpResponse.BodyHandler<?> bodyHandler,
107+
@Advice.Local("otelCallDepth") CallDepth callDepth,
105108
@Advice.Local("otelContext") Context context,
106109
@Advice.Local("otelParentContext") Context parentContext,
107110
@Advice.Local("otelScope") Scope scope) {
111+
callDepth = CallDepth.forClass(HttpClient.class);
112+
if (callDepth.getAndIncrement() > 0) {
113+
return;
114+
}
115+
108116
parentContext = currentContext();
109117
if (bodyHandler != null) {
110118
bodyHandler = new BodyHandlerWrapper<>(bodyHandler, parentContext);
@@ -122,9 +130,14 @@ public static void methodExit(
122130
@Advice.Argument(value = 0) HttpRequest httpRequest,
123131
@Advice.Return(readOnly = false) CompletableFuture<HttpResponse<?>> future,
124132
@Advice.Thrown Throwable throwable,
133+
@Advice.Local("otelCallDepth") CallDepth callDepth,
125134
@Advice.Local("otelContext") Context context,
126135
@Advice.Local("otelParentContext") Context parentContext,
127136
@Advice.Local("otelScope") Scope scope) {
137+
if (callDepth.decrementAndGet() > 0) {
138+
return;
139+
}
140+
128141
if (scope == null) {
129142
return;
130143
}

0 commit comments

Comments
 (0)