From a960272f7148eb2a620a1f50276469f4b7b17d4e Mon Sep 17 00:00:00 2001 From: "Xin,Zhang" Date: Thu, 31 May 2018 08:48:19 +0800 Subject: [PATCH] Fix the issue that Hystrix plugin case NPE (#1293) --- .../hystrix/v1/HystrixCommandGetFallbackInterceptor.java | 4 +++- .../plugin/hystrix/v1/HystrixCommandRunInterceptor.java | 4 +++- .../apm/plugin/hystrix/v1/SWExecutionHookWrapper.java | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandGetFallbackInterceptor.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandGetFallbackInterceptor.java index f935c77eb..2dd1f4f9c 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandGetFallbackInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandGetFallbackInterceptor.java @@ -36,7 +36,9 @@ public class HystrixCommandGetFallbackInterceptor implements InstanceMethodsArou AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Fallback"); activeSpan.setComponent(ComponentsDefine.HYSTRIX); - ContextManager.continued(snapshot); + if (snapshot != null) { + ContextManager.continued(snapshot); + } } @Override diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java index c6f1bcc97..28dce1a39 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java @@ -41,7 +41,9 @@ public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterc AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Execution"); activeSpan.setComponent(ComponentsDefine.HYSTRIX); - ContextManager.continued(snapshot); + if (snapshot != null) { + ContextManager.continued(snapshot); + } // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing. enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWExecutionHookWrapper.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWExecutionHookWrapper.java index eedc646cd..fbe0022a0 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWExecutionHookWrapper.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/SWExecutionHookWrapper.java @@ -41,7 +41,9 @@ public class SWExecutionHookWrapper extends HystrixCommandExecutionHook { public void onStart(HystrixInvokable commandInstance) { EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance; EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField(); - enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + if (ContextManager.isActive()) { + enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + } actual.onStart(commandInstance); } @@ -166,7 +168,9 @@ public class SWExecutionHookWrapper extends HystrixCommandExecutionHook { @Override public void onStart(HystrixCommand commandInstance) { EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance; EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField(); - enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + if (ContextManager.isActive()) { + enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + } actual.onStart(commandInstance); }