Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually fix redisson instrumentation after 3.16.8 release #5201

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,15 @@
import com.google.auto.value.AutoValue;
import io.netty.buffer.ByteBuf;
import io.opentelemetry.instrumentation.api.db.RedisCommandSanitizer;
import io.opentelemetry.instrumentation.api.field.VirtualField;
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 All @@ -23,6 +29,11 @@
@AutoValue
public abstract class RedissonRequest {

private static final VirtualField<CommandData<?, ?>, CompletionStage<?>> commandDataPromiseField =
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
VirtualField.find(CommandData.class, CompletionStage.class);
private static final VirtualField<CommandsData, CompletionStage<?>> commandsDataPromiseField =
VirtualField.find(CommandsData.class, CompletionStage.class);

public static RedissonRequest create(InetSocketAddress address, Object command) {
return new AutoValue_RedissonRequest(address, command);
}
Expand Down Expand Up @@ -98,13 +109,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;
}
}
}