Skip to content

Commit 600c587

Browse files
authored
Lettuce instrumentation - optimization to avoid extra toString() (open-telemetry#8984)
1 parent 7379810 commit 600c587

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/lettuce/core/protocol/OtelCommandArgsUtil.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.lettuce.core.protocol;
77

8+
import io.lettuce.core.codec.StringCodec;
89
import io.lettuce.core.protocol.CommandArgs.KeyArgument;
910
import io.lettuce.core.protocol.CommandArgs.SingularArgument;
1011
import io.lettuce.core.protocol.CommandArgs.ValueArgument;
@@ -22,19 +23,27 @@ public final class OtelCommandArgsUtil {
2223
*/
2324
public static List<String> getCommandArgs(CommandArgs<?, ?> commandArgs) {
2425
List<String> result = new ArrayList<>();
26+
StringCodec stringCodec = new StringCodec();
27+
2528
for (SingularArgument argument : commandArgs.singularArguments) {
26-
String value = argument.toString();
27-
if (argument instanceof KeyArgument && value.startsWith("key<") && value.endsWith(">")) {
28-
value = value.substring("key<".length(), value.length() - 1);
29-
} else if (argument instanceof ValueArgument
30-
&& value.startsWith("value<")
31-
&& value.endsWith(">")) {
32-
value = value.substring("value<".length(), value.length() - 1);
33-
}
29+
String value = getArgValue(stringCodec, argument);
3430
result.add(value);
3531
}
3632
return result;
3733
}
3834

35+
@SuppressWarnings({"rawtypes", "unchecked"})
36+
private static String getArgValue(StringCodec stringCodec, SingularArgument argument) {
37+
if (argument instanceof KeyArgument) {
38+
KeyArgument keyArg = (KeyArgument) argument;
39+
return stringCodec.decodeValue(keyArg.codec.encodeValue(keyArg.key));
40+
}
41+
if (argument instanceof ValueArgument) {
42+
ValueArgument valueArg = (ValueArgument) argument;
43+
return stringCodec.decodeValue(valueArg.codec.encodeValue(valueArg.val));
44+
}
45+
return argument.toString();
46+
}
47+
3948
private OtelCommandArgsUtil() {}
4049
}

0 commit comments

Comments
 (0)