|
19 | 19 | import io.opentelemetry.javaagent.bootstrap.spring.SpringSchedulingTaskTracing; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 22 | +import javax.annotation.Nullable; |
22 | 23 | import net.bytebuddy.asm.Advice; |
23 | 24 | import net.bytebuddy.description.type.TypeDescription; |
24 | 25 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -77,37 +78,53 @@ public static void onExit(@Advice.Enter boolean previousValue) { |
77 | 78 | @SuppressWarnings("unused") |
78 | 79 | public static class InvokeBatchAdvice { |
79 | 80 |
|
| 81 | + public static class AdviceScope { |
| 82 | + public final KafkaReceiveRequest request; |
| 83 | + public final Context context; |
| 84 | + public final Scope scope; |
| 85 | + |
| 86 | + private AdviceScope(KafkaReceiveRequest request, Context context, Scope scope) { |
| 87 | + this.request = request; |
| 88 | + this.context = context; |
| 89 | + this.scope = scope; |
| 90 | + } |
| 91 | + |
| 92 | + @Nullable |
| 93 | + public static AdviceScope start(ConsumerRecords<?, ?> records, Consumer<?, ?> consumer) { |
| 94 | + KafkaConsumerContext consumerContext = KafkaConsumerContextUtil.get(records); |
| 95 | + Context receiveContext = consumerContext.getContext(); |
| 96 | + |
| 97 | + // use the receive CONSUMER span as parent if it's available |
| 98 | + Context parentContext = receiveContext != null ? receiveContext : Context.current(); |
| 99 | + KafkaReceiveRequest request = KafkaReceiveRequest.create(records, consumer); |
| 100 | + |
| 101 | + if (!batchProcessInstrumenter().shouldStart(parentContext, request)) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + Context context = batchProcessInstrumenter().start(parentContext, request); |
| 105 | + return new AdviceScope(request, context, context.makeCurrent()); |
| 106 | + } |
| 107 | + |
| 108 | + public void close(@Nullable Throwable throwable) { |
| 109 | + scope.close(); |
| 110 | + batchProcessInstrumenter().end(context, request, null, throwable); |
| 111 | + } |
| 112 | + } |
| 113 | + |
80 | 114 | @Advice.OnMethodEnter(suppress = Throwable.class) |
81 | | - public static void onEnter( |
| 115 | + public static AdviceScope onEnter( |
82 | 116 | @Advice.Argument(0) ConsumerRecords<?, ?> records, |
83 | | - @Advice.FieldValue("consumer") Consumer<?, ?> consumer, |
84 | | - @Advice.Local("otelRequest") KafkaReceiveRequest request, |
85 | | - @Advice.Local("otelContext") Context context, |
86 | | - @Advice.Local("otelScope") Scope scope) { |
87 | | - KafkaConsumerContext consumerContext = KafkaConsumerContextUtil.get(records); |
88 | | - Context receiveContext = consumerContext.getContext(); |
89 | | - |
90 | | - // use the receive CONSUMER span as parent if it's available |
91 | | - Context parentContext = receiveContext != null ? receiveContext : Context.current(); |
92 | | - |
93 | | - request = KafkaReceiveRequest.create(records, consumer); |
94 | | - if (batchProcessInstrumenter().shouldStart(parentContext, request)) { |
95 | | - context = batchProcessInstrumenter().start(parentContext, request); |
96 | | - scope = context.makeCurrent(); |
97 | | - } |
| 117 | + @Advice.FieldValue("consumer") Consumer<?, ?> consumer) { |
| 118 | + return AdviceScope.start(records, consumer); |
98 | 119 | } |
99 | 120 |
|
100 | 121 | @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
101 | 122 | public static void onExit( |
102 | | - @Advice.Thrown Throwable throwable, |
103 | | - @Advice.Local("otelRequest") KafkaReceiveRequest request, |
104 | | - @Advice.Local("otelContext") Context context, |
105 | | - @Advice.Local("otelScope") Scope scope) { |
106 | | - if (scope == null) { |
107 | | - return; |
| 123 | + @Advice.Thrown @Nullable Throwable throwable, |
| 124 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 125 | + if (adviceScope != null) { |
| 126 | + adviceScope.close(throwable); |
108 | 127 | } |
109 | | - scope.close(); |
110 | | - batchProcessInstrumenter().end(context, request, null, throwable); |
111 | 128 | } |
112 | 129 | } |
113 | 130 | } |
0 commit comments