Skip to content

Commit

Permalink
Refactor the interceptor method parameter list for SpringMVC plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-sheng committed Aug 10, 2017
1 parent bfed002 commit 992032c
Show file tree
Hide file tree
Showing 41 changed files with 266 additions and 185 deletions.
Expand Up @@ -57,7 +57,7 @@ public Object intercept(@This Object obj,

MethodInterceptResult result = new MethodInterceptResult();
try {
interceptor.beforeMethod(targetObject, method.getName(), allArguments, method.getParameterTypes(),
interceptor.beforeMethod(targetObject, method, allArguments, method.getParameterTypes(),
result);
} catch (Throwable t) {
logger.error(t, "class[{}] before method[{}] intercept failure", obj.getClass(), method.getName());
Expand All @@ -72,15 +72,15 @@ public Object intercept(@This Object obj,
}
} catch (Throwable t) {
try {
interceptor.handleMethodException(targetObject, method.getName(), allArguments, method.getParameterTypes(),
interceptor.handleMethodException(targetObject, method, allArguments, method.getParameterTypes(),
t);
} catch (Throwable t2) {
logger.error(t2, "class[{}] handle method[{}] exception failure", obj.getClass(), method.getName());
}
throw t;
} finally {
try {
ret = interceptor.afterMethod(targetObject, method.getName(), allArguments, method.getParameterTypes(),
ret = interceptor.afterMethod(targetObject, method, allArguments, method.getParameterTypes(),
ret);
} catch (Throwable t) {
logger.error(t, "class[{}] after method[{}] intercept failure", obj.getClass(), method.getName());
Expand Down
Expand Up @@ -59,7 +59,7 @@ public Object intercept(@This Object obj,

MethodInterceptResult result = new MethodInterceptResult();
try {
interceptor.beforeMethod(targetObject, method.getName(), allArguments, method.getParameterTypes(),
interceptor.beforeMethod(targetObject, method, allArguments, method.getParameterTypes(),
result);
} catch (Throwable t) {
logger.error(t, "class[{}] before method[{}] intercept failure", obj.getClass(), method.getName());
Expand All @@ -74,15 +74,15 @@ public Object intercept(@This Object obj,
}
} catch (Throwable t) {
try {
interceptor.handleMethodException(targetObject, method.getName(), allArguments, method.getParameterTypes(),
interceptor.handleMethodException(targetObject, method, allArguments, method.getParameterTypes(),
t);
} catch (Throwable t2) {
logger.error(t2, "class[{}] handle method[{}] exception failure", obj.getClass(), method.getName());
}
throw t;
} finally {
try {
ret = interceptor.afterMethod(targetObject, method.getName(), allArguments, method.getParameterTypes(),
ret = interceptor.afterMethod(targetObject, method, allArguments, method.getParameterTypes(),
ret);
} catch (Throwable t) {
logger.error(t, "class[{}] after method[{}] intercept failure", obj.getClass(), method.getName());
Expand Down
@@ -1,5 +1,7 @@
package org.skywalking.apm.agent.core.plugin.interceptor.enhance;

import java.lang.reflect.Method;

/**
* A interceptor, which intercept method's invocation. The target methods will be defined in {@link
* ClassEnhancePluginDefine}'s subclass, most likely in {@link ClassInstanceMethodsEnhancePluginDefine}
Expand All @@ -10,27 +12,32 @@ public interface InstanceMethodsAroundInterceptor {
/**
* called before target method invocation.
*
*
* @param method
* @param result change this result, if you want to truncate the method.
* @throws Throwable
*/
void beforeMethod(EnhancedInstance objInst, String methodName, Object[] allArguments, Class<?>[] argumentsTypes,
void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable;

/**
* called after target method invocation. Even method's invocation triggers an exception.
*
*
* @param method
* @param ret the method's original return value.
* @return the method's actual return value.
* @throws Throwable
*/
Object afterMethod(EnhancedInstance objInst, String methodName, Object[] allArguments, Class<?>[] argumentsTypes,
Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable;

/**
* called when occur exception.
*
* @param method
* @param t the exception occur.
*/
void handleMethodException(EnhancedInstance objInst, String methodName, Object[] allArguments, Class<?>[] argumentsTypes,
void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Throwable t);
}
@@ -1,5 +1,7 @@
package org.skywalking.apm.agent.core.plugin.interceptor.enhance;

import java.lang.reflect.Method;

/**
* The static method's interceptor interface.
* Any plugin, which wants to intercept static methods, must implement this interface.
Expand All @@ -10,24 +12,28 @@ public interface StaticMethodsAroundInterceptor {
/**
* called before target method invocation.
*
* @param method
* @param result change this result, if you want to truncate the method.
*/
void beforeMethod(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes,
void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
MethodInterceptResult result);

/**
* called after target method invocation. Even method's invocation triggers an exception.
*
*
* @param method
* @param ret the method's original return value.
* @return the method's actual return value.
*/
Object afterMethod(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes, Object ret);
Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret);

/**
* called when occur exception.
*
* @param method
* @param t the exception occur.
*/
void handleMethodException(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes,
void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
Throwable t);
}
Expand Up @@ -55,7 +55,7 @@ public Object intercept(@Origin Class<?> clazz, @AllArguments Object[] allArgume

MethodInterceptResult result = new MethodInterceptResult();
try {
interceptor.beforeMethod(clazz, method.getName(), allArguments, method.getParameterTypes(), result);
interceptor.beforeMethod(clazz, method, allArguments, method.getParameterTypes(), result);
} catch (Throwable t) {
logger.error(t, "class[{}] before static method[{}] intercept failure", clazz, method.getName());
}
Expand All @@ -69,14 +69,14 @@ public Object intercept(@Origin Class<?> clazz, @AllArguments Object[] allArgume
}
} catch (Throwable t) {
try {
interceptor.handleMethodException(clazz, method.getName(), allArguments, method.getParameterTypes(), t);
interceptor.handleMethodException(clazz, method, allArguments, method.getParameterTypes(), t);
} catch (Throwable t2) {
logger.error(t2, "class[{}] handle static method[{}] exception failure", clazz, method.getName(), t2.getMessage());
}
throw t;
} finally {
try {
ret = interceptor.afterMethod(clazz, method.getName(), allArguments, method.getParameterTypes(), ret);
ret = interceptor.afterMethod(clazz, method, allArguments, method.getParameterTypes(), ret);
} catch (Throwable t) {
logger.error(t, "class[{}] after static method[{}] intercept failure:{}", clazz, method.getName(), t.getMessage());
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public Object intercept(@Origin Class<?> clazz, @AllArguments Object[] allArgume

MethodInterceptResult result = new MethodInterceptResult();
try {
interceptor.beforeMethod(clazz, method.getName(), allArguments, method.getParameterTypes(), result);
interceptor.beforeMethod(clazz, method, allArguments, method.getParameterTypes(), result);
} catch (Throwable t) {
logger.error(t, "class[{}] before static method[{}] intercept failure", clazz, method.getName());
}
Expand All @@ -67,14 +67,14 @@ public Object intercept(@Origin Class<?> clazz, @AllArguments Object[] allArgume
}
} catch (Throwable t) {
try {
interceptor.handleMethodException(clazz, method.getName(), allArguments, method.getParameterTypes(), t);
interceptor.handleMethodException(clazz, method, allArguments, method.getParameterTypes(), t);
} catch (Throwable t2) {
logger.error(t2, "class[{}] handle static method[{}] exception failure", clazz, method.getName(), t2.getMessage());
}
throw t;
} finally {
try {
ret = interceptor.afterMethod(clazz, method.getName(), allArguments, method.getParameterTypes(), ret);
ret = interceptor.afterMethod(clazz, method, allArguments, method.getParameterTypes(), ret);
} catch (Throwable t) {
logger.error(t, "class[{}] after static method[{}] intercept failure:{}", clazz, method.getName(), t.getMessage());
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import java.lang.reflect.Method;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class DubboInterceptor implements InstanceMethodsAroundInterceptor {
* {@link RpcContext#attachments}. current trace segment will ref if the serialize context data is not null.
*/
@Override
public void beforeMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Invoker invoker = (Invoker)allArguments[0];
Invocation invocation = (Invocation)allArguments[1];
Expand Down Expand Up @@ -80,7 +81,7 @@ public void beforeMethod(EnhancedInstance objInst, String methodName, Object[] a
}

@Override
public Object afterMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
Result result = (Result)ret;
if (result != null && result.getException() != null) {
Expand All @@ -92,7 +93,7 @@ public Object afterMethod(EnhancedInstance objInst, String methodName, Object[]
}

@Override
public void handleMethodException(EnhancedInstance objInst, String methodName, Object[] allArguments,
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
dealException(t);
}
Expand Down
Expand Up @@ -97,8 +97,8 @@ public void setUp() throws Exception {
public void testConsumerBelow283() throws Throwable {
BugFixActive.active();

dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);

assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
Expand All @@ -113,8 +113,8 @@ public void testConsumerBelow283() throws Throwable {

@Test
public void testConsumerWithAttachment() throws Throwable {
dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);

assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
Expand All @@ -125,9 +125,9 @@ public void testConsumerWithAttachment() throws Throwable {

@Test
public void testConsumerWithException() throws Throwable {
dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.handleMethodException(enhancedInstance, "invoke", allArguments, argumentTypes, new RuntimeException());
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.handleMethodException(enhancedInstance, null, allArguments, argumentTypes, new RuntimeException());
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
assertConsumerTraceSegmentInErrorCase(traceSegment);
Expand All @@ -137,8 +137,8 @@ public void testConsumerWithException() throws Throwable {
public void testConsumerWithResultHasException() throws Throwable {
when(result.getException()).thenReturn(new RuntimeException());

dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);

assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
Expand All @@ -150,8 +150,8 @@ public void testProviderWithAttachment() throws Throwable {
when(rpcContext.isConsumerSide()).thenReturn(false);
when(rpcContext.getAttachment(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("#AQA*#AQA*4WcWe0tQNQA*|3|1|1|#192.168.1.8 :18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*");

dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);
assertProvider();
}

Expand All @@ -162,8 +162,8 @@ public void testProviderBelow283() throws Throwable {

testParam.setTraceContext("#AQA*#AQA*4WcWe0tQNQA*|3|1|1|#192.168.1.8 :18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*");

dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
dubboInterceptor.beforeMethod(enhancedInstance, null, allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, null, allArguments, argumentTypes, result);

assertProvider();
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import feign.Request;
import feign.Response;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -36,10 +37,12 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
* port, kind, component, url from {@link feign.Request}.
* Through the reflection of the way, set the http header of context data into {@link feign.Request#headers}.
*
*
* @param method
* @param result change this result, if you want to truncate the method.
* @throws Throwable
*/
@Override public void beforeMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Request request = (Request)allArguments[0];

Expand Down Expand Up @@ -73,11 +76,13 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
* the server.
* Finish the {@link AbstractSpan}.
*
*
* @param method
* @param ret the method's original return value.
* @return
* @throws Throwable
*/
@Override public Object afterMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
Response response = (Response)ret;
int statusCode = response.status();
Expand All @@ -93,7 +98,7 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
return ret;
}

@Override public void handleMethodException(EnhancedInstance objInst, String methodName, Object[] allArguments,
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
AbstractSpan activeSpan = ContextManager.activeSpan();
activeSpan.log(t);
Expand Down

0 comments on commit 992032c

Please sign in to comment.