7
7
8
8
import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
9
9
import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .implementsInterface ;
10
- import static io .opentelemetry .javaagent .instrumentation .clickhouse .ClickHouseSingletons .instrumenter ;
11
10
import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
12
11
import static net .bytebuddy .matcher .ElementMatchers .named ;
13
12
import static net .bytebuddy .matcher .ElementMatchers .namedOneOf ;
17
16
import com .clickhouse .client .ClickHouseRequest ;
18
17
import com .clickhouse .client .config .ClickHouseDefaults ;
19
18
import io .opentelemetry .context .Context ;
20
- import io .opentelemetry .context .Scope ;
21
19
import io .opentelemetry .javaagent .bootstrap .CallDepth ;
22
20
import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
23
21
import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
@@ -43,19 +41,12 @@ public void transform(TypeTransformer transformer) {
43
41
@ SuppressWarnings ("unused" )
44
42
public static class ClickHouseExecuteAndWaitAdvice {
45
43
@ 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 ) {
51
46
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 ;
59
50
}
60
51
61
52
Context parentContext = currentContext ();
@@ -70,32 +61,19 @@ public static void onEnter(
70
61
.orElse (ClickHouseDefaults .DATABASE .getDefaultValue ().toString ()),
71
62
clickHouseRequest .getPreparedQuery ().getOriginalQuery ());
72
63
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 );
79
65
}
80
66
81
67
@ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
82
68
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 ) {
92
70
93
- if (scope == null ) {
71
+ CallDepth callDepth = CallDepth .forClass (ClickHouseClient .class );
72
+ if (callDepth .decrementAndGet () > 0 || scope == null ) {
94
73
return ;
95
74
}
96
75
97
- scope .close ();
98
- instrumenter ().end (context , clickHouseRequest , null , throwable );
76
+ scope .end (throwable );
99
77
}
100
78
}
101
79
}
0 commit comments