1313
1414import io .opentelemetry .context .Context ;
1515import io .opentelemetry .context .Scope ;
16- import io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge ;
1716import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1817import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
1918import java .util .Collection ;
19+ import javax .annotation .Nullable ;
2020import net .bytebuddy .asm .Advice ;
2121import net .bytebuddy .description .type .TypeDescription ;
2222import net .bytebuddy .matcher .ElementMatcher ;
@@ -39,36 +39,50 @@ public void transform(TypeTransformer transformer) {
3939 @ SuppressWarnings ("unused" )
4040 public static class SinkTaskPutAdvice {
4141
42- @ Advice .OnMethodEnter (suppress = Throwable .class )
43- public static void onEnter (
44- @ Advice .Argument (0 ) Collection <SinkRecord > records ,
45- @ Advice .Local ("otelTask" ) KafkaConnectTask task ,
46- @ Advice .Local ("otelContext" ) Context context ,
47- @ Advice .Local ("otelScope" ) Scope scope ) {
42+ public static class AdviceScope {
43+ private final KafkaConnectTask task ;
44+ private final Context context ;
45+ private final Scope scope ;
46+
47+ private AdviceScope (KafkaConnectTask task , Context context , Scope scope ) {
48+ this .task = task ;
49+ this .context = context ;
50+ this .scope = scope ;
51+ }
52+
53+ @ Nullable
54+ public static AdviceScope start (Collection <SinkRecord > records ) {
55+ Context parentContext = Context .current ();
4856
49- Context parentContext = Java8BytecodeBridge .currentContext ();
57+ KafkaConnectTask task = new KafkaConnectTask (records );
58+ if (!instrumenter ().shouldStart (parentContext , task )) {
59+ return null ;
60+ }
5061
51- task = new KafkaConnectTask (records );
52- if (!instrumenter ().shouldStart (parentContext , task )) {
53- return ;
62+ Context context = instrumenter ().start (parentContext , task );
63+ return new AdviceScope (task , context , context .makeCurrent ());
5464 }
5565
56- context = instrumenter ().start (parentContext , task );
57- scope = context .makeCurrent ();
66+ public void end (@ Nullable Throwable throwable ) {
67+ scope .close ();
68+ instrumenter ().end (context , task , null , throwable );
69+ }
70+ }
71+
72+ @ Nullable
73+ @ Advice .OnMethodEnter (suppress = Throwable .class )
74+ public static AdviceScope onEnter (@ Advice .Argument (0 ) Collection <SinkRecord > records ) {
75+ return AdviceScope .start (records );
5876 }
5977
6078 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
6179 public static void onExit (
62- @ Advice .Thrown Throwable throwable ,
63- @ Advice .Local ("otelTask" ) KafkaConnectTask task ,
64- @ Advice .Local ("otelContext" ) Context context ,
65- @ Advice .Local ("otelScope" ) Scope scope ) {
80+ @ Advice .Thrown @ Nullable Throwable throwable ,
81+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
6682
67- if (scope = = null ) {
68- return ;
83+ if (adviceScope ! = null ) {
84+ adviceScope . end ( throwable ) ;
6985 }
70- scope .close ();
71- instrumenter ().end (context , task , null , throwable );
7286 }
7387 }
7488}
0 commit comments