This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new b72e984289 fix npe in guava eventbus plugin (#559)
b72e984289 is described below

commit b72e984289b154c4d7d9714e826ce03482fbf5b3
Author: xzyJavaX <[email protected]>
AuthorDate: Wed Jun 21 14:32:18 2023 +0800

    fix npe in guava eventbus plugin (#559)
    
    Co-authored-by: 熊哲源 <[email protected]>
---
 CHANGES.md                                               |  1 +
 .../guava/eventbus/EventBusDispatchInterceptor.java      |  2 +-
 .../guava/eventbus/EventBusDispatchInterceptorTest.java  |  1 +
 .../guava-eventbus-scenario/config/expectedData.yaml     | 16 +++++++++++++++-
 .../guava/eventbus/controller/CaseController.java        |  3 +++
 5 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index c97f852570..52404c2dbd 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -11,6 +11,7 @@ Release Notes.
 * Add RocketMQ 5.x plugin
 * Fix the conflict between the logging kernel and the JDK threadpool plugin.
 * Fix the thread safety bug of finishing operation for the span named 
"SpringCloudGateway/sendRequest"
+* Fix NPE in guava-eventbus-plugin.
 
 #### Documentation
 
diff --git 
a/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/main/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptor.java
 
b/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/main/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptor.java
index 34ab37ab85..f641d025e6 100644
--- 
a/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/main/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptor.java
+++ 
b/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/main/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptor.java
@@ -34,7 +34,7 @@ public class EventBusDispatchInterceptor implements 
InstanceMethodsAroundInterce
     public void beforeMethod(EnhancedInstance objInst, Method method, Object[] 
allArguments, Class<?>[] argumentsTypes,
             MethodInterceptResult result) throws Throwable {
         final Object event = allArguments[0];
-        if (event != null) {
+        if (event != null && ContextManager.isActive()) {
             allArguments[0] = EventWrapper.wrapEvent(event, 
ContextManager.capture());
         }
     }
diff --git 
a/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/test/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptorTest.java
 
b/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/test/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptorTest.java
index dc5056ffd8..ff0c3853bb 100644
--- 
a/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/test/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptorTest.java
+++ 
b/apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/test/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptorTest.java
@@ -54,6 +54,7 @@ public class EventBusDispatchInterceptorTest {
         originalEventObj = new Object();
 
         mockedContextManager = Mockito.mockStatic(ContextManager.class);
+        mockedContextManager.when(ContextManager::isActive).thenReturn(true);
         
mockedContextManager.when(ContextManager::capture).thenReturn(contextSnapshot);
     }
 
diff --git 
a/test/plugin/scenarios/guava-eventbus-scenario/config/expectedData.yaml 
b/test/plugin/scenarios/guava-eventbus-scenario/config/expectedData.yaml
index 550149fae4..1978ae6bbf 100644
--- a/test/plugin/scenarios/guava-eventbus-scenario/config/expectedData.yaml
+++ b/test/plugin/scenarios/guava-eventbus-scenario/config/expectedData.yaml
@@ -15,7 +15,7 @@
 # limitations under the License.
 segmentItems:
   - serviceName: guava-eventbus-scenario
-    segmentSize: ge 2
+    segmentSize: ge 3
     segments:
       - segmentId: not null
         spans:
@@ -66,4 +66,18 @@ segmentItems:
                   refType: CrossThread, parentSpanId: 0, parentTraceSegmentId: 
not null,
                   parentServiceInstance: not null, parentService: 
guava-eventbus-scenario,
                   traceId: not null }
+      - segmentId: not null
+        spans:
+          - operationName: 
Guava/EventBus/org.apache.skywalking.apm.testcase.guava.eventbus.service.SubscriberService/consumer
+            operationId: 0
+            parentSpanId: -1
+            spanId: 0
+            spanLayer: Unknown
+            startTime: nq 0
+            endTime: nq 0
+            componentId: 123
+            isError: false
+            spanType: Local
+            peer: ''
+            skipAnalysis: false
 meterItems: []
diff --git 
a/test/plugin/scenarios/guava-eventbus-scenario/src/main/java/org/apache/skywalking/apm/testcase/guava/eventbus/controller/CaseController.java
 
b/test/plugin/scenarios/guava-eventbus-scenario/src/main/java/org/apache/skywalking/apm/testcase/guava/eventbus/controller/CaseController.java
index adee578ee1..14bb44f86a 100644
--- 
a/test/plugin/scenarios/guava-eventbus-scenario/src/main/java/org/apache/skywalking/apm/testcase/guava/eventbus/controller/CaseController.java
+++ 
b/test/plugin/scenarios/guava-eventbus-scenario/src/main/java/org/apache/skywalking/apm/testcase/guava/eventbus/controller/CaseController.java
@@ -46,6 +46,9 @@ public class CaseController {
         asyncEventBus.register(subscriberService);
         final TestEvent testEvent = new TestEvent();
         testEvent.setContent("test");
+        final TestEvent testEvent2 = new TestEvent();
+        testEvent2.setContent("test2");
+        new Thread(() -> asyncEventBus.post(testEvent2)).start();
         eventBus.post(testEvent);
         testEvent.setAsyncContext(request.startAsync());
         asyncEventBus.post(testEvent);

Reply via email to