Skip to content

Commit b72e984

Browse files
xzyJavaX熊哲源
and
熊哲源
authored
fix npe in guava eventbus plugin (#559)
Co-authored-by: 熊哲源 <[email protected]>
1 parent 3497199 commit b72e984

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Release Notes.
1111
* Add RocketMQ 5.x plugin
1212
* Fix the conflict between the logging kernel and the JDK threadpool plugin.
1313
* Fix the thread safety bug of finishing operation for the span named "SpringCloudGateway/sendRequest"
14+
* Fix NPE in guava-eventbus-plugin.
1415

1516
#### Documentation
1617

apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/main/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class EventBusDispatchInterceptor implements InstanceMethodsAroundInterce
3434
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
3535
MethodInterceptResult result) throws Throwable {
3636
final Object event = allArguments[0];
37-
if (event != null) {
37+
if (event != null && ContextManager.isActive()) {
3838
allArguments[0] = EventWrapper.wrapEvent(event, ContextManager.capture());
3939
}
4040
}

apm-sniffer/apm-sdk-plugin/guava-eventbus-plugin/src/test/java/org/apache/skywalking/apm/plugin/guava/eventbus/EventBusDispatchInterceptorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void setUp() throws Exception {
5454
originalEventObj = new Object();
5555

5656
mockedContextManager = Mockito.mockStatic(ContextManager.class);
57+
mockedContextManager.when(ContextManager::isActive).thenReturn(true);
5758
mockedContextManager.when(ContextManager::capture).thenReturn(contextSnapshot);
5859
}
5960

test/plugin/scenarios/guava-eventbus-scenario/config/expectedData.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
segmentItems:
1717
- serviceName: guava-eventbus-scenario
18-
segmentSize: ge 2
18+
segmentSize: ge 3
1919
segments:
2020
- segmentId: not null
2121
spans:
@@ -66,4 +66,18 @@ segmentItems:
6666
refType: CrossThread, parentSpanId: 0, parentTraceSegmentId: not null,
6767
parentServiceInstance: not null, parentService: guava-eventbus-scenario,
6868
traceId: not null }
69+
- segmentId: not null
70+
spans:
71+
- operationName: Guava/EventBus/org.apache.skywalking.apm.testcase.guava.eventbus.service.SubscriberService/consumer
72+
operationId: 0
73+
parentSpanId: -1
74+
spanId: 0
75+
spanLayer: Unknown
76+
startTime: nq 0
77+
endTime: nq 0
78+
componentId: 123
79+
isError: false
80+
spanType: Local
81+
peer: ''
82+
skipAnalysis: false
6983
meterItems: []

test/plugin/scenarios/guava-eventbus-scenario/src/main/java/org/apache/skywalking/apm/testcase/guava/eventbus/controller/CaseController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public void testcase(HttpServletRequest request) throws Exception {
4646
asyncEventBus.register(subscriberService);
4747
final TestEvent testEvent = new TestEvent();
4848
testEvent.setContent("test");
49+
final TestEvent testEvent2 = new TestEvent();
50+
testEvent2.setContent("test2");
51+
new Thread(() -> asyncEventBus.post(testEvent2)).start();
4952
eventBus.post(testEvent);
5053
testEvent.setAsyncContext(request.startAsync());
5154
asyncEventBus.post(testEvent);

0 commit comments

Comments
 (0)