2121import io .vertx .redis .client .impl .RedisStandaloneConnection ;
2222import io .vertx .redis .client .impl .RedisURI ;
2323import io .vertx .redis .client .impl .RequestUtil ;
24+ import javax .annotation .Nullable ;
2425import net .bytebuddy .asm .Advice ;
26+ import net .bytebuddy .asm .Advice .AssignReturned ;
2527import net .bytebuddy .description .type .TypeDescription ;
2628import net .bytebuddy .matcher .ElementMatcher ;
2729
@@ -40,54 +42,80 @@ public void transform(TypeTransformer transformer) {
4042
4143 @ SuppressWarnings ("unused" )
4244 public static class SendAdvice {
43- @ Advice .OnMethodEnter (suppress = Throwable .class )
44- public static void onEnter (
45- @ Advice .This RedisStandaloneConnection connection ,
46- @ Advice .Argument (0 ) Request request ,
47- @ Advice .FieldValue ("netSocket" ) NetSocket netSocket ,
48- @ Advice .Local ("otelRequest" ) VertxRedisClientRequest otelRequest ,
49- @ Advice .Local ("otelContext" ) Context context ,
50- @ Advice .Local ("otelScope" ) Scope scope ) {
51- if (request == null ) {
52- return ;
53- }
45+ public static class AdviceScope {
46+ public VertxRedisClientRequest otelRequest ;
47+ public Context context ;
48+ public Scope scope ;
49+
50+ @ Nullable
51+ public static AdviceScope start (
52+ RedisStandaloneConnection connection , @ Nullable Request request , NetSocket netSocket ) {
5453
55- String commandName = VertxRedisClientSingletons .getCommandName (request .command ());
56- RedisURI redisUri = VertxRedisClientSingletons .getRedisUri (connection );
57- if (commandName == null || redisUri == null ) {
58- return ;
54+ if (request == null ) {
55+ return null ;
56+ }
57+
58+ String commandName = VertxRedisClientSingletons .getCommandName (request .command ());
59+ RedisURI redisUri = VertxRedisClientSingletons .getRedisUri (connection );
60+ if (commandName == null || redisUri == null ) {
61+ return null ;
62+ }
63+
64+ VertxRedisClientRequest otelRequest =
65+ new VertxRedisClientRequest (
66+ commandName , RequestUtil .getArgs (request ), redisUri , netSocket );
67+ Context parentContext = currentContext ();
68+ if (!instrumenter ().shouldStart (parentContext , otelRequest )) {
69+ return null ;
70+ }
71+ AdviceScope locals = new AdviceScope ();
72+ locals .otelRequest = otelRequest ;
73+ locals .context = instrumenter ().start (parentContext , locals .otelRequest );
74+ locals .scope = locals .context .makeCurrent ();
75+ return locals ;
5976 }
6077
61- otelRequest =
62- new VertxRedisClientRequest (
63- commandName , RequestUtil .getArgs (request ), redisUri , netSocket );
64- Context parentContext = currentContext ();
65- if (!instrumenter ().shouldStart (parentContext , otelRequest )) {
66- return ;
78+ @ Nullable
79+ public Future <Response > end (
80+ @ Nullable Future <Response > responseFuture , @ Nullable Throwable throwable ) {
81+ if (scope == null ) {
82+ return responseFuture ;
83+ }
84+
85+ scope .close ();
86+ if (throwable != null ) {
87+ instrumenter ().end (context , otelRequest , null , throwable );
88+ } else {
89+ responseFuture =
90+ VertxRedisClientSingletons .wrapEndSpan (responseFuture , context , otelRequest );
91+ }
92+ return responseFuture ;
6793 }
94+ }
95+
96+ @ Nullable
97+ @ Advice .OnMethodEnter (suppress = Throwable .class )
98+ public static AdviceScope onEnter (
99+ @ Advice .This RedisStandaloneConnection connection ,
100+ @ Advice .Argument (0 ) @ Nullable Request request ,
101+ @ Advice .FieldValue ("netSocket" ) NetSocket netSocket ) {
68102
69- context = instrumenter ().start (parentContext , otelRequest );
70- scope = context .makeCurrent ();
103+ return AdviceScope .start (connection , request , netSocket );
71104 }
72105
106+ @ Nullable
107+ @ AssignReturned .ToReturned
73108 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
74- public static void onExit (
109+ public static Future < Response > onExit (
75110 @ Advice .Thrown Throwable throwable ,
76- @ Advice .Return (readOnly = false ) Future <Response > responseFuture ,
77- @ Advice .Local ("otelRequest" ) VertxRedisClientRequest otelRequest ,
78- @ Advice .Local ("otelContext" ) Context context ,
79- @ Advice .Local ("otelScope" ) Scope scope ) {
80- if (scope == null ) {
81- return ;
82- }
111+ @ Advice .Return @ Nullable Future <Response > responseFuture ,
112+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
83113
84- scope .close ();
85- if (throwable != null ) {
86- instrumenter ().end (context , otelRequest , null , throwable );
87- } else {
88- responseFuture =
89- VertxRedisClientSingletons .wrapEndSpan (responseFuture , context , otelRequest );
114+ if (adviceScope != null ) {
115+ return adviceScope .end (responseFuture , throwable );
90116 }
117+
118+ return responseFuture ;
91119 }
92120 }
93121
0 commit comments