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 new file mode 100644 index 0000000000000000000000000000000000000000..f935c77eb985c37a32c825e02ecba1318d2fdab3 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandGetFallbackInterceptor.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.hystrix.v1; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +public class HystrixCommandGetFallbackInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField(); + ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot(); + + AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Fallback"); + activeSpan.setComponent(ComponentsDefine.HYSTRIX); + ContextManager.continued(snapshot); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..c281f59f98ca7f47eb91ac16774fd3079679e78a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/HystrixCommandRunInterceptor.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.hystrix.v1; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +public class HystrixCommandRunInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + // create a local span, and continued, The `execution method` running in other thread if the + // hystrix strategy is `THREAD`. + EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField(); + ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot(); + + AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Execution"); + activeSpan.setComponent(ComponentsDefine.HYSTRIX); + ContextManager.continued(snapshot); + // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing. + enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + } +} 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 dafba818acd338572d8fa520affaee87b53b4ae9..eedc646cd280452a553d101f6f6ae593e15dbdb3 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 @@ -18,13 +18,12 @@ package org.apache.skywalking.apm.plugin.hystrix.v1; +import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixInvokable; +import com.netflix.hystrix.exception.HystrixRuntimeException; import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook; import org.apache.skywalking.apm.agent.core.context.ContextManager; -import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; -import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; -import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; /** * {@link SWExecutionHookWrapper} wrapper the HystrixCommandExecutionHook object for tracing. @@ -48,57 +47,134 @@ public class SWExecutionHookWrapper extends HystrixCommandExecutionHook { @Override public void onExecutionStart(HystrixInvokable commandInstance) { - // create a local span, and continued, The `execution method` running in other thread if the - // hystrix strategy is `THREAD`. - EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance; - EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField(); - ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot(); - - AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Execution"); - activeSpan.setComponent(ComponentsDefine.HYSTRIX); - ContextManager.continued(snapshot); actual.onExecutionStart(commandInstance); - - // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing. - enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); } @Override public Exception onExecutionError(HystrixInvokable commandInstance, Exception e) { - ContextManager.activeSpan().errorOccurred().log(e); - ContextManager.stopSpan(); return actual.onExecutionError(commandInstance, e); } @Override public void onExecutionSuccess(HystrixInvokable commandInstance) { - ContextManager.stopSpan(); actual.onExecutionSuccess(commandInstance); } @Override public void onFallbackStart(HystrixInvokable commandInstance) { - EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance; - EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField(); - ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot(); - - AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Fallback"); - activeSpan.setComponent(ComponentsDefine.HYSTRIX); - ContextManager.continued(snapshot); - actual.onFallbackStart(commandInstance); } @Override public Exception onFallbackError(HystrixInvokable commandInstance, Exception e) { - ContextManager.activeSpan().errorOccurred().log(e); - ContextManager.stopSpan(); return actual.onFallbackError(commandInstance, e); } @Override public void onFallbackSuccess(HystrixInvokable commandInstance) { - ContextManager.stopSpan(); actual.onFallbackSuccess(commandInstance); } + + @Override public Exception onRunError(HystrixInvokable commandInstance, Exception e) { + return actual.onRunError(commandInstance, e); + } + + @Override public Exception onRunError(HystrixCommand commandInstance, Exception e) { + return actual.onRunError(commandInstance, e); + } + + @Override + public Exception onError(HystrixInvokable commandInstance, HystrixRuntimeException.FailureType failureType, + Exception e) { + return actual.onError(commandInstance, failureType, e); + } + + @Override public void onSuccess(HystrixInvokable commandInstance) { + actual.onSuccess(commandInstance); + } + + @Override public T onEmit(HystrixInvokable commandInstance, T value) { + return actual.onEmit(commandInstance, value); + } + + @Override public T onExecutionEmit(HystrixInvokable commandInstance, T value) { + return actual.onExecutionEmit(commandInstance, value); + } + + @Override public T onFallbackEmit(HystrixInvokable commandInstance, T value) { + return actual.onFallbackEmit(commandInstance, value); + } + + @Override public void onCacheHit(HystrixInvokable commandInstance) { + actual.onCacheHit(commandInstance); + } + + @Override public void onThreadComplete(HystrixInvokable commandInstance) { + actual.onThreadComplete(commandInstance); + } + + @Override public void onThreadStart(HystrixInvokable commandInstance) { + actual.onThreadStart(commandInstance); + } + + @Override + public Exception onError(HystrixCommand commandInstance, HystrixRuntimeException.FailureType failureType, + Exception e) { + return actual.onError(commandInstance, failureType, e); + } + + @Override public Exception onFallbackError(HystrixCommand commandInstance, Exception e) { + return actual.onFallbackError(commandInstance, e); + } + + @Override public T onComplete(HystrixCommand commandInstance, T response) { + return actual.onComplete(commandInstance, response); + } + + @Override public T onComplete(HystrixInvokable commandInstance, T response) { + return actual.onComplete(commandInstance, response); + } + + @Override public T onFallbackSuccess(HystrixCommand commandInstance, T fallbackResponse) { + return actual.onFallbackSuccess(commandInstance, fallbackResponse); + } + + @Override public T onFallbackSuccess(HystrixInvokable commandInstance, T fallbackResponse) { + return actual.onFallbackSuccess(commandInstance, fallbackResponse); + } + + @Override public T onRunSuccess(HystrixCommand commandInstance, T response) { + return actual.onRunSuccess(commandInstance, response); + } + + @Override public T onRunSuccess(HystrixInvokable commandInstance, T response) { + return actual.onRunSuccess(commandInstance, response); + } + + @Override public void onFallbackStart(HystrixCommand commandInstance) { + actual.onFallbackStart(commandInstance); + } + + @Override public void onRunStart(HystrixCommand commandInstance) { + actual.onRunStart(commandInstance); + } + + @Override public void onRunStart(HystrixInvokable commandInstance) { + actual.onRunStart(commandInstance); + } + + @Override public void onStart(HystrixCommand commandInstance) { + EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance; + EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField(); + enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture()); + actual.onStart(commandInstance); + } + + @Override public void onThreadComplete(HystrixCommand commandInstance) { + actual.onThreadComplete(commandInstance); + } + + @Override public void onThreadStart(HystrixCommand commandInstance) { + actual.onThreadStart(commandInstance); + } } diff --git a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixCommandInstrumentation.java b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixCommandInstrumentation.java index 54c2faf72d099eb271c7e08357fc9c850769d41f..fe23ef30de6d7912d5a0163d19869170c00742ea 100644 --- a/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixCommandInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/hystrix-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hystrix/v1/define/HystrixCommandInstrumentation.java @@ -26,6 +26,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInst import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch; /** @@ -55,7 +56,34 @@ public class HystrixCommandInstrumentation extends ClassInstanceMethodsEnhancePl } @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[0]; + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named("run"); + } + + @Override public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixCommandRunInterceptor"; + } + + @Override public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named("getFallback"); + } + + @Override public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixCommandGetFallbackInterceptor"; + } + + @Override public boolean isOverrideArgs() { + return false; + } + } + }; } @Override protected ClassMatch enhanceClass() {