11package datadog .trace .instrumentation .jedis30 ;
22
3+ import static datadog .trace .agent .tooling .bytebuddy .matcher .ClassLoaderMatchers .hasClassNamed ;
34import static datadog .trace .agent .tooling .bytebuddy .matcher .NameMatchers .named ;
45import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpan ;
56import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .startSpan ;
7+ import static datadog .trace .instrumentation .jedis30 .JedisClientDecorator .COMPONENT_NAME ;
68import static datadog .trace .instrumentation .jedis30 .JedisClientDecorator .DECORATE ;
79import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
10+ import static net .bytebuddy .matcher .ElementMatchers .not ;
811import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
912
1013import com .google .auto .service .AutoService ;
1417import datadog .trace .bootstrap .instrumentation .api .AgentScope ;
1518import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
1619import net .bytebuddy .asm .Advice ;
20+ import net .bytebuddy .matcher .ElementMatcher ;
1721import redis .clients .jedis .Connection ;
1822import redis .clients .jedis .Protocol ;
1923import redis .clients .jedis .commands .ProtocolCommand ;
@@ -27,44 +31,51 @@ public JedisInstrumentation() {
2731 }
2832
2933 @ Override
30- public String [] helperClassNames () {
31- return new String [] {
32- packageName + ".JedisClientDecorator" ,
33- } ;
34+ public ElementMatcher . Junction < ClassLoader > classLoaderMatcher () {
35+ // Only match Jedis 3.x which has ProtocolCommand but not CommandObject (4.x+).
36+ return hasClassNamed ( "redis.clients.jedis.commands.ProtocolCommand" )
37+ . and ( not ( hasClassNamed ( "redis.clients.jedis.CommandObject" ))) ;
3438 }
3539
3640 @ Override
3741 public String instrumentedType () {
3842 return "redis.clients.jedis.Connection" ;
3943 }
4044
45+ @ Override
46+ public String [] helperClassNames () {
47+ return new String [] {
48+ packageName + ".JedisClientDecorator" ,
49+ };
50+ }
51+
4152 @ Override
4253 public void methodAdvice (MethodTransformer transformer ) {
4354 transformer .applyAdvice (
4455 isMethod ()
4556 .and (named ("sendCommand" ))
46- .and (takesArgument (0 , named ("redis.clients.jedis.commands.ProtocolCommand" ))),
57+ .and (
58+ takesArgument (
59+ 0 , named ("redis.clients.jedis.commands.ProtocolCommand" ))),
4760 JedisInstrumentation .class .getName () + "$JedisAdvice" );
48- // FIXME: This instrumentation only incorporates sending the command, not processing the result.
4961 }
5062
5163 public static class JedisAdvice {
5264
5365 @ Advice .OnMethodEnter (suppress = Throwable .class )
5466 public static AgentScope onEnter (
55- @ Advice .Argument (0 ) final ProtocolCommand command , @ Advice .This final Connection thiz ) {
67+ @ Advice .Argument (0 ) final ProtocolCommand command ,
68+ @ Advice .This final Connection thiz ) {
5669 if (CallDepthThreadLocalMap .incrementCallDepth (Connection .class ) > 0 ) {
5770 return null ;
5871 }
59- final AgentSpan span = startSpan ("redis-command" , JedisClientDecorator .OPERATION_NAME );
72+ final AgentSpan span =
73+ startSpan (COMPONENT_NAME .toString (), JedisClientDecorator .OPERATION_NAME );
6074 DECORATE .afterStart (span );
6175 DECORATE .onConnection (span , thiz );
62-
6376 if (command instanceof Protocol .Command ) {
6477 DECORATE .onStatement (span , ((Protocol .Command ) command ).name ());
6578 } else {
66- // Protocol.Command is the only implementation in the Jedis lib as of 3.1 but this will save
67- // us if that changes
6879 DECORATE .onStatement (span , new String (command .getRaw ()));
6980 }
7081 return activateSpan (span );
0 commit comments