Skip to content

Commit 6727767

Browse files
authored
Fix the RocketMQ async-send callback NPE, and the client-java plugin with newer clients (#848)
* rocketMQ-5.x: skip callbacks without SendCallBackEnhanceInfo in OnSuccessInterceptor. Since rocketmq-client 5.1.4 the producer wraps every async-send callback in BackpressureSendCallBack, and the wrapped callback threw an NPE and stopped the wrapper's span, whose afterMethod then threw a second NPE (apache/skywalking#14189). * rocketMQ-client-java-5.x: intercept every PushConsumerImpl constructor taking the ClientConfiguration and the MessageListener, so push-consumer spans get their peer and mq.broker again with rocketmq-client-java 5.0.8 or later. * rocketMQ-client-java-5.x: stop rebuilding the sent message, which dropped the lite topic and the priority. Inject the span into the producer's PublishingMessageImpl copy and add the headers to the properties it sends. * Test rocketmq-client 5.5, and rocketmq-client-java 5.0, 5.1 and 5.2.
1 parent 1d3bdc0 commit 6727767

15 files changed

Lines changed: 300 additions & 87 deletions

File tree

‎CHANGES.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ Release Notes.
1717
error when any of them fails. A batch of a cluster connection gets one span per slot, as Lettuce writes the commands
1818
of each slot as a batch of their own. Mark the span of a reactive Lettuce command as an error when Redis replies with
1919
an error, such as `WRONGTYPE`.
20+
* Fix the `NullPointerException`s thrown by the `rocketMQ-5.x` plugin, and the callback span stopped by the wrong
21+
callback, when an asynchronous send with a callback succeeds on rocketmq-client 5.1.4 or later, which wraps the
22+
callback in a `SendCallback` of its own (apache/skywalking#14189).
23+
* Fix the missing `peer` and `mq.broker` of the consumer spans of the `rocketMQ-client-java-5.x` plugin for push
24+
consumers built by rocketmq-client-java 5.0.8 or later, which construct `PushConsumerImpl` with another constructor.
25+
* Stop the `rocketMQ-client-java-5.x` plugin from dropping the lite topic (rocketmq-client-java 5.1.0 or later) and the
26+
priority (5.2.0 or later) of the messages it traces, which turned them into normal messages. The plugin rebuilt every
27+
message to add its headers, and now adds them to the copy of the message that the client sends.
28+
* Test the `rocketMQ-5.x` plugin with rocketmq-client 5.5, and the `rocketMQ-client-java-5.x` plugin with
29+
rocketmq-client-java 5.0, 5.1 and 5.2.
2030

2131
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/267?closed=1)
2232

‎apm-sniffer/apm-sdk-plugin/rocketMQ-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/rocketMQ/v5/OnSuccessInterceptor.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
/**
3535
* {@link OnSuccessInterceptor} create local span when the method {@link org.apache.rocketmq.client.producer.SendCallback#onSuccess(SendResult)}
3636
* execute.
37+
* <p>
38+
* {@link MessageSendInterceptor} attaches the {@link SendCallBackEnhanceInfo} to the callback passed to {@link
39+
* org.apache.rocketmq.client.impl.MQClientAPIImpl}. A callback invoked by that callback, such as the user callback that
40+
* {@code DefaultMQProducerImpl$BackpressureSendCallBack} wraps since rocketmq-client 5.1.4, usually has none. It runs
41+
* inside the span of the outer callback, so all three methods skip it, which keeps the span stack balanced.
3742
*/
3843
public class OnSuccessInterceptor implements InstanceMethodsAroundInterceptor {
3944

@@ -43,6 +48,9 @@ public class OnSuccessInterceptor implements InstanceMethodsAroundInterceptor {
4348
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
4449
MethodInterceptResult result) throws Throwable {
4550
SendCallBackEnhanceInfo enhanceInfo = (SendCallBackEnhanceInfo) objInst.getSkyWalkingDynamicField();
51+
if (enhanceInfo == null) {
52+
return;
53+
}
4654
AbstractSpan activeSpan = ContextManager.createLocalSpan(CALLBACK_OPERATION_NAME_PREFIX + enhanceInfo.getTopicId() + "/Producer/Callback");
4755
activeSpan.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER);
4856
SendStatus sendStatus = ((SendResult) allArguments[0]).getSendStatus();
@@ -56,13 +64,19 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5664
@Override
5765
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
5866
Object ret) throws Throwable {
67+
if (objInst.getSkyWalkingDynamicField() == null) {
68+
return ret;
69+
}
5970
ContextManager.stopSpan();
6071
return ret;
6172
}
6273

6374
@Override
6475
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
6576
Class<?>[] argumentsTypes, Throwable t) {
77+
if (objInst.getSkyWalkingDynamicField() == null) {
78+
return;
79+
}
6680
ContextManager.activeSpan().log(t);
6781
}
6882
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5;
20+
21+
import java.lang.reflect.Method;
22+
import java.util.Map;
23+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
24+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
28+
import org.apache.skywalking.apm.util.StringUtil;
29+
30+
/**
31+
* {@link MessageGetPropertiesInterceptor} adds the context that {@link PublishingMessageImplInterceptor} injected into
32+
* a {@code PublishingMessageImpl} to the new map that {@code MessageImpl#getProperties()} returns. Other messages carry
33+
* no context and are left alone.
34+
*/
35+
public class MessageGetPropertiesInterceptor implements InstanceMethodsAroundInterceptor {
36+
37+
@Override
38+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
39+
MethodInterceptResult result) throws Throwable {
40+
}
41+
42+
@Override
43+
@SuppressWarnings("unchecked")
44+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
45+
Object ret) throws Throwable {
46+
Object contextCarrier = objInst.getSkyWalkingDynamicField();
47+
if (!(contextCarrier instanceof ContextCarrier) || !(ret instanceof Map)) {
48+
return ret;
49+
}
50+
Map<String, String> properties = (Map<String, String>) ret;
51+
CarrierItem next = ((ContextCarrier) contextCarrier).items();
52+
while (next.hasNext()) {
53+
next = next.next();
54+
if (!StringUtil.isEmpty(next.getHeadValue())) {
55+
properties.put(next.getHeadKey(), next.getHeadValue());
56+
}
57+
}
58+
return ret;
59+
}
60+
61+
@Override
62+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
63+
Class<?>[] argumentsTypes, Throwable t) {
64+
}
65+
}

