-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate context into async http client CompletableFuture callbacks (#…
- Loading branch information
Showing
7 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...pentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/CompletableFutureWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v2_0; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public final class CompletableFutureWrapper { | ||
|
||
private CompletableFutureWrapper() {} | ||
|
||
public static <T> CompletableFuture<T> wrap(CompletableFuture<T> future, Context context) { | ||
CompletableFuture<T> result = new CompletableFuture<>(); | ||
future.whenComplete( | ||
(T value, Throwable throwable) -> { | ||
try (Scope ignored = context.makeCurrent()) { | ||
if (throwable != null) { | ||
result.completeExceptionally(throwable); | ||
} else { | ||
result.complete(value); | ||
} | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ry/javaagent/instrumentation/asynchttpclient/v2_0/NettyResponseFutureInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v2_0; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.concurrent.CompletableFuture; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class NettyResponseFutureInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.asynchttpclient.netty.NettyResponseFuture"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("toCompletableFuture").and(takesNoArguments()).and(returns(CompletableFuture.class)), | ||
this.getClass().getName() + "$WrapFutureAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class WrapFutureAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit(@Advice.Return(readOnly = false) CompletableFuture<?> result) { | ||
result = CompletableFutureWrapper.wrap(result, Java8BytecodeBridge.currentContext()); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
.../javaagent/instrumentation/asynchttpclient/v2_0/AsyncHttpClientCompletableFutureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v2_0; | ||
|
||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult; | ||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; | ||
import java.net.URI; | ||
import java.util.Map; | ||
import org.asynchttpclient.Request; | ||
|
||
class AsyncHttpClientCompletableFutureTest extends AsyncHttpClientTest { | ||
|
||
@Override | ||
protected void configure(HttpClientTestOptions.Builder optionsBuilder) { | ||
super.configure(optionsBuilder); | ||
|
||
optionsBuilder.setHasSendRequest(false); | ||
} | ||
|
||
@Override | ||
public int sendRequest(Request request, String method, URI uri, Map<String, String> headers) { | ||
throw new IllegalStateException("this test only tests requests with callback"); | ||
} | ||
|
||
@Override | ||
public void sendRequestWithCallback( | ||
Request request, | ||
String method, | ||
URI uri, | ||
Map<String, String> headers, | ||
HttpClientResult requestResult) { | ||
client | ||
.executeRequest(request) | ||
.toCompletableFuture() | ||
.whenComplete( | ||
(response, throwable) -> { | ||
if (throwable == null) { | ||
requestResult.complete(response.getStatusCode()); | ||
} else { | ||
requestResult.complete(throwable); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters