1818
1919package org .apache .skywalking .apm .plugin .lettuce .v5 ;
2020
21+ import io .lettuce .core .codec .StringCodec ;
2122import io .lettuce .core .protocol .CommandArgs ;
2223import io .lettuce .core .protocol .DecoratedCommand ;
2324import io .lettuce .core .protocol .RedisCommand ;
3334import org .apache .skywalking .apm .util .StringUtil ;
3435
3536import java .lang .reflect .Method ;
37+ import java .nio .ByteBuffer ;
3638import java .util .Collection ;
3739import java .util .List ;
40+ import java .util .Optional ;
3841
3942@ SuppressWarnings ("unchecked" )
4043public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInterceptor {
4144
4245 private static final String PASSWORD_MASK = "******" ;
4346 private static final String ABBR = "..." ;
44- private static final String DELIMITER_SPACE = " " ;
4547 private static final String AUTH = "AUTH" ;
48+ private static final StringCodec STRING_CODEC = new StringCodec ();
4649
4750 @ Override
4851 public void beforeMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes , MethodInterceptResult result ) {
@@ -63,45 +66,48 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
6366 return ;
6467 }
6568
66- StringBuilder dbStatement = new StringBuilder ();
6769 String operationName = "Lettuce/" ;
70+ String key = Constants .EMPTY_STRING ;
71+ String command = Constants .EMPTY_STRING ;
6872 if (allArguments [0 ] instanceof RedisCommand ) {
6973 RedisCommand <?, ?, ?> redisCommand = (RedisCommand <?, ?, ?>) allArguments [0 ];
70- String command = redisCommand .getType ().name ();
74+ command = redisCommand .getType ().name ();
7175 operationName = operationName + command ;
72- dbStatement .append (command );
7376 if (LettucePluginConfig .Plugin .Lettuce .TRACE_REDIS_PARAMETERS ) {
74- dbStatement . append ( DELIMITER_SPACE ). append ( getArgsStatement ( redisCommand ) );
77+ key = getArgsKey ( redisCommand );
7578 }
7679 } else if (allArguments [0 ] instanceof Collection ) {
77- Collection <RedisCommand <?, ?, ?>> redisCommands = (Collection <RedisCommand <?, ?, ?>>) allArguments [0 ];
7880 operationName = operationName + "BATCH_WRITE" ;
79- for (RedisCommand <?, ?, ?> redisCommand : redisCommands ) {
80- dbStatement .append (redisCommand .getType ().name ()).append (";" );
81- }
81+ command = "BATCH_WRITE" ;
8282 }
8383 AbstractSpan span = ContextManager .createExitSpan (operationName , peer );
8484 span .setComponent (ComponentsDefine .LETTUCE );
85- Tags .DB_TYPE .set (span , "Redis" );
86- Tags .DB_STATEMENT .set (span , dbStatement .toString ());
85+ Tags .CACHE_TYPE .set (span , "Redis" );
86+ if (StringUtil .isNotEmpty (key )) {
87+ Tags .CACHE_KEY .set (span , key );
88+ }
89+ Tags .CACHE_CMD .set (span , command );
90+ parseOperation (command .toLowerCase ()).ifPresent (op -> Tags .CACHE_OP .set (span , op ));
8791 SpanLayer .asCache (span );
8892 span .prepareForAsync ();
8993 ContextManager .stopSpan ();
9094 enhancedCommand .setSkyWalkingDynamicField (span );
9195 }
9296
93- private String getArgsStatement (RedisCommand <?, ?, ?> redisCommand ) {
94- String statement ;
97+ private String getArgsKey (RedisCommand <?, ?, ?> redisCommand ) {
9598 if (AUTH .equalsIgnoreCase (redisCommand .getType ().name ())) {
96- statement = PASSWORD_MASK ;
97- } else {
98- CommandArgs <?, ?> args = redisCommand .getArgs ();
99- statement = (args != null ) ? args .toCommandString () : Constants .EMPTY_STRING ;
99+ return PASSWORD_MASK ;
100100 }
101- if (StringUtil .isNotEmpty (statement ) && statement .length () > LettucePluginConfig .Plugin .Lettuce .REDIS_PARAMETER_MAX_LENGTH ) {
102- statement = statement .substring (0 , LettucePluginConfig .Plugin .Lettuce .REDIS_PARAMETER_MAX_LENGTH ) + ABBR ;
101+ CommandArgs <?, ?> args = redisCommand .getArgs ();
102+ if (args == null ) {
103+ return Constants .EMPTY_STRING ;
104+ }
105+ ByteBuffer firstEncodedKey = args .getFirstEncodedKey ();
106+ String key = STRING_CODEC .decodeKey (firstEncodedKey );
107+ if (StringUtil .isNotEmpty (key ) && key .length () > LettucePluginConfig .Plugin .Lettuce .REDIS_PARAMETER_MAX_LENGTH ) {
108+ key = StringUtil .cut (key , LettucePluginConfig .Plugin .Lettuce .REDIS_PARAMETER_MAX_LENGTH ) + ABBR ;
103109 }
104- return statement ;
110+ return key ;
105111 }
106112
107113 @ Override
@@ -144,4 +150,14 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
144150 }
145151 return command ;
146152 }
153+
154+ private Optional <String > parseOperation (String cmd ) {
155+ if (LettucePluginConfig .Plugin .Lettuce .OPERATION_MAPPING_READ .contains (cmd )) {
156+ return Optional .of ("read" );
157+ }
158+ if (LettucePluginConfig .Plugin .Lettuce .OPERATION_MAPPING_WRITE .contains (cmd )) {
159+ return Optional .of ("write" );
160+ }
161+ return Optional .empty ();
162+ }
147163}
0 commit comments