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.
This commit is contained in:
Lauri Tulmin 2023-03-25 13:08:40 +02:00 committed by GitHub
parent b33cadd791
commit 8deaaaedbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -19,8 +19,10 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
@ -102,9 +104,15 @@ public class HttpClientInstrumentation implements TypeInstrumentation {
public static void methodEnter(
@Advice.Argument(value = 0) HttpRequest httpRequest,
@Advice.Argument(value = 1, readOnly = false) HttpResponse.BodyHandler<?> bodyHandler,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelParentContext") Context parentContext,
@Advice.Local("otelScope") Scope scope) {
callDepth = CallDepth.forClass(HttpClient.class);
if (callDepth.getAndIncrement() > 0) {
return;
}
parentContext = currentContext();
if (bodyHandler != null) {
bodyHandler = new BodyHandlerWrapper<>(bodyHandler, parentContext);
@ -122,9 +130,14 @@ public class HttpClientInstrumentation implements TypeInstrumentation {
@Advice.Argument(value = 0) HttpRequest httpRequest,
@Advice.Return(readOnly = false) CompletableFuture<HttpResponse<?>> future,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelParentContext") Context parentContext,
@Advice.Local("otelScope") Scope scope) {
if (callDepth.decrementAndGet() > 0) {
return;
}
if (scope == null) {
return;
}