-
Notifications
You must be signed in to change notification settings - Fork 872
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 jdk http client callback (#3719)
* Propage context into jdk http client callback * address review comment * remove null check
- Loading branch information
Showing
3 changed files
with
39 additions
and
7 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
.../java/io/opentelemetry/javaagent/instrumentation/httpclient/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,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.httpclient; | ||
|
||
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) { | ||
if (context == Context.root()) { | ||
return future; | ||
} | ||
|
||
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; | ||
} | ||
} |
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