Skip to content

Commit

Permalink
Avoid duplicate instrumentation in jdk http client sendAsyncMethod (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Mar 25, 2023
1 parent b33cadd commit 8deaaae
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

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;
Expand Down Expand Up @@ -102,9 +104,15 @@ public static class SendAsyncAdvice {
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);
Expand All @@ -122,9 +130,14 @@ public static void methodExit(
@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;
}
Expand Down

0 comments on commit 8deaaae

Please sign in to comment.