-
Notifications
You must be signed in to change notification settings - Fork 879
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 redisson async callback (#5313)
* Propagate context into redisson async callback * Apply suggestions from code review Co-authored-by: Anuraag Agrawal <[email protected]> Co-authored-by: Anuraag Agrawal <[email protected]>
- Loading branch information
Showing
9 changed files
with
201 additions
and
24 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...in/java/io/opentelemetry/javaagent/instrumentation/redisson/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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.redisson; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public final class CompletableFutureWrapper<T> extends CompletableFuture<T> | ||
implements PromiseWrapper<T> { | ||
private volatile EndOperationListener<T> endOperationListener; | ||
|
||
private CompletableFutureWrapper(CompletableFuture<T> delegate, Context context) { | ||
this.whenComplete( | ||
(result, error) -> { | ||
EndOperationListener<T> endOperationListener = this.endOperationListener; | ||
if (endOperationListener != null) { | ||
endOperationListener.accept(result, error); | ||
} | ||
try (Scope ignored = context.makeCurrent()) { | ||
if (error != null) { | ||
delegate.completeExceptionally(error); | ||
} else { | ||
delegate.complete(result); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public static <T> CompletableFuture<T> wrap(CompletableFuture<T> delegate) { | ||
if (delegate instanceof CompletableFutureWrapper) { | ||
return delegate; | ||
} | ||
|
||
return new CompletableFutureWrapper<>(delegate, Context.current()); | ||
} | ||
|
||
@Override | ||
public void setEndOperationListener(EndOperationListener<T> endOperationListener) { | ||
this.endOperationListener = endOperationListener; | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...ent/src/main/java/io/opentelemetry/javaagent/instrumentation/redisson/PromiseWrapper.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,11 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.redisson; | ||
|
||
public interface PromiseWrapper<T> { | ||
|
||
void setEndOperationListener(EndOperationListener<T> endOperationListener); | ||
} |
59 changes: 59 additions & 0 deletions
59
.../io/opentelemetry/javaagent/instrumentation/redisson/RedisCommandDataInstrumentation.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,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.redisson; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
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; | ||
import org.redisson.misc.RPromise; | ||
|
||
public class RedisCommandDataInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return namedOneOf( | ||
"org.redisson.client.protocol.CommandData", "org.redisson.client.protocol.CommandsData"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
// before 3.16.8 | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, named("org.redisson.misc.RPromise"))), | ||
this.getClass().getName() + "$WrapPromiseAdvice"); | ||
// since 3.16.8 | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, named("java.util.concurrent.CompletableFuture"))), | ||
this.getClass().getName() + "$WrapCompletableFutureAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class WrapPromiseAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(value = 0, readOnly = false) RPromise<?> promise) { | ||
promise = RedissonPromiseWrapper.wrap(promise); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class WrapCompletableFutureAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0, readOnly = false) CompletableFuture<?> completableFuture) { | ||
completableFuture = CompletableFutureWrapper.wrap(completableFuture); | ||
} | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...main/java/io/opentelemetry/javaagent/instrumentation/redisson/RedissonPromiseWrapper.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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.redisson; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import org.redisson.misc.RPromise; | ||
import org.redisson.misc.RedissonPromise; | ||
|
||
public class RedissonPromiseWrapper<T> extends RedissonPromise<T> implements PromiseWrapper<T> { | ||
private volatile EndOperationListener<T> endOperationListener; | ||
|
||
private RedissonPromiseWrapper(RPromise<T> delegate, Context context) { | ||
this.whenComplete( | ||
(result, error) -> { | ||
EndOperationListener<T> endOperationListener = this.endOperationListener; | ||
if (endOperationListener != null) { | ||
endOperationListener.accept(result, error); | ||
} | ||
try (Scope ignored = context.makeCurrent()) { | ||
if (error != null) { | ||
delegate.tryFailure(error); | ||
} else { | ||
delegate.trySuccess(result); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public static <T> RPromise<T> wrap(RPromise<T> delegate) { | ||
if (delegate instanceof RedissonPromiseWrapper) { | ||
return delegate; | ||
} | ||
|
||
return new RedissonPromiseWrapper<>(delegate, Context.current()); | ||
} | ||
|
||
@Override | ||
public void setEndOperationListener(EndOperationListener<T> endOperationListener) { | ||
this.endOperationListener = endOperationListener; | ||
} | ||
} |
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