|
18 | 18 | import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
19 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 21 | +import javax.annotation.Nullable; |
21 | 22 | import net.bytebuddy.asm.Advice; |
| 23 | +import net.bytebuddy.asm.Advice.AssignReturned; |
| 24 | +import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; |
22 | 25 | import net.bytebuddy.description.type.TypeDescription; |
23 | 26 | import net.bytebuddy.matcher.ElementMatcher; |
24 | 27 | import org.apache.kafka.clients.ApiVersions; |
@@ -46,48 +49,83 @@ public void transform(TypeTransformer transformer) { |
46 | 49 | @SuppressWarnings("unused") |
47 | 50 | public static class SendAdvice { |
48 | 51 |
|
49 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
50 | | - public static KafkaProducerRequest onEnter( |
51 | | - @Advice.FieldValue("apiVersions") ApiVersions apiVersions, |
52 | | - @Advice.FieldValue("clientId") String clientId, |
53 | | - @Advice.Argument(value = 0, readOnly = false) ProducerRecord<?, ?> record, |
54 | | - @Advice.Argument(value = 1, readOnly = false) Callback callback, |
55 | | - @Advice.Local("otelContext") Context context, |
56 | | - @Advice.Local("otelScope") Scope scope) { |
| 52 | + public static class AdviceScope { |
| 53 | + private final KafkaProducerRequest request; |
| 54 | + private final Context context; |
| 55 | + private final Scope scope; |
| 56 | + private final Context parentContext; |
57 | 57 |
|
58 | | - KafkaProducerRequest request = KafkaProducerRequest.create(record, clientId); |
59 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
60 | | - if (!producerInstrumenter().shouldStart(parentContext, request)) { |
61 | | - return null; |
| 58 | + private AdviceScope( |
| 59 | + Context parentContext, KafkaProducerRequest request, Context context, Scope scope) { |
| 60 | + this.parentContext = parentContext; |
| 61 | + this.request = request; |
| 62 | + this.context = context; |
| 63 | + this.scope = scope; |
62 | 64 | } |
63 | 65 |
|
64 | | - context = producerInstrumenter().start(parentContext, request); |
65 | | - scope = context.makeCurrent(); |
| 66 | + @Nullable |
| 67 | + public static AdviceScope start(KafkaProducerRequest request) { |
| 68 | + Context parentContext = Java8BytecodeBridge.currentContext(); |
| 69 | + if (!producerInstrumenter().shouldStart(parentContext, request)) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + Context context = producerInstrumenter().start(parentContext, request); |
| 73 | + return new AdviceScope(parentContext, request, context, context.makeCurrent()); |
| 74 | + } |
66 | 75 |
|
67 | | - if (KafkaSingletons.isProducerPropagationEnabled() |
68 | | - && KafkaPropagation.shouldPropagate(apiVersions)) { |
69 | | - record = KafkaPropagation.propagateContext(context, record); |
| 76 | + public Callback wrapCallback(Callback originalCallback) { |
| 77 | + return new ProducerCallback(originalCallback, parentContext, context, request); |
70 | 78 | } |
71 | 79 |
|
72 | | - callback = new ProducerCallback(callback, parentContext, context, request); |
73 | | - return request; |
| 80 | + public ProducerRecord<?, ?> propagateContext( |
| 81 | + ApiVersions apiVersions, ProducerRecord<?, ?> record) { |
| 82 | + if (KafkaSingletons.isProducerPropagationEnabled() |
| 83 | + && KafkaPropagation.shouldPropagate(apiVersions)) { |
| 84 | + return KafkaPropagation.propagateContext(context, record); |
| 85 | + } |
| 86 | + return record; |
| 87 | + } |
| 88 | + |
| 89 | + public void end(@Nullable Throwable throwable) { |
| 90 | + scope.close(); |
| 91 | + if (throwable != null) { |
| 92 | + producerInstrumenter().end(context, request, null, throwable); |
| 93 | + } |
| 94 | + // span finished by ProducerCallback |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @AssignReturned.ToArguments({ |
| 99 | + @ToArgument(value = 0, index = 1), |
| 100 | + @ToArgument(value = 1, index = 2) |
| 101 | + }) |
| 102 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 103 | + public static Object[] onEnter( |
| 104 | + @Advice.FieldValue("apiVersions") ApiVersions apiVersions, |
| 105 | + @Advice.FieldValue("clientId") String clientId, |
| 106 | + @Advice.Argument(0) ProducerRecord<?, ?> originalRecord, |
| 107 | + @Advice.Argument(1) Callback originalCallback) { |
| 108 | + ProducerRecord<?, ?> record = originalRecord; |
| 109 | + Callback callback = originalCallback; |
| 110 | + |
| 111 | + KafkaProducerRequest request = KafkaProducerRequest.create(record, clientId); |
| 112 | + AdviceScope adviceScope = AdviceScope.start(request); |
| 113 | + if (adviceScope == null) { |
| 114 | + return new Object[] {null, record, callback}; |
| 115 | + } |
| 116 | + record = adviceScope.propagateContext(apiVersions, record); |
| 117 | + callback = adviceScope.wrapCallback(callback); |
| 118 | + return new Object[] {adviceScope, record, callback}; |
74 | 119 | } |
75 | 120 |
|
76 | 121 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
77 | 122 | public static void stopSpan( |
78 | | - @Advice.Enter KafkaProducerRequest request, |
79 | | - @Advice.Thrown Throwable throwable, |
80 | | - @Advice.Local("otelContext") Context context, |
81 | | - @Advice.Local("otelScope") Scope scope) { |
82 | | - if (scope == null) { |
83 | | - return; |
84 | | - } |
85 | | - scope.close(); |
| 123 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) { |
86 | 124 |
|
87 | | - if (throwable != null) { |
88 | | - producerInstrumenter().end(context, request, null, throwable); |
| 125 | + AdviceScope adviceScope = (AdviceScope) enterResult[0]; |
| 126 | + if (adviceScope != null) { |
| 127 | + adviceScope.end(throwable); |
89 | 128 | } |
90 | | - // span finished by ProducerCallback |
91 | 129 | } |
92 | 130 | } |
93 | 131 | } |
0 commit comments