提交 107eaacf 编写于 作者: A ascrutae

Adjust hystrix plugin buried point

上级 851ee0fe
/*
* 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);
}
}
/*
* 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);
}
}
......@@ -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 <T> void onExecutionStart(HystrixInvokable<T> 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 <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
ContextManager.activeSpan().errorOccurred().log(e);
ContextManager.stopSpan();
return actual.onExecutionError(commandInstance, e);
}
@Override
public <T> void onExecutionSuccess(HystrixInvokable<T> commandInstance) {
ContextManager.stopSpan();
actual.onExecutionSuccess(commandInstance);
}
@Override
public <T> void onFallbackStart(HystrixInvokable<T> 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 <T> Exception onFallbackError(HystrixInvokable<T> commandInstance, Exception e) {
ContextManager.activeSpan().errorOccurred().log(e);
ContextManager.stopSpan();
return actual.onFallbackError(commandInstance, e);
}
@Override
public <T> void onFallbackSuccess(HystrixInvokable<T> commandInstance) {
ContextManager.stopSpan();
actual.onFallbackSuccess(commandInstance);
}
@Override public <T> Exception onRunError(HystrixInvokable<T> commandInstance, Exception e) {
return actual.onRunError(commandInstance, e);
}
@Override public <T> Exception onRunError(HystrixCommand<T> commandInstance, Exception e) {
return actual.onRunError(commandInstance, e);
}
@Override
public <T> Exception onError(HystrixInvokable<T> commandInstance, HystrixRuntimeException.FailureType failureType,
Exception e) {
return actual.onError(commandInstance, failureType, e);
}
@Override public <T> void onSuccess(HystrixInvokable<T> commandInstance) {
actual.onSuccess(commandInstance);
}
@Override public <T> T onEmit(HystrixInvokable<T> commandInstance, T value) {
return actual.onEmit(commandInstance, value);
}
@Override public <T> T onExecutionEmit(HystrixInvokable<T> commandInstance, T value) {
return actual.onExecutionEmit(commandInstance, value);
}
@Override public <T> T onFallbackEmit(HystrixInvokable<T> commandInstance, T value) {
return actual.onFallbackEmit(commandInstance, value);
}
@Override public <T> void onCacheHit(HystrixInvokable<T> commandInstance) {
actual.onCacheHit(commandInstance);
}
@Override public <T> void onThreadComplete(HystrixInvokable<T> commandInstance) {
actual.onThreadComplete(commandInstance);
}
@Override public <T> void onThreadStart(HystrixInvokable<T> commandInstance) {
actual.onThreadStart(commandInstance);
}
@Override
public <T> Exception onError(HystrixCommand<T> commandInstance, HystrixRuntimeException.FailureType failureType,
Exception e) {
return actual.onError(commandInstance, failureType, e);
}
@Override public <T> Exception onFallbackError(HystrixCommand<T> commandInstance, Exception e) {
return actual.onFallbackError(commandInstance, e);
}
@Override public <T> T onComplete(HystrixCommand<T> commandInstance, T response) {
return actual.onComplete(commandInstance, response);
}
@Override public <T> T onComplete(HystrixInvokable<T> commandInstance, T response) {
return actual.onComplete(commandInstance, response);
}
@Override public <T> T onFallbackSuccess(HystrixCommand<T> commandInstance, T fallbackResponse) {
return actual.onFallbackSuccess(commandInstance, fallbackResponse);
}
@Override public <T> T onFallbackSuccess(HystrixInvokable<T> commandInstance, T fallbackResponse) {
return actual.onFallbackSuccess(commandInstance, fallbackResponse);
}
@Override public <T> T onRunSuccess(HystrixCommand<T> commandInstance, T response) {
return actual.onRunSuccess(commandInstance, response);
}
@Override public <T> T onRunSuccess(HystrixInvokable<T> commandInstance, T response) {
return actual.onRunSuccess(commandInstance, response);
}
@Override public <T> void onFallbackStart(HystrixCommand<T> commandInstance) {
actual.onFallbackStart(commandInstance);
}
@Override public <T> void onRunStart(HystrixCommand<T> commandInstance) {
actual.onRunStart(commandInstance);
}
@Override public <T> void onRunStart(HystrixInvokable<T> commandInstance) {
actual.onRunStart(commandInstance);
}
@Override public <T> void onStart(HystrixCommand<T> commandInstance) {
EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance;
EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField();
enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture());
actual.onStart(commandInstance);
}
@Override public <T> void onThreadComplete(HystrixCommand<T> commandInstance) {
actual.onThreadComplete(commandInstance);
}
@Override public <T> void onThreadStart(HystrixCommand<T> commandInstance) {
actual.onThreadStart(commandInstance);
}
}
......@@ -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<MethodDescription> 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<MethodDescription> 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() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册