Adjust the springMVC plugin.
This commit is contained in:
parent
62817eedf7
commit
ef8b5aaad1
|
|
@ -1,11 +1,22 @@
|
|||
package org.skywalking.apm.plugin.spring.mvc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* The <code>ControllerConstructorInterceptor</code> intercepts the Controller's constructor, in order to acquire the
|
||||
* mapping annotation, if exist.
|
||||
*
|
||||
* But, you can see we only use the first mapping value, <B>Why?</B>
|
||||
*
|
||||
* Right now, we intercept the controller by annotation as you known, so we CAN'T know which uri patten is actually
|
||||
* matched. Even we know, that costs a lot.
|
||||
*
|
||||
* If we want to resolve that, we must intercept the Spring MVC core codes, that is not a good choice for now.
|
||||
*
|
||||
* Comment by @wu-sheng
|
||||
*/
|
||||
public class ControllerConstructorInterceptor implements InstanceConstructorInterceptor {
|
||||
|
||||
@Override
|
||||
|
|
@ -15,8 +26,7 @@ public class ControllerConstructorInterceptor implements InstanceConstructorInte
|
|||
if (basePathRequestMapping != null) {
|
||||
basePath = basePathRequestMapping.value()[0];
|
||||
}
|
||||
Map<Object, String> cacheRequestPath = new HashMap<Object, String>();
|
||||
cacheRequestPath.put("BASE_PATH", basePath);
|
||||
objInst.setSkyWalkingDynamicField(cacheRequestPath);
|
||||
PathMappingCache pathMappingCache = new PathMappingCache(basePath);
|
||||
objInst.setSkyWalkingDynamicField(pathMappingCache);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package org.skywalking.apm.plugin.spring.mvc;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.skywalking.apm.agent.core.conf.Config;
|
||||
|
|
@ -18,16 +17,20 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/**
|
||||
* The <code>ControllerServiceMethodInterceptor</code> only use the first mapping value.
|
||||
*
|
||||
* @See {@link ControllerConstructorInterceptor} to explain why we are doing this.
|
||||
*/
|
||||
public class ControllerServiceMethodInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
Map<Object, String> cacheRequestURL = (Map<Object, String>)objInst.getSkyWalkingDynamicField();
|
||||
String requestURL = cacheRequestURL.get(method);
|
||||
PathMappingCache pathMappingCache = (PathMappingCache)objInst.getSkyWalkingDynamicField();
|
||||
String requestURL = pathMappingCache.findPathMapping(method);
|
||||
if (requestURL == null) {
|
||||
requestURL = new String(cacheRequestURL.get("BASE_PATH"));
|
||||
requestURL += method.getAnnotation(RequestMapping.class).value()[0];
|
||||
cacheRequestURL.put(method, requestURL.toString());
|
||||
requestURL = method.getAnnotation(RequestMapping.class).value()[0];
|
||||
pathMappingCache.addPathMapping(method, requestURL.toString());
|
||||
}
|
||||
|
||||
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package org.skywalking.apm.plugin.spring.mvc;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* The <code>PathMappingCache</code> represents a field
|
||||
*
|
||||
*
|
||||
* @author wusheng
|
||||
*/
|
||||
public class PathMappingCache {
|
||||
private String classPath = "";
|
||||
|
||||
private ConcurrentHashMap<Method, String> methodPathMapping = new ConcurrentHashMap<Method, String>();
|
||||
|
||||
public PathMappingCache(String classPath) {
|
||||
this.classPath = classPath;
|
||||
}
|
||||
|
||||
public String findPathMapping(Method method) {
|
||||
return methodPathMapping.get(method);
|
||||
}
|
||||
|
||||
public void addPathMapping(Method method, String methodPath) {
|
||||
methodPathMapping.put(method, classPath + methodPath);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue