|
15 | 15 | import io.opentelemetry.context.Context; |
16 | 16 | import io.opentelemetry.context.Scope; |
17 | 17 | import io.opentelemetry.javaagent.bootstrap.CallDepth; |
18 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
19 | 18 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
20 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
21 | 20 | import io.opentelemetry.javaagent.instrumentation.jms.MessageWithDestination; |
22 | 21 | import jakarta.jms.Destination; |
23 | 22 | import jakarta.jms.JMSException; |
24 | 23 | import jakarta.jms.Message; |
25 | 24 | import jakarta.jms.MessageProducer; |
| 25 | +import javax.annotation.Nullable; |
26 | 26 | import net.bytebuddy.asm.Advice; |
27 | 27 | import net.bytebuddy.description.type.TypeDescription; |
28 | 28 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -52,103 +52,90 @@ public void transform(TypeTransformer transformer) { |
52 | 52 | JmsMessageProducerInstrumentation.class.getName() + "$ProducerWithDestinationAdvice"); |
53 | 53 | } |
54 | 54 |
|
55 | | - @SuppressWarnings("unused") |
56 | | - public static class ProducerAdvice { |
| 55 | + public static class AdviceScope { |
| 56 | + private final CallDepth callDepth; |
| 57 | + @Nullable private final MessageWithDestination messageWithDestination; |
| 58 | + @Nullable private final Context context; |
| 59 | + @Nullable private final Scope scope; |
| 60 | + |
| 61 | + private AdviceScope( |
| 62 | + CallDepth callDepth, |
| 63 | + @Nullable MessageWithDestination messageWithDestination, |
| 64 | + @Nullable Context context, |
| 65 | + @Nullable Scope scope) { |
| 66 | + this.callDepth = callDepth; |
| 67 | + this.messageWithDestination = messageWithDestination; |
| 68 | + this.context = context; |
| 69 | + this.scope = scope; |
| 70 | + } |
57 | 71 |
|
58 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
59 | | - public static void onEnter( |
60 | | - @Advice.Argument(0) Message message, |
61 | | - @Advice.This MessageProducer producer, |
62 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
63 | | - @Advice.Local("otelRequest") MessageWithDestination request, |
64 | | - @Advice.Local("otelContext") Context context, |
65 | | - @Advice.Local("otelScope") Scope scope) { |
66 | | - callDepth = CallDepth.forClass(MessageProducer.class); |
| 72 | + public static AdviceScope start(CallDepth callDepth, Destination destination, Message message) { |
67 | 73 | if (callDepth.getAndIncrement() > 0) { |
68 | | - return; |
| 74 | + return new AdviceScope(callDepth, null, null, null); |
69 | 75 | } |
| 76 | + Context parentContext = Context.current(); |
70 | 77 |
|
71 | | - Destination defaultDestination; |
72 | | - try { |
73 | | - defaultDestination = producer.getDestination(); |
74 | | - } catch (JMSException e) { |
75 | | - defaultDestination = null; |
76 | | - } |
77 | | - |
78 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
79 | | - request = |
| 78 | + MessageWithDestination messageWithDestination = |
80 | 79 | MessageWithDestination.create( |
81 | | - JakartaMessageAdapter.create(message), |
82 | | - JakartaDestinationAdapter.create(defaultDestination)); |
83 | | - if (!producerInstrumenter().shouldStart(parentContext, request)) { |
84 | | - return; |
| 80 | + JakartaMessageAdapter.create(message), JakartaDestinationAdapter.create(destination)); |
| 81 | + if (!producerInstrumenter().shouldStart(parentContext, messageWithDestination)) { |
| 82 | + return new AdviceScope(callDepth, null, null, null); |
85 | 83 | } |
86 | 84 |
|
87 | | - context = producerInstrumenter().start(parentContext, request); |
88 | | - scope = context.makeCurrent(); |
| 85 | + Context context = producerInstrumenter().start(parentContext, messageWithDestination); |
| 86 | + return new AdviceScope(callDepth, messageWithDestination, context, context.makeCurrent()); |
89 | 87 | } |
90 | 88 |
|
91 | | - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
92 | | - public static void stopSpan( |
93 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
94 | | - @Advice.Local("otelRequest") MessageWithDestination request, |
95 | | - @Advice.Local("otelContext") Context context, |
96 | | - @Advice.Local("otelScope") Scope scope, |
97 | | - @Advice.Thrown Throwable throwable) { |
| 89 | + public void end(@Nullable Throwable throwable) { |
98 | 90 | if (callDepth.decrementAndGet() > 0) { |
99 | 91 | return; |
100 | 92 | } |
101 | | - |
102 | | - if (scope != null) { |
103 | | - scope.close(); |
104 | | - producerInstrumenter().end(context, request, null, throwable); |
| 93 | + if (scope == null || context == null || messageWithDestination == null) { |
| 94 | + return; |
105 | 95 | } |
| 96 | + |
| 97 | + scope.close(); |
| 98 | + producerInstrumenter().end(context, messageWithDestination, null, throwable); |
106 | 99 | } |
107 | 100 | } |
108 | 101 |
|
109 | 102 | @SuppressWarnings("unused") |
110 | | - public static class ProducerWithDestinationAdvice { |
| 103 | + public static class ProducerAdvice { |
111 | 104 |
|
112 | 105 | @Advice.OnMethodEnter(suppress = Throwable.class) |
113 | | - public static void onEnter( |
114 | | - @Advice.Argument(0) Destination destination, |
115 | | - @Advice.Argument(1) Message message, |
116 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
117 | | - @Advice.Local("otelRequest") MessageWithDestination request, |
118 | | - @Advice.Local("otelContext") Context context, |
119 | | - @Advice.Local("otelScope") Scope scope) { |
120 | | - callDepth = CallDepth.forClass(MessageProducer.class); |
121 | | - if (callDepth.getAndIncrement() > 0) { |
122 | | - return; |
| 106 | + public static AdviceScope onEnter( |
| 107 | + @Advice.Argument(0) Message message, @Advice.This MessageProducer producer) { |
| 108 | + Destination destination; |
| 109 | + try { |
| 110 | + destination = producer.getDestination(); |
| 111 | + } catch (JMSException e) { |
| 112 | + destination = null; |
123 | 113 | } |
| 114 | + CallDepth callDepth = CallDepth.forClass(MessageProducer.class); |
| 115 | + return AdviceScope.start(callDepth, destination, message); |
| 116 | + } |
124 | 117 |
|
125 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
126 | | - request = |
127 | | - MessageWithDestination.create( |
128 | | - JakartaMessageAdapter.create(message), JakartaDestinationAdapter.create(destination)); |
129 | | - if (!producerInstrumenter().shouldStart(parentContext, request)) { |
130 | | - return; |
131 | | - } |
| 118 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 119 | + public static void stopSpan( |
| 120 | + @Advice.Thrown Throwable throwable, @Advice.Enter AdviceScope adviceScope) { |
| 121 | + adviceScope.end(throwable); |
| 122 | + } |
| 123 | + } |
132 | 124 |
|
133 | | - context = producerInstrumenter().start(parentContext, request); |
134 | | - scope = context.makeCurrent(); |
| 125 | + @SuppressWarnings("unused") |
| 126 | + public static class ProducerWithDestinationAdvice { |
| 127 | + |
| 128 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 129 | + public static AdviceScope onEnter( |
| 130 | + @Advice.Argument(0) Destination destination, @Advice.Argument(1) Message message) { |
| 131 | + CallDepth callDepth = CallDepth.forClass(MessageProducer.class); |
| 132 | + return AdviceScope.start(callDepth, destination, message); |
135 | 133 | } |
136 | 134 |
|
137 | 135 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
138 | 136 | public static void stopSpan( |
139 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
140 | | - @Advice.Local("otelRequest") MessageWithDestination request, |
141 | | - @Advice.Local("otelContext") Context context, |
142 | | - @Advice.Local("otelScope") Scope scope, |
143 | | - @Advice.Thrown Throwable throwable) { |
144 | | - if (callDepth.decrementAndGet() > 0) { |
145 | | - return; |
146 | | - } |
147 | | - |
148 | | - if (scope != null) { |
149 | | - scope.close(); |
150 | | - producerInstrumenter().end(context, request, null, throwable); |
151 | | - } |
| 137 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) { |
| 138 | + adviceScope.end(throwable); |
152 | 139 | } |
153 | 140 | } |
154 | 141 | } |
0 commit comments