‎apm-sniffer/apm-sdk-plugin/rocketMQ-client-java-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/MessageSendAsyncInterceptor.java‎

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020

2121
import java.lang.reflect.Method;
2222
import java.util.Collection;
23-
import java.util.Map;
2423
import java.util.Optional;
2524
import java.util.concurrent.CompletableFuture;
2625
import org.apache.rocketmq.client.apis.message.Message;
27-
import org.apache.rocketmq.client.apis.message.MessageBuilder;
2826
import org.apache.rocketmq.client.apis.producer.SendReceipt;
2927
import org.apache.rocketmq.client.java.impl.ClientImpl;
30-
import org.apache.rocketmq.client.java.message.MessageBuilderImpl;
31-
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
32-
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
3328
import org.apache.skywalking.apm.agent.core.context.ContextManager;
3429
import org.apache.skywalking.apm.agent.core.context.tag.StringTag;
3530
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
@@ -40,11 +35,10 @@
4035
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
4136
import org.apache.skywalking.apm.agent.core.util.CollectionUtil;
4237
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
43-
import org.apache.skywalking.apm.util.StringUtil;
4438

4539
/**
4640
* {@link MessageSendAsyncInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#sendAsync(org.apache.rocketmq.client.apis.message.Message)}
47-
* execute
41+
* execute. {@link PublishingMessageImplInterceptor} injects the span into the message that is sent.
4842
*/
4943
public class MessageSendAsyncInterceptor implements InstanceMethodsAroundInterceptor {
5044

@@ -62,10 +56,8 @@ public void beforeMethod(EnhancedInstance objInst,
6256
Message message = (Message) allArguments[0];
6357
ClientImpl producerImpl = (ClientImpl) objInst;
6458

65-
ContextCarrier contextCarrier = new ContextCarrier();
6659
String namingServiceAddress = producerImpl.getClientConfiguration().getEndpoints();
67-
AbstractSpan span = ContextManager.createExitSpan(
68-
buildOperationName(message.getTopic()), contextCarrier, namingServiceAddress);
60+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), namingServiceAddress);
6961
span.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER);
7062
Tags.MQ_BROKER.set(span, namingServiceAddress);
7163
Tags.MQ_TOPIC.set(span, message.getTopic());
@@ -79,38 +71,7 @@ public void beforeMethod(EnhancedInstance objInst,
7971
Optional<String> tag = message.getTag();
8072
tag.ifPresent(s -> span.tag(MQ_MESSAGE_TAGS, s));
8173
}
82-
83-
contextCarrier.extensionInjector().injectSendingTimestamp();
8474
SpanLayer.asMQ(span);
85-
86-
Map<String, String> properties = message.getProperties();
87-
CarrierItem next = contextCarrier.items();
88-
while (next.hasNext()) {
89-
next = next.next();
90-
if (!StringUtil.isEmpty(next.getHeadValue())) {
91-
properties.put(next.getHeadKey(), next.getHeadValue());
92-
}
93-
}
94-
95-
MessageBuilder messageBuilder = new MessageBuilderImpl();
96-
messageBuilder.setTopic(message.getTopic());
97-
if (message.getTag().isPresent()) {
98-
messageBuilder.setTag(message.getTag().get());
99-
}
100-
messageBuilder.setKeys(message.getKeys().toArray(new String[0]));
101-
if (message.getMessageGroup().isPresent()) {
102-
messageBuilder.setMessageGroup(message.getMessageGroup().get());
103-
}
104-
105-
byte[] body = new byte[message.getBody().limit()];
106-
message.getBody().get(body);
107-
messageBuilder.setBody(body);
108-
if (message.getDeliveryTimestamp().isPresent()) {
109-
messageBuilder.setDeliveryTimestamp(message.getDeliveryTimestamp().get());
110-
}
111-
112-
properties.forEach(messageBuilder::addProperty);
113-
allArguments[0] = messageBuilder.build();
11475
}
11576

