Skip to content

Commit

Permalink
Actually fix redisson instrumentation after 3.16.8 release (open-tele…
Browse files Browse the repository at this point in the history
…metry#5201)

* Actually fix redisson instrumentation after 3.16.8 release

* try MethodHandles

* remove unneeded virtual fields
  • Loading branch information
Mateusz Rzeszutek authored and RashmiRam committed May 23, 2022
1 parent be96852 commit d467233
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
6 changes: 1 addition & 5 deletions instrumentation/redisson-3.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ muzzle {
pass {
group.set("org.redisson")
module.set("redisson")
// in 3.16.8 CommandsData#getPromise() and CommandData#getPromise() return type was changed in
// a backwards-incompatible way from RPromise to CompletableStage
versions.set("[3.0.0,3.16.8)")
versions.set("[3.0.0,)")
}
}

Expand All @@ -17,8 +15,6 @@ dependencies {

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

latestDepTestLibrary("org.redisson:redisson:3.16.7")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletionStage;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.redisson.client.RedisConnection;
import org.redisson.misc.RPromise;

public class RedisConnectionInstrumentation implements TypeInstrumentation {
@Override
Expand Down Expand Up @@ -66,7 +66,7 @@ public static void stopSpan(
}
scope.close();

RPromise<?> promise = request.getPromise();
CompletionStage<?> promise = request.getPromise();
if (promise == null || throwable != null) {
instrumenter().end(context, request, null, throwable);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import com.google.auto.value.AutoValue;
import io.netty.buffer.ByteBuf;
import io.opentelemetry.instrumentation.api.db.RedisCommandSanitizer;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.redisson.client.protocol.CommandData;
Expand Down Expand Up @@ -98,13 +103,45 @@ private static String normalizeSingleCommand(CommandData<?, ?> command) {
}

@Nullable
public RPromise<?> getPromise() {
public CompletionStage<?> getPromise() {
Object command = getCommand();
if (command instanceof CommandData) {
return ((CommandData<?, ?>) command).getPromise();
} else if (command instanceof CommandsData) {
return ((CommandsData) command).getPromise();
if (command instanceof CommandData && COMMAND_DATA_GET_PROMISE != null) {
try {
return (CompletionStage<?>) COMMAND_DATA_GET_PROMISE.invoke(command);
} catch (Throwable ignored) {
return null;
}
} else if (command instanceof CommandsData && COMMANDS_DATA_GET_PROMISE != null) {
try {
return (CompletionStage<?>) COMMANDS_DATA_GET_PROMISE.invoke(command);
} catch (Throwable ignored) {
return null;
}
}
return null;
}

private static final MethodHandle COMMAND_DATA_GET_PROMISE =
findGetPromiseMethod(CommandData.class);
private static final MethodHandle COMMANDS_DATA_GET_PROMISE =
findGetPromiseMethod(CommandsData.class);

private static MethodHandle findGetPromiseMethod(Class<?> commandClass) {
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
try {
// try versions older than 3.16.8
return lookup.findVirtual(commandClass, "getPromise", MethodType.methodType(RPromise.class));
} catch (NoSuchMethodException e) {
// in 3.16.8 CommandsData#getPromise() and CommandData#getPromise() return type was changed in
// a backwards-incompatible way from RPromise to CompletableFuture
try {
return lookup.findVirtual(
commandClass, "getPromise", MethodType.methodType(CompletableFuture.class));
} catch (NoSuchMethodException | IllegalAccessException ignored) {
return null;
}
} catch (IllegalAccessException ignored) {
return null;
}
}
}

0 comments on commit d467233

Please sign in to comment.