Skip to content

Commit 4922167

Browse files
committed
jms-1.1
1 parent a1a12f6 commit 4922167

File tree

3 files changed

+105
-94
lines changed

3 files changed

+105
-94
lines changed

instrumentation/jms/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/v1_1/JmsInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class JmsInstrumentationModule extends InstrumentationModule {
17+
public class JmsInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719
public JmsInstrumentationModule() {
1820
super("jms", "jms-1.1");
1921
}
@@ -25,4 +27,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
2527
new JmsMessageListenerInstrumentation(),
2628
new JmsMessageProducerInstrumentation());
2729
}
30+
31+
@Override
32+
public boolean isIndyReady() {
33+
return true;
34+
}
2835
}

instrumentation/jms/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/v1_1/JmsMessageListenerInstrumentation.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
import io.opentelemetry.context.Context;
1616
import io.opentelemetry.context.Scope;
17-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1817
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1918
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2019
import io.opentelemetry.javaagent.instrumentation.jms.MessageWithDestination;
20+
import javax.annotation.Nullable;
2121
import javax.jms.Message;
2222
import net.bytebuddy.asm.Advice;
2323
import net.bytebuddy.description.type.TypeDescription;
@@ -45,35 +45,52 @@ public void transform(TypeTransformer transformer) {
4545
@SuppressWarnings("unused")
4646
public static class MessageListenerAdvice {
4747

48-
@Advice.OnMethodEnter(suppress = Throwable.class)
49-
public static void onEnter(
50-
@Advice.Argument(0) Message message,
51-
@Advice.Local("otelRequest") MessageWithDestination request,
52-
@Advice.Local("otelContext") Context context,
53-
@Advice.Local("otelScope") Scope scope) {
48+
public static class AdviceScope {
49+
private final MessageWithDestination messageWithDestination;
50+
private final Context context;
51+
private final Scope scope;
52+
53+
private AdviceScope(
54+
MessageWithDestination messageWithDestination, Context context, Scope scope) {
55+
this.messageWithDestination = messageWithDestination;
56+
this.context = context;
57+
this.scope = scope;
58+
}
59+
60+
@Nullable
61+
private static AdviceScope start(Message message) {
62+
Context parentContext = Context.current();
63+
MessageWithDestination messageWithDestination =
64+
MessageWithDestination.create(JavaxMessageAdapter.create(message), null);
5465

55-
Context parentContext = Java8BytecodeBridge.currentContext();
56-
request = MessageWithDestination.create(JavaxMessageAdapter.create(message), null);
66+
if (!consumerProcessInstrumenter().shouldStart(parentContext, messageWithDestination)) {
67+
return null;
68+
}
5769

58-
if (!consumerProcessInstrumenter().shouldStart(parentContext, request)) {
59-
return;
70+
Context context =
71+
consumerProcessInstrumenter().start(parentContext, messageWithDestination);
72+
return new AdviceScope(messageWithDestination, context, context.makeCurrent());
6073
}
6174

62-
context = consumerProcessInstrumenter().start(parentContext, request);
63-
scope = context.makeCurrent();
75+
public void end(@Nullable Throwable throwable) {
76+
scope.close();
77+
consumerProcessInstrumenter().end(context, messageWithDestination, null, throwable);
78+
}
79+
}
80+
81+
@Nullable
82+
@Advice.OnMethodEnter(suppress = Throwable.class)
83+
public static AdviceScope onEnter(@Advice.Argument(0) Message message) {
84+
return AdviceScope.start(message);
6485
}
6586

6687
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6788
public static void stopSpan(
68-
@Advice.Local("otelRequest") MessageWithDestination request,
69-
@Advice.Local("otelContext") Context context,
70-
@Advice.Local("otelScope") Scope scope,
71-
@Advice.Thrown Throwable throwable) {
72-
if (scope == null) {
73-
return;
89+
@Advice.Thrown @Nullable Throwable throwable,
90+
@Advice.Enter @Nullable AdviceScope adviceScope) {
91+
if (adviceScope != null) {
92+
adviceScope.end(throwable);
7493
}
75-
scope.close();
76-
consumerProcessInstrumenter().end(context, request, null, throwable);
7794
}
7895
}
7996
}

instrumentation/jms/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/v1_1/JmsMessageProducerInstrumentation.java

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import io.opentelemetry.context.Context;
1616
import io.opentelemetry.context.Scope;
1717
import io.opentelemetry.javaagent.bootstrap.CallDepth;
18-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1918
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2019
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2120
import io.opentelemetry.javaagent.instrumentation.jms.MessageWithDestination;
21+
import javax.annotation.Nullable;
2222
import javax.jms.Destination;
2323
import javax.jms.JMSException;
2424
import javax.jms.Message;
@@ -52,103 +52,90 @@ public void transform(TypeTransformer transformer) {
5252
JmsMessageProducerInstrumentation.class.getName() + "$ProducerWithDestinationAdvice");
5353
}
5454

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+
}
5771

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) {
6773
if (callDepth.getAndIncrement() > 0) {
68-
return;
74+
return new AdviceScope(callDepth, null, null, null);
6975
}
76+
Context parentContext = Context.current();
7077

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 =
8079
MessageWithDestination.create(
81-
JavaxMessageAdapter.create(message),
82-
JavaxDestinationAdapter.create(defaultDestination));
83-
if (!producerInstrumenter().shouldStart(parentContext, request)) {
84-
return;
80+
JavaxMessageAdapter.create(message), JavaxDestinationAdapter.create(destination));
81+
if (!producerInstrumenter().shouldStart(parentContext, messageWithDestination)) {
82+
return new AdviceScope(callDepth, null, null, null);
8583
}
8684

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());
8987
}
9088

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) {
9890
if (callDepth.decrementAndGet() > 0) {
9991
return;
10092
}
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;
10595
}
96+
97+
scope.close();
98+
producerInstrumenter().end(context, messageWithDestination, null, throwable);
10699
}
107100
}
108101

109102
@SuppressWarnings("unused")
110-
public static class ProducerWithDestinationAdvice {
103+
public static class ProducerAdvice {
111104

112105
@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;
123113
}
114+
CallDepth callDepth = CallDepth.forClass(MessageProducer.class);
115+
return AdviceScope.start(callDepth, destination, message);
116+
}
124117

125-
Context parentContext = Java8BytecodeBridge.currentContext();
126-
request =
127-
MessageWithDestination.create(
128-
JavaxMessageAdapter.create(message), JavaxDestinationAdapter.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+
}
132124

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);
135133
}
136134

137135
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
138136
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);
152139
}
153140
}
154141
}

0 commit comments

Comments
 (0)