2222import net .bytebuddy .description .type .TypeDescription ;
2323import net .bytebuddy .matcher .ElementMatcher ;
2424import org .redisson .client .RedisConnection ;
25+ import javax .annotation .Nullable ;
2526
2627public class RedisConnectionInstrumentation implements TypeInstrumentation {
2728 @ Override
@@ -38,47 +39,64 @@ public void transform(TypeTransformer transformer) {
3839 @ SuppressWarnings ("unused" )
3940 public static class SendAdvice {
4041
41- @ Advice .OnMethodEnter (suppress = Throwable .class )
42- public static void onEnter (
43- @ Advice .This RedisConnection connection ,
44- @ Advice .Argument (0 ) Object arg ,
45- @ Advice .Local ("otelRequest" ) RedissonRequest request ,
46- @ Advice .Local ("otelContext" ) Context context ,
47- @ Advice .Local ("otelScope" ) Scope scope ) {
42+ public static class AdviceScope {
43+ private final RedissonRequest request ;
44+ private final Context context ;
45+ private final Scope scope ;
4846
49- Context parentContext = currentContext ();
50- InetSocketAddress remoteAddress = (InetSocketAddress ) connection .getChannel ().remoteAddress ();
51- request = RedissonRequest .create (remoteAddress , arg );
52- PromiseWrapper <?> promise = request .getPromiseWrapper ();
53- if (promise == null ) {
54- return ;
47+ public AdviceScope (RedissonRequest request , Context context , Scope scope ) {
48+ this .request = request ;
49+ this .context = context ;
50+ this .scope = scope ;
5551 }
56- if (!instrumenter ().shouldStart (parentContext , request )) {
57- return ;
52+
53+ @ Nullable
54+ public static AdviceScope start (RedisConnection connection , Object arg ) {
55+ Context parentContext = currentContext ();
56+ InetSocketAddress remoteAddress = (InetSocketAddress ) connection .getChannel ()
57+ .remoteAddress ();
58+ RedissonRequest request = RedissonRequest .create (remoteAddress , arg );
59+ PromiseWrapper <?> promise = request .getPromiseWrapper ();
60+ if (promise == null ) {
61+ return null ;
62+ }
63+ if (!instrumenter ().shouldStart (parentContext , request )) {
64+ return null ;
65+ }
66+
67+ Context context = instrumenter ().start (parentContext , request );
68+ Scope scope = context .makeCurrent ();
69+
70+ promise .setEndOperationListener (
71+ new EndOperationListener <>(instrumenter (), context , request ));
72+ return new AdviceScope (request , context , scope );
5873 }
5974
60- context = instrumenter ().start (parentContext , request );
61- scope = context .makeCurrent ();
75+ public void end (@ Nullable Throwable throwable ) {
76+ scope .close ();
77+ if (throwable != null ) {
78+ instrumenter ().end (context , request , null , throwable );
79+ }
80+ // span ended in EndOperationListener
81+ }
82+ }
83+
84+ @ Nullable
85+ @ Advice .OnMethodEnter (suppress = Throwable .class )
86+ public static AdviceScope onEnter (
87+ @ Advice .This RedisConnection connection ,
88+ @ Advice .Argument (0 ) Object arg ) {
89+ return AdviceScope .start (connection , arg );
6290
63- promise .setEndOperationListener (new EndOperationListener <>(instrumenter (), context , request ));
6491 }
6592
6693 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
6794 public static void stopSpan (
68- @ Advice .Thrown Throwable throwable ,
69- @ Advice .Local ("otelRequest" ) RedissonRequest request ,
70- @ Advice .Local ("otelContext" ) Context context ,
71- @ Advice .Local ("otelScope" ) Scope scope ) {
72-
73- if (scope == null ) {
74- return ;
75- }
76- scope .close ();
77-
78- if (throwable != null ) {
79- instrumenter ().end (context , request , null , throwable );
95+ @ Advice .Thrown @ Nullable Throwable throwable ,
96+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
97+ if (adviceScope != null ) {
98+ adviceScope .end (throwable );
8099 }
81- // span ended in EndOperationListener
82100 }
83101 }
84102}
0 commit comments