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

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

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Comment on lines +42 to +43
Copy link
Contributor Author

@breedx-splk breedx-splk Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken from the existing source of ValueArgument.toString(). One note: the codec is being reused here to avoid allocations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially implemented it similarly to what you have done but chose to use substring because of simplicity and reduced dependency on lettuce internals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed that this feels a tad more fragile. Hopefully it's a fair trade off for performance tho.

}
return argument.toString();
}

private OtelCommandArgsUtil() {}
}
Loading