11677
@Override

‎apm-sniffer/apm-sdk-plugin/rocketMQ-client-java-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/MessageSendInterceptor.java‎

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5;
2020

2121
import org.apache.rocketmq.client.apis.message.Message;
22-
import org.apache.rocketmq.client.apis.message.MessageBuilder;
2322
import org.apache.rocketmq.client.apis.producer.SendReceipt;
2423
import org.apache.rocketmq.client.apis.producer.Transaction;
2524
import org.apache.rocketmq.client.java.impl.ClientImpl;
26-
import org.apache.rocketmq.client.java.message.MessageBuilderImpl;
27-
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
28-
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
2925
import org.apache.skywalking.apm.agent.core.context.ContextManager;
3026
import org.apache.skywalking.apm.agent.core.context.tag.StringTag;
3127
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
@@ -36,17 +32,16 @@
3632
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
3733
import org.apache.skywalking.apm.agent.core.util.CollectionUtil;
3834
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
39-
import org.apache.skywalking.apm.util.StringUtil;
4035

4136
import java.lang.reflect.Method;
4237
import java.util.Collection;
43-
import java.util.Map;
4438
import java.util.Optional;
4539
import java.util.stream.Collectors;
4640

4741
/**
4842
* {@link MessageSendInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message)}
4943
* and {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message, Transaction)} execute.
44+
* {@link PublishingMessageImplInterceptor} injects the span into the message that is sent.
5045
*/
5146
public class MessageSendInterceptor implements InstanceMethodsAroundInterceptor {
5247

@@ -60,9 +55,8 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
6055
Message message = (Message) allArguments[0];
6156
ClientImpl producerImpl = (ClientImpl) objInst;
6257

63-
ContextCarrier contextCarrier = new ContextCarrier();
6458
String namingServiceAddress = producerImpl.getClientConfiguration().getEndpoints();
65-
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), contextCarrier, namingServiceAddress);
59+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), namingServiceAddress);
6660
span.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER);
6761
Tags.MQ_BROKER.set(span, namingServiceAddress);
6862
Tags.MQ_TOPIC.set(span, message.getTopic());
@@ -78,37 +72,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
7872
span.tag(MQ_MESSAGE_TAGS, tag.get());
7973
}
8074
}
81-
82-
contextCarrier.extensionInjector().injectSendingTimestamp();
8375
SpanLayer.asMQ(span);
84-
85-
Map<String, String> properties = message.getProperties();
86-
CarrierItem next = contextCarrier.items();
87-
while (next.hasNext()) {
88-
next = next.next();
89-
if (!StringUtil.isEmpty(next.getHeadValue())) {
90-
properties.put(next.getHeadKey(), next.getHeadValue());
91-
}
92-
}
93-
94-
MessageBuilder messageBuilder = new MessageBuilderImpl();
95-
messageBuilder.setTopic(message.getTopic());
96-
if (message.getTag().isPresent()) {
97-
messageBuilder.setTag(message.getTag().get());
98-
}
99-
messageBuilder.setKeys(message.getKeys().toArray(new String[0]));
100-
if (message.getMessageGroup().isPresent()) {
101-
messageBuilder.setMessageGroup(message.getMessageGroup().get());
102-
}
103-
104-
byte[] body = new byte[message.getBody().limit()];
105-
message.getBody().get(body);
106-
messageBuilder.setBody(body);
107-
if (message.getDeliveryTimestamp().isPresent()) {
108-
messageBuilder.setDeliveryTimestamp(message.getDeliveryTimestamp().get());
109-
}
110-
properties.entrySet().forEach(item -> messageBuilder.addProperty(item.getKey(), item.getValue()));
111-
allArguments[0] = messageBuilder.build();
11276
}
11377

11478
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
24+
import org.apache.skywalking.apm.agent.core.context.trace.NoopSpan;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
27+
28+
/**
29+
* {@link PublishingMessageImplInterceptor} injects the exit span of the send, created by {@link
30+
* MessageSendInterceptor} or {@link MessageSendAsyncInterceptor}, into the {@code PublishingMessageImpl} that the
31+
* producer copies the message into. {@link MessageGetPropertiesInterceptor} adds it to the user properties of the
32+
* message that is sent, so the message the application passed keeps every attribute, such as the lite topic and the
33+
* priority of newer clients. When the trace is ignored, the active span is a {@link NoopSpan} instead of an exit span,
34+
* and the carrier still carries the correlation context and the sending timestamp.
35+
*/
36+
public class PublishingMessageImplInterceptor implements InstanceConstructorInterceptor {
37+
38+
@Override
39+
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
40+
if (!ContextManager.isActive()) {
41+
return;
42+
}
43+
AbstractSpan activeSpan = ContextManager.activeSpan();
44+
if (!activeSpan.isExit() && !(activeSpan instanceof NoopSpan)) {
45+
return;
46+
}
47+
ContextCarrier contextCarrier = new ContextCarrier();
48+
ContextManager.inject(contextCarrier);
49+
contextCarrier.extensionInjector().injectSendingTimestamp();
50+
objInst.setSkyWalkingDynamicField(contextCarrier);
51+
}
52+
}

‎apm-sniffer/apm-sdk-plugin/rocketMQ-client-java-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/PushConsumerImplInterceptor.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
2525
import org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5.define.ConsumerEnhanceInfos;
2626

27-
import java.util.Map;
28-
2927
/**
30-
* {@link PushConsumerImplInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.consumer.PushConsumerImpl#PushConsumerImpl(ClientConfiguration, String, Map, MessageListener, int, int, int)} execute.
28+
* {@link PushConsumerImplInterceptor} passes the endpoints of the {@link ClientConfiguration} to the {@link
29+
* MessageListener} when a {@link org.apache.rocketmq.client.java.impl.consumer.PushConsumerImpl} is constructed.
3130
*/
3231
public class PushConsumerImplInterceptor implements InstanceConstructorInterceptor {
3332

0 commit comments

Comments
 (0)