Don't use rocketmq trace context
Resolves
As described in the linked issue our usage of rocketmq trace context can
conflict with other hooks that also use `setMqTraceContext`.
MODIFY
instrumentation/rocketmq/rocketmq-client/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmqclient/v4_8/TracingConsumeMessageHookImpl.java
instrumentation/rocketmq/rocketmq-client/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmqclient/v4_8/TracingConsumeMessageHookImpl.java
package io.opentelemetry.instrumentation.rocketmqclient.v4_8;
import io.opentelemetry.context.Context;
+import io.opentelemetry.instrumentation.api.util.VirtualField;
import org.apache.rocketmq.client.hook.ConsumeMessageContext;
import org.apache.rocketmq.client.hook.ConsumeMessageHook;
final class TracingConsumeMessageHookImpl implements ConsumeMessageHook {
+ private static final VirtualField<ConsumeMessageContext, ContextAndScope> contextAndScopeField =
+ VirtualField.find(ConsumeMessageContext.class, ContextAndScope.class);
+
private final RocketMqConsumerInstrumenter instrumenter;
TracingConsumeMessageHookImpl(RocketMqConsumerInstrumenter instrumenter) {
@@ -30,12 +34,13 @@ final class TracingConsumeMessageHookImpl implements ConsumeMessageHook {
Context parentContext = Context.current();
Context newContext = instrumenter.start(parentContext, context.getMsgList());
- // it's safe to store the scope in the rocketMq trace context, both before() and after() methods
- // are always called from the same thread; see:
+ // it's safe to store the scope in the rocketMq message context, both before() and after()
+ // methods are always called from the same thread; see:
// - ConsumeMessageConcurrentlyService$ConsumeRequest#run()
// - ConsumeMessageOrderlyService$ConsumeRequest#run()
if (newContext != parentContext) {
- context.setMqTraceContext(ContextAndScope.create(newContext, newContext.makeCurrent()));
+ contextAndScopeField.set(
+ context, ContextAndScope.create(newContext, newContext.makeCurrent()));
}
}
@@ -44,8 +49,8 @@ final class TracingConsumeMessageHookImpl implements ConsumeMessageHook {
if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
return;
}
- if (context.getMqTraceContext() instanceof ContextAndScope) {
- ContextAndScope contextAndScope = (ContextAndScope) context.getMqTraceContext();
+ ContextAndScope contextAndScope = contextAndScopeField.get(context);
+ if (contextAndScope != null) {
contextAndScope.close();
instrumenter.end(contextAndScope.getContext(), context.getMsgList());
}

MODIFY
instrumentation/rocketmq/rocketmq-client/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmqclient/v4_8/TracingSendMessageHookImpl.java
instrumentation/rocketmq/rocketmq-client/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmqclient/v4_8/TracingSendMessageHookImpl.java
@@ -7,11 +7,15 @@ package io.opentelemetry.instrumentation.rocketmqclient.v4_8;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+import io.opentelemetry.instrumentation.api.util.VirtualField;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.client.hook.SendMessageHook;
final class TracingSendMessageHookImpl implements SendMessageHook {
+ private static final VirtualField<SendMessageContext, Context> contextField =
+ VirtualField.find(SendMessageContext.class, Context.class);
+
private final Instrumenter<SendMessageContext, Void> instrumenter;
TracingSendMessageHookImpl(Instrumenter<SendMessageContext, Void> instrumenter) {
@@ -32,7 +36,7 @@ final class TracingSendMessageHookImpl implements SendMessageHook {
if (!instrumenter.shouldStart(parentContext, context)) {
return;
}
- context.setMqTraceContext(instrumenter.start(parentContext, context));
+ contextField.set(context, instrumenter.start(parentContext, context));
}
@Override
@@ -40,9 +44,9 @@ final class TracingSendMessageHookImpl implements SendMessageHook {
if (context == null) {
return;
}
- if (context.getMqTraceContext() instanceof Context
+ Context otelContext = contextField.get(context);
+ if (otelContext != null
&& (context.getSendResult() != null || context.getException() != null)) {
- Context otelContext = (Context) context.getMqTraceContext();
instrumenter.end(otelContext, context, null, context.getException());
}
}

Don't use rocketmq trace context (#6940)

Resolves
https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/6934
As described in the linked issue our usage of rocketmq trace context can
conflict with other hooks that also use `setMqTraceContext`.