提交 1c3606a8 编写于 作者: X Xin,Zhang 提交者: wu-sheng

Change the way to transmit the Request and Response (#1471)

上级 06b6f054
...@@ -19,14 +19,12 @@ ...@@ -19,14 +19,12 @@
package org.apache.skywalking.apm.plugin.spring.mvc.v3; package org.apache.skywalking.apm.plugin.spring.mvc.v3;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; 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.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT;
/** /**
* {@link HandlerMethodInvokerInterceptor} pass the {@link NativeWebRequest} object into the {@link * {@link HandlerMethodInvokerInterceptor} pass the {@link NativeWebRequest} object into the {@link
* org.springframework.stereotype.Controller} object. * org.springframework.stereotype.Controller} object.
...@@ -39,7 +37,7 @@ public class HandlerMethodInvokerInterceptor implements InstanceMethodsAroundInt ...@@ -39,7 +37,7 @@ public class HandlerMethodInvokerInterceptor implements InstanceMethodsAroundInt
MethodInterceptResult result) throws Throwable { MethodInterceptResult result) throws Throwable {
Object handler = allArguments[1]; Object handler = allArguments[1];
if (handler instanceof EnhancedInstance) { if (handler instanceof EnhancedInstance) {
ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, ((NativeWebRequest)allArguments[2]).getNativeResponse()); ((EnhanceRequireObjectCache)((EnhancedInstance)handler).getSkyWalkingDynamicField()).setNativeWebRequest((NativeWebRequest)allArguments[2]);
} }
} }
......
...@@ -61,4 +61,12 @@ public class GetBeanInterceptorTest { ...@@ -61,4 +61,12 @@ public class GetBeanInterceptorTest {
verify(enhanceRet, times(0)).setSkyWalkingDynamicField(Matchers.any()); verify(enhanceRet, times(0)).setSkyWalkingDynamicField(Matchers.any());
} }
@Test
public void testResultIsEnhanceInstance() throws Throwable {
interceptor.afterMethod(enhancedInstance, null, null, null, enhanceRet);
verify(enhanceRet, times(0)).setSkyWalkingDynamicField(Matchers.any());
}
} }
...@@ -174,9 +174,9 @@ public class RequestMappingMethodInterceptorTest { ...@@ -174,9 +174,9 @@ public class RequestMappingMethodInterceptorTest {
@Override @Override
public Object getSkyWalkingDynamicField() { public Object getSkyWalkingDynamicField() {
value.setPathMappingCache(new PathMappingCache("/test")); value.setPathMappingCache(new PathMappingCache("/test"));
value.setHttpResponse(response);
value.setNativeWebRequest(nativeWebRequest);
return value; return value;
} }
......
...@@ -306,6 +306,8 @@ public class RestMappingMethodInterceptorTest { ...@@ -306,6 +306,8 @@ public class RestMappingMethodInterceptorTest {
@Override @Override
public Object getSkyWalkingDynamicField() { public Object getSkyWalkingDynamicField() {
value.setPathMappingCache(new PathMappingCache("/test")); value.setPathMappingCache(new PathMappingCache("/test"));
value.setHttpResponse(response);
value.setNativeWebRequest(nativeWebRequest);
return value; return value;
} }
......
...@@ -21,15 +21,12 @@ package org.apache.skywalking.apm.plugin.spring.mvc.v4; ...@@ -21,15 +21,12 @@ package org.apache.skywalking.apm.plugin.spring.mvc.v4;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants;
public final class SpringTestCaseHelper { public final class SpringTestCaseHelper {
public final static void createCaseHandler(HttpServletRequest request, HttpServletResponse response, public final static void createCaseHandler(HttpServletRequest request, HttpServletResponse response,
CaseHandler a) throws Throwable { CaseHandler a) throws Throwable {
ContextManager.createLocalSpan("For-Test"); ContextManager.createLocalSpan("For-Test");
ContextManager.getRuntimeContext().put(Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT, request);
ContextManager.getRuntimeContext().put(Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT, response);
a.handleCase(); a.handleCase();
ContextManager.stopSpan(); ContextManager.stopSpan();
} }
......
...@@ -34,10 +34,6 @@ public class Constants { ...@@ -34,10 +34,6 @@ public class Constants {
public static final String HYSTRIX_COMMAND_ANNOTATION = "com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand"; public static final String HYSTRIX_COMMAND_ANNOTATION = "com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand";
public static final String REQUEST_KEY_IN_RUNTIME_CONTEXT = "SW_REQUEST";
public static final String RESPONSE_KEY_IN_RUNTIME_CONTEXT = "SW_RESPONSE";
public static final String ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT = "ISOLATE_STRATEGY"; public static final String ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT = "ISOLATE_STRATEGY";
public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG";
......
...@@ -18,15 +18,28 @@ ...@@ -18,15 +18,28 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons; package org.apache.skywalking.apm.plugin.spring.mvc.commons;
import org.springframework.web.context.request.NativeWebRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class EnhanceRequireObjectCache { public class EnhanceRequireObjectCache {
private PathMappingCache pathMappingCache; private PathMappingCache pathMappingCache;
private ThreadLocal<NativeWebRequest> nativeWebRequest = new ThreadLocal<NativeWebRequest>();
private ThreadLocal<HttpServletResponse> httpResponse = new ThreadLocal<HttpServletResponse>();
public void setPathMappingCache(PathMappingCache pathMappingCache) { public void setPathMappingCache(PathMappingCache pathMappingCache) {
this.pathMappingCache = pathMappingCache; this.pathMappingCache = pathMappingCache;
} }
public HttpServletResponse getHttpServletResponse() {
return httpResponse.get() == null ? (HttpServletResponse) nativeWebRequest.get().getNativeResponse() : httpResponse.get();
}
public void setNativeWebRequest(NativeWebRequest nativeWebRequest) {
this.nativeWebRequest.set(nativeWebRequest);
}
public String findPathMapping(Method method) { public String findPathMapping(Method method) {
return pathMappingCache.findPathMapping(method); return pathMappingCache.findPathMapping(method);
} }
...@@ -38,4 +51,14 @@ public class EnhanceRequireObjectCache { ...@@ -38,4 +51,14 @@ public class EnhanceRequireObjectCache {
public PathMappingCache getPathMappingCache() { public PathMappingCache getPathMappingCache() {
return pathMappingCache; return pathMappingCache;
} }
public void setHttpResponse(HttpServletResponse httpResponse) {
this.httpResponse.set(httpResponse);
}
public void clearRequestAndResponse() {
setNativeWebRequest(null);
setHttpResponse(null);
}
} }
...@@ -32,11 +32,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM ...@@ -32,11 +32,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT;
/** /**
* the abstract method inteceptor * the abstract method inteceptor
...@@ -66,7 +66,7 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround ...@@ -66,7 +66,7 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround
} }
String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT);
HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
if (hystrixIsolateStrategy != null) { if (hystrixIsolateStrategy != null) {
ContextManager.createLocalSpan(requestURL); ContextManager.createLocalSpan(requestURL);
...@@ -99,17 +99,20 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround ...@@ -99,17 +99,20 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround
} }
String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT); String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT);
HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); HttpServletResponse response = ((EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField()).getHttpServletResponse();
try {
if (hystrixIsolateStrategy != null) { if (hystrixIsolateStrategy != null) {
ContextManager.stopSpan(); ContextManager.stopSpan();
} else if (response != null) { } else if (response != null) {
AbstractSpan span = ContextManager.activeSpan(); AbstractSpan span = ContextManager.activeSpan();
if (response.getStatus() >= 400) { if (response.getStatus() >= 400) {
span.errorOccurred(); span.errorOccurred();
Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus()));
}
ContextManager.stopSpan();
} }
ContextManager.stopSpan(); } finally {
((EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField()).clearRequestAndResponse();
} }
return ret; return ret;
......
...@@ -19,15 +19,11 @@ ...@@ -19,15 +19,11 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; 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.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT;
/** /**
* {@link GetBeanInterceptor} pass the {@link NativeWebRequest} object into the {@link * {@link GetBeanInterceptor} pass the {@link NativeWebRequest} object into the {@link
...@@ -45,7 +41,7 @@ public class GetBeanInterceptor implements InstanceMethodsAroundInterceptor { ...@@ -45,7 +41,7 @@ public class GetBeanInterceptor implements InstanceMethodsAroundInterceptor {
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable { Object ret) throws Throwable {
if (ret instanceof EnhancedInstance) { if (ret instanceof EnhancedInstance) {
ContextManager.getRuntimeContext().put(REQUEST_KEY_IN_RUNTIME_CONTEXT, ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest()); ((EnhanceRequireObjectCache)((EnhancedInstance)ret).getSkyWalkingDynamicField()).setNativeWebRequest((NativeWebRequest)objInst.getSkyWalkingDynamicField());
} }
return ret; return ret;
} }
......
...@@ -16,19 +16,14 @@ ...@@ -16,19 +16,14 @@
* *
*/ */
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
import org.apache.skywalking.apm.agent.core.context.ContextManager; import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; 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.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import java.lang.reflect.Method;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT;
/** /**
* {@link InvokeForRequestInterceptor} pass the {@link NativeWebRequest} object into the {@link * {@link InvokeForRequestInterceptor} pass the {@link NativeWebRequest} object into the {@link
* org.springframework.stereotype.Controller} object. * org.springframework.stereotype.Controller} object.
...@@ -39,7 +34,7 @@ public class InvokeForRequestInterceptor implements InstanceMethodsAroundInterce ...@@ -39,7 +34,7 @@ public class InvokeForRequestInterceptor implements InstanceMethodsAroundInterce
@Override @Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable { MethodInterceptResult result) throws Throwable {
ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, ((NativeWebRequest)allArguments[0]).getNativeResponse()); objInst.setSkyWalkingDynamicField(allArguments[0]);
} }
@Override @Override
......
...@@ -19,21 +19,18 @@ ...@@ -19,21 +19,18 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.context.ContextManager; import javax.servlet.http.HttpServletResponse;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; 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.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT;
import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT;
public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInterceptor { public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override @Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable { MethodInterceptResult result) throws Throwable {
if (allArguments[2] instanceof EnhancedInstance) { if (allArguments[2] instanceof EnhancedInstance) {
ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, allArguments[1]); ((EnhanceRequireObjectCache)((EnhancedInstance)allArguments[2]).getSkyWalkingDynamicField()).setHttpResponse((HttpServletResponse)allArguments[1]);
ContextManager.getRuntimeContext().put(REQUEST_KEY_IN_RUNTIME_CONTEXT, allArguments[0]);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册