1919import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
2020import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
2121import io .opentelemetry .javaagent .instrumentation .jedis .JedisRequestContext ;
22+ import javax .annotation .Nullable ;
2223import net .bytebuddy .asm .Advice ;
2324import net .bytebuddy .description .type .TypeDescription ;
2425import net .bytebuddy .matcher .ElementMatcher ;
@@ -45,36 +46,51 @@ public void transform(TypeTransformer transformer) {
4546 @ SuppressWarnings ("unused" )
4647 public static class SendCommandAdvice {
4748
49+ public static class AdviceScope {
50+ private final Context context ;
51+ private final Scope scope ;
52+ private final JedisRequest request ;
53+
54+ private AdviceScope (Context context , Scope scope , JedisRequest request ) {
55+ this .context = context ;
56+ this .scope = scope ;
57+ this .request = request ;
58+ }
59+
60+ @ Nullable
61+ public static AdviceScope start (
62+ Connection connection , ProtocolCommand command , byte [][] args ) {
63+ Context parentContext = currentContext ();
64+ JedisRequest request = JedisRequest .create (connection , command , asList (args ));
65+ if (!instrumenter ().shouldStart (parentContext , request )) {
66+ return null ;
67+ }
68+ Context context = instrumenter ().start (parentContext , request );
69+ return new AdviceScope (context , context .makeCurrent (), request );
70+ }
71+
72+ public void end (@ Nullable Throwable throwable ) {
73+ scope .close ();
74+ JedisRequestContext .endIfNotAttached (instrumenter (), context , request , throwable );
75+ }
76+ }
77+
78+ @ Nullable
4879 @ Advice .OnMethodEnter (suppress = Throwable .class )
49- public static void onEnter (
80+ public static AdviceScope onEnter (
5081 @ Advice .This Connection connection ,
5182 @ Advice .Argument (0 ) ProtocolCommand command ,
52- @ Advice .Argument (1 ) byte [][] args ,
53- @ Advice .Local ("otelJedisRequest" ) JedisRequest request ,
54- @ Advice .Local ("otelContext" ) Context context ,
55- @ Advice .Local ("otelScope" ) Scope scope ) {
56- Context parentContext = currentContext ();
57- request = JedisRequest .create (connection , command , asList (args ));
58- if (!instrumenter ().shouldStart (parentContext , request )) {
59- return ;
60- }
61-
62- context = instrumenter ().start (parentContext , request );
63- scope = context .makeCurrent ();
83+ @ Advice .Argument (1 ) byte [][] args ) {
84+ return AdviceScope .start (connection , command , args );
6485 }
6586
6687 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
6788 public static void stopSpan (
68- @ Advice .Thrown Throwable throwable ,
69- @ Advice .Local ("otelJedisRequest" ) JedisRequest request ,
70- @ Advice .Local ("otelContext" ) Context context ,
71- @ Advice .Local ("otelScope" ) Scope scope ) {
72- if (scope == null ) {
73- return ;
89+ @ Advice .Thrown @ Nullable Throwable throwable ,
90+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
91+ if (adviceScope != null ) {
92+ adviceScope .end (throwable );
7493 }
75-
76- scope .close ();
77- JedisRequestContext .endIfNotAttached (instrumenter (), context , request , throwable );
7894 }
7995 }
8096}
0 commit comments