Skip to content

Commit

Permalink
Lettuce instrumentation - optimization to avoid extra toString() (#8984)
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk committed Jul 20, 2023
1 parent 7379810 commit 600c587
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {}
}

0 comments on commit 600c587

Please sign in to comment.