77
88import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
99import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .implementsInterface ;
10- import static io .opentelemetry .javaagent .instrumentation .clickhouse .ClickHouseSingletons .instrumenter ;
1110import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
1211import static net .bytebuddy .matcher .ElementMatchers .named ;
1312import static net .bytebuddy .matcher .ElementMatchers .namedOneOf ;
1716import com .clickhouse .client .ClickHouseRequest ;
1817import com .clickhouse .client .config .ClickHouseDefaults ;
1918import io .opentelemetry .context .Context ;
20- import io .opentelemetry .context .Scope ;
2119import io .opentelemetry .javaagent .bootstrap .CallDepth ;
2220import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
2321import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
@@ -43,19 +41,12 @@ public void transform(TypeTransformer transformer) {
4341 @ SuppressWarnings ("unused" )
4442 public static class ClickHouseExecuteAndWaitAdvice {
4543 @ Advice .OnMethodEnter (suppress = Throwable .class )
46- public static void onEnter (
47- @ Advice .Argument (0 ) ClickHouseRequest <?> clickHouseRequest ,
48- @ Advice .Local ("otelContext" ) Context context ,
49- @ Advice .Local ("otelScope" ) Scope scope ,
50- @ Advice .Local ("otelCallDepth" ) CallDepth callDepth ) {
44+ public static ClickHouseScope onEnter (
45+ @ Advice .Argument (0 ) ClickHouseRequest <?> clickHouseRequest ) {
5146
52- callDepth = CallDepth .forClass (ClickHouseClient .class );
53- if (callDepth .getAndIncrement () > 0 ) {
54- return ;
55- }
56-
57- if (clickHouseRequest == null ) {
58- return ;
47+ CallDepth callDepth = CallDepth .forClass (ClickHouseClient .class );
48+ if (callDepth .getAndIncrement () > 0 || clickHouseRequest == null ) {
49+ return null ;
5950 }
6051
6152 Context parentContext = currentContext ();
@@ -70,32 +61,19 @@ public static void onEnter(
7061 .orElse (ClickHouseDefaults .DATABASE .getDefaultValue ().toString ()),
7162 clickHouseRequest .getPreparedQuery ().getOriginalQuery ());
7263
73- if (!instrumenter ().shouldStart (parentContext , request )) {
74- return ;
75- }
76-
77- context = instrumenter ().start (parentContext , request );
78- scope = context .makeCurrent ();
64+ return ClickHouseScope .start (parentContext , request );
7965 }
8066
8167 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
8268 public static void onExit (
83- @ Advice .Thrown Throwable throwable ,
84- @ Advice .Local ("otelRequest" ) ClickHouseDbRequest clickHouseRequest ,
85- @ Advice .Local ("otelContext" ) Context context ,
86- @ Advice .Local ("otelScope" ) Scope scope ,
87- @ Advice .Local ("otelCallDepth" ) CallDepth callDepth ) {
88-
89- if (callDepth .decrementAndGet () > 0 ) {
90- return ;
91- }
69+ @ Advice .Thrown Throwable throwable , @ Advice .Enter ClickHouseScope scope ) {
9270
93- if (scope == null ) {
71+ CallDepth callDepth = CallDepth .forClass (ClickHouseClient .class );
72+ if (callDepth .decrementAndGet () > 0 || scope == null ) {
9473 return ;
9574 }
9675
97- scope .close ();
98- instrumenter ().end (context , clickHouseRequest , null , throwable );
76+ scope .end (throwable );
9977 }
10078 }
10179}
0 commit comments