55
66package io .opentelemetry .javaagent .instrumentation .mybatis .v3_2 ;
77
8- import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
98import static io .opentelemetry .javaagent .instrumentation .mybatis .v3_2 .MyBatisSingletons .instrumenter ;
109import static net .bytebuddy .matcher .ElementMatchers .named ;
1110
1413import io .opentelemetry .instrumentation .api .incubator .semconv .util .ClassAndMethod ;
1514import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1615import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
16+ import javax .annotation .Nullable ;
1717import net .bytebuddy .asm .Advice ;
1818import net .bytebuddy .description .type .TypeDescription ;
1919import net .bytebuddy .matcher .ElementMatcher ;
@@ -35,36 +35,52 @@ public void transform(TypeTransformer transformer) {
3535 @ SuppressWarnings ("unused" )
3636 public static class ExecuteAdvice {
3737
38- @ Advice .OnMethodEnter (suppress = Throwable .class )
39- public static void getMapperInfo (
40- @ Advice .FieldValue ("command" ) SqlCommand command ,
41- @ Advice .Local ("otelRequest" ) ClassAndMethod request ,
42- @ Advice .Local ("otelContext" ) Context context ,
43- @ Advice .Local ("otelScope" ) Scope scope ) {
44- if (command == null ) {
45- return ;
38+ public static class AdviceScope {
39+ private final ClassAndMethod classAndMethod ;
40+ private final Context context ;
41+ private final Scope scope ;
42+
43+ private AdviceScope (ClassAndMethod classAndMethod , Context context , Scope scope ) {
44+ this .classAndMethod = classAndMethod ;
45+ this .context = context ;
46+ this .scope = scope ;
4647 }
47- request = SqlCommandUtil .getClassAndMethod (command );
48- if (request == null ) {
49- return ;
48+
49+ @ Nullable
50+ public static AdviceScope start (@ Nullable SqlCommand command ) {
51+ if (command == null ) {
52+ return null ;
53+ }
54+ ClassAndMethod classAndMethod = SqlCommandUtil .getClassAndMethod (command );
55+ if (classAndMethod == null ) {
56+ return null ;
57+ }
58+ Context parentContext = Context .current ();
59+ if (!instrumenter ().shouldStart (parentContext , classAndMethod )) {
60+ return null ;
61+ }
62+ Context context = instrumenter ().start (parentContext , classAndMethod );
63+ return new AdviceScope (classAndMethod , context , context .makeCurrent ());
5064 }
51- Context parentContext = currentContext ();
52- if (!instrumenter ().shouldStart (parentContext , request )) {
53- return ;
65+
66+ public void end (@ Nullable Throwable throwable ) {
67+ scope .close ();
68+ instrumenter ().end (context , classAndMethod , null , throwable );
5469 }
55- context = instrumenter ().start (parentContext , request );
56- scope = context .makeCurrent ();
70+ }
71+
72+ @ Nullable
73+ @ Advice .OnMethodEnter (suppress = Throwable .class )
74+ public static AdviceScope getMapperInfo (@ Advice .FieldValue ("command" ) SqlCommand command ) {
75+ return AdviceScope .start (command );
5776 }
5877
5978 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
6079 public static void stopSpan (
61- @ Advice .Thrown Throwable throwable ,
62- @ Advice .Local ("otelRequest" ) ClassAndMethod request ,
63- @ Advice .Local ("otelContext" ) Context context ,
64- @ Advice .Local ("otelScope" ) Scope scope ) {
65- if (scope != null ) {
66- scope .close ();
67- instrumenter ().end (context , request , null , throwable );
80+ @ Advice .Thrown @ Nullable Throwable throwable ,
81+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
82+ if (adviceScope != null ) {
83+ adviceScope .end (throwable );
6884 }
6985 }
7086 }
0 commit comments