Change the way to transmit the Request and Response (#1471)
This commit is contained in:
parent
06b6f05438
commit
1c3606a832
|
|
@ -19,14 +19,12 @@
|
|||
package org.apache.skywalking.apm.plugin.spring.mvc.v3;
|
||||
|
||||
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.InstanceMethodsAroundInterceptor;
|
||||
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 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
|
||||
* org.springframework.stereotype.Controller} object.
|
||||
|
|
@ -39,7 +37,7 @@ public class HandlerMethodInvokerInterceptor implements InstanceMethodsAroundInt
|
|||
MethodInterceptResult result) throws Throwable {
|
||||
Object handler = allArguments[1];
|
||||
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 {
|
|||
|
||||
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 {
|
|||
|
||||
@Override
|
||||
public Object getSkyWalkingDynamicField() {
|
||||
|
||||
value.setPathMappingCache(new PathMappingCache("/test"));
|
||||
|
||||
value.setHttpResponse(response);
|
||||
value.setNativeWebRequest(nativeWebRequest);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -306,6 +306,8 @@ public class RestMappingMethodInterceptorTest {
|
|||
@Override
|
||||
public Object getSkyWalkingDynamicField() {
|
||||
value.setPathMappingCache(new PathMappingCache("/test"));
|
||||
value.setHttpResponse(response);
|
||||
value.setNativeWebRequest(nativeWebRequest);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,15 +21,12 @@ package org.apache.skywalking.apm.plugin.spring.mvc.v4;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
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 static void createCaseHandler(HttpServletRequest request, HttpServletResponse response,
|
||||
CaseHandler a) throws Throwable {
|
||||
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();
|
||||
ContextManager.stopSpan();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 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 FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG";
|
||||
|
|
|
|||
|
|
@ -18,15 +18,28 @@
|
|||
|
||||
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;
|
||||
|
||||
public class EnhanceRequireObjectCache {
|
||||
private PathMappingCache pathMappingCache;
|
||||
private ThreadLocal<NativeWebRequest> nativeWebRequest = new ThreadLocal<NativeWebRequest>();
|
||||
private ThreadLocal<HttpServletResponse> httpResponse = new ThreadLocal<HttpServletResponse>();
|
||||
|
||||
public void setPathMappingCache(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) {
|
||||
return pathMappingCache.findPathMapping(method);
|
||||
}
|
||||
|
|
@ -38,4 +51,14 @@ public class EnhanceRequireObjectCache {
|
|||
public PathMappingCache getPathMappingCache() {
|
||||
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
|
|||
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.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.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
|
||||
|
|
@ -66,7 +66,7 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround
|
|||
}
|
||||
|
||||
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) {
|
||||
ContextManager.createLocalSpan(requestURL);
|
||||
|
|
@ -99,17 +99,20 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround
|
|||
}
|
||||
|
||||
String hystrixIsolateStrategy = (String)ContextManager.getRuntimeContext().get(ISOLATE_STRATEGY_KEY_IN_RUNNING_CONTEXT);
|
||||
HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT);
|
||||
|
||||
if (hystrixIsolateStrategy != null) {
|
||||
ContextManager.stopSpan();
|
||||
} else if (response != null) {
|
||||
AbstractSpan span = ContextManager.activeSpan();
|
||||
if (response.getStatus() >= 400) {
|
||||
span.errorOccurred();
|
||||
Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus()));
|
||||
HttpServletResponse response = ((EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField()).getHttpServletResponse();
|
||||
try {
|
||||
if (hystrixIsolateStrategy != null) {
|
||||
ContextManager.stopSpan();
|
||||
} else if (response != null) {
|
||||
AbstractSpan span = ContextManager.activeSpan();
|
||||
if (response.getStatus() >= 400) {
|
||||
span.errorOccurred();
|
||||
Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus()));
|
||||
}
|
||||
ContextManager.stopSpan();
|
||||
}
|
||||
ContextManager.stopSpan();
|
||||
} finally {
|
||||
((EnhanceRequireObjectCache)objInst.getSkyWalkingDynamicField()).clearRequestAndResponse();
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
|
|
@ -19,15 +19,11 @@
|
|||
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
|
||||
|
||||
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.InstanceMethodsAroundInterceptor;
|
||||
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.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
|
||||
|
|
@ -45,7 +41,7 @@ public class GetBeanInterceptor implements InstanceMethodsAroundInterceptor {
|
|||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
Object ret) throws Throwable {
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,19 +16,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
|
||||
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.InstanceMethodsAroundInterceptor;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
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
|
||||
* org.springframework.stereotype.Controller} object.
|
||||
|
|
@ -39,7 +34,7 @@ public class InvokeForRequestInterceptor implements InstanceMethodsAroundInterce
|
|||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, ((NativeWebRequest)allArguments[0]).getNativeResponse());
|
||||
objInst.setSkyWalkingDynamicField(allArguments[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -19,21 +19,18 @@
|
|||
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
|
||||
|
||||
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.InstanceMethodsAroundInterceptor;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
|
||||
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;
|
||||
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
|
||||
|
||||
public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
if (allArguments[2] instanceof EnhancedInstance) {
|
||||
ContextManager.getRuntimeContext().put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, allArguments[1]);
|
||||
ContextManager.getRuntimeContext().put(REQUEST_KEY_IN_RUNTIME_CONTEXT, allArguments[0]);
|
||||
((EnhanceRequireObjectCache)((EnhancedInstance)allArguments[2]).getSkyWalkingDynamicField()).setHttpResponse((HttpServletResponse)allArguments[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue