Lettuce instrumentation - optimization to avoid extra toString() (#8984)

This commit is contained in:
jason plumb 2023-07-20 08:06:42 -07:00 committed by GitHub
parent 7379810603
commit 600c5873dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 8 deletions

View File

@ -5,6 +5,7 @@
package io.lettuce.core.protocol;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.protocol.CommandArgs.KeyArgument;
import io.lettuce.core.protocol.CommandArgs.SingularArgument;
import io.lettuce.core.protocol.CommandArgs.ValueArgument;
@ -22,19 +23,27 @@ public final class OtelCommandArgsUtil {
*/
public static List<String> getCommandArgs(CommandArgs<?, ?> commandArgs) {
List<String> result = new ArrayList<>();
StringCodec stringCodec = new StringCodec();
for (SingularArgument argument : commandArgs.singularArguments) {
String value = argument.toString();
if (argument instanceof KeyArgument && value.startsWith("key<") && value.endsWith(">")) {
value = value.substring("key<".length(), value.length() - 1);
} else if (argument instanceof ValueArgument
&& value.startsWith("value<")
&& value.endsWith(">")) {
value = value.substring("value<".length(), value.length() - 1);
}
String value = getArgValue(stringCodec, argument);
result.add(value);
}
return result;
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static String getArgValue(StringCodec stringCodec, SingularArgument argument) {
if (argument instanceof KeyArgument) {
KeyArgument keyArg = (KeyArgument) argument;
return stringCodec.decodeValue(keyArg.codec.encodeValue(keyArg.key));
}
if (argument instanceof ValueArgument) {
ValueArgument valueArg = (ValueArgument) argument;
return stringCodec.decodeValue(valueArg.codec.encodeValue(valueArg.val));
}
return argument.toString();
}
private OtelCommandArgsUtil() {}
}