1818
1919package org .apache .skywalking .apm .plugin .redisson .v3 ;
2020
21- import io .netty .buffer .ByteBuf ;
2221import io .netty .channel .Channel ;
2322import org .apache .skywalking .apm .agent .core .context .ContextManager ;
2423import org .apache .skywalking .apm .agent .core .context .tag .Tags ;
3231import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .MethodInterceptResult ;
3332import org .apache .skywalking .apm .network .trace .component .ComponentsDefine ;
3433import org .apache .skywalking .apm .plugin .redisson .v3 .util .ClassUtil ;
34+ import org .apache .skywalking .apm .util .StringUtil ;
3535import org .redisson .client .RedisClient ;
3636import org .redisson .client .RedisConnection ;
3737import org .redisson .client .protocol .CommandData ;
3838import org .redisson .client .protocol .CommandsData ;
3939
4040import java .lang .reflect .Method ;
4141import java .net .InetSocketAddress ;
42+ import java .util .Optional ;
4243
4344public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundInterceptor , InstanceConstructorInterceptor {
4445
@@ -58,43 +59,29 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5859 InetSocketAddress remoteAddress = (InetSocketAddress ) channel .remoteAddress ();
5960 String dbInstance = remoteAddress .getAddress ().getHostAddress () + ":" + remoteAddress .getPort ();
6061
61- StringBuilder dbStatement = new StringBuilder ();
6262 String operationName = "Redisson/" ;
63+ String command = "" ;
64+ Object [] arguments = new Object [0 ];
6365
6466 if (allArguments [0 ] instanceof CommandsData ) {
6567 operationName = operationName + "BATCH_EXECUTE" ;
66- CommandsData commands = (CommandsData ) allArguments [0 ];
67- for (CommandData commandData : commands .getCommands ()) {
68- addCommandData (dbStatement , commandData );
69- dbStatement .append (";" );
70- }
68+ command = "BATCH_EXECUTE" ;
7169 } else if (allArguments [0 ] instanceof CommandData ) {
7270 CommandData commandData = (CommandData ) allArguments [0 ];
73- String command = commandData .getCommand ().getName ();
71+ command = commandData .getCommand ().getName ();
7472 operationName = operationName + command ;
75- addCommandData ( dbStatement , commandData );
73+ arguments = commandData . getParams ( );
7674 }
7775
7876 AbstractSpan span = ContextManager .createExitSpan (operationName , peer );
7977 span .setComponent (ComponentsDefine .REDISSON );
80- Tags .DB_TYPE .set (span , "Redis" );
81- Tags .DB_INSTANCE .set (span , dbInstance );
82- Tags .DB_STATEMENT .set (span , dbStatement .toString ());
83- SpanLayer .asCache (span );
84- }
78+ Tags .CACHE_TYPE .set (span , "Redis" );
79+ Tags .CACHE_INSTANCE .set (span , dbInstance );
80+ Tags .CACHE_CMD .set (span , command );
8581
86- private void addCommandData (StringBuilder dbStatement , CommandData commandData ) {
87- dbStatement .append (commandData .getCommand ().getName ());
88- if (RedissonPluginConfig .Plugin .Redisson .TRACE_REDIS_PARAMETERS && commandData .getParams () != null ) {
89- for (Object param : commandData .getParams ()) {
90- dbStatement .append (DELIMITER_SPACE );
91- String paramStr = param instanceof ByteBuf ? QUESTION_MARK : String .valueOf (param .toString ());
92- if (paramStr .length () > RedissonPluginConfig .Plugin .Redisson .REDIS_PARAMETER_MAX_LENGTH ) {
93- paramStr = paramStr .substring (0 , RedissonPluginConfig .Plugin .Redisson .REDIS_PARAMETER_MAX_LENGTH ) + ABBR ;
94- }
95- dbStatement .append (paramStr );
96- }
97- }
82+ getKey (arguments ).ifPresent (key -> Tags .CACHE_KEY .set (span , key ));
83+ parseOperation (command .toLowerCase ()).ifPresent (op -> Tags .CACHE_OP .set (span , op ));
84+ SpanLayer .asCache (span );
9885 }
9986
10087 @ Override
@@ -131,4 +118,29 @@ public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
131118 }
132119 objInst .setSkyWalkingDynamicField (peer );
133120 }
121+
122+ private Optional <String > getKey (Object [] allArguments ) {
123+ if (!RedissonPluginConfig .Plugin .Redisson .TRACE_REDIS_PARAMETERS ) {
124+ return Optional .empty ();
125+ }
126+ if (allArguments .length == 0 ) {
127+ return Optional .empty ();
128+ }
129+ Object argument = allArguments [0 ];
130+ // include null
131+ if (!(argument instanceof String )) {
132+ return Optional .empty ();
133+ }
134+ return Optional .of (StringUtil .cut ((String ) argument , RedissonPluginConfig .Plugin .Redisson .REDIS_PARAMETER_MAX_LENGTH ));
135+ }
136+
137+ private Optional <String > parseOperation (String cmd ) {
138+ if (RedissonPluginConfig .Plugin .Redisson .OPERATION_MAPPING_READ .contains (cmd )) {
139+ return Optional .of ("read" );
140+ }
141+ if (RedissonPluginConfig .Plugin .Redisson .OPERATION_MAPPING_WRITE .contains (cmd )) {
142+ return Optional .of ("write" );
143+ }
144+ return Optional .empty ();
145+ }
134146}
0 commit comments