From 600c5873dda1fddd34a18db1d01d2bbf7a084487 Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Thu, 20 Jul 2023 08:06:42 -0700 Subject: [PATCH] Lettuce instrumentation - optimization to avoid extra toString() (#8984) --- .../core/protocol/OtelCommandArgsUtil.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/lettuce/core/protocol/OtelCommandArgsUtil.java b/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/lettuce/core/protocol/OtelCommandArgsUtil.java index ee0b3e3d08..7e04eea0ad 100644 --- a/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/lettuce/core/protocol/OtelCommandArgsUtil.java +++ b/instrumentation/lettuce/lettuce-5.1/library/src/main/java/io/lettuce/core/protocol/OtelCommandArgsUtil.java @@ -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 getCommandArgs(CommandArgs commandArgs) { List 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() {} }