-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rocketmq 5: set context for async callback (#7238)
Run callbacks added to the `CompletableFuture` returned from `sendAsync` with the context that was used when `sendAsync` was called. Add test for capturing message headers.
- Loading branch information
Showing
3 changed files
with
347 additions
and
107 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...opentelemetry/javaagent/instrumentation/rocketmqclient/v5_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,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_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) { | ||
CompletableFuture<T> result = new CompletableFuture<>(); | ||
Context context = Context.current(); | ||
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
Oops, something went wrong.