提交 ef8b5aaa 编写于 作者: wu-sheng's avatar wu-sheng

Adjust the springMVC plugin.

上级 62817eed
package org.skywalking.apm.plugin.spring.mvc; 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.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import org.springframework.web.bind.annotation.RequestMapping; 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 { public class ControllerConstructorInterceptor implements InstanceConstructorInterceptor {
@Override @Override
...@@ -15,8 +26,7 @@ public class ControllerConstructorInterceptor implements InstanceConstructorInte ...@@ -15,8 +26,7 @@ public class ControllerConstructorInterceptor implements InstanceConstructorInte
if (basePathRequestMapping != null) { if (basePathRequestMapping != null) {
basePath = basePathRequestMapping.value()[0]; basePath = basePathRequestMapping.value()[0];
} }
Map<Object, String> cacheRequestPath = new HashMap<Object, String>(); PathMappingCache pathMappingCache = new PathMappingCache(basePath);
cacheRequestPath.put("BASE_PATH", basePath); objInst.setSkyWalkingDynamicField(pathMappingCache);
objInst.setSkyWalkingDynamicField(cacheRequestPath);
} }
} }
package org.skywalking.apm.plugin.spring.mvc; package org.skywalking.apm.plugin.spring.mvc;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.skywalking.apm.agent.core.conf.Config; import org.skywalking.apm.agent.core.conf.Config;
...@@ -18,16 +17,20 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -18,16 +17,20 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; 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 { public class ControllerServiceMethodInterceptor 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 {
Map<Object, String> cacheRequestURL = (Map<Object, String>)objInst.getSkyWalkingDynamicField(); PathMappingCache pathMappingCache = (PathMappingCache)objInst.getSkyWalkingDynamicField();
String requestURL = cacheRequestURL.get(method); String requestURL = pathMappingCache.findPathMapping(method);
if (requestURL == null) { if (requestURL == null) {
requestURL = new String(cacheRequestURL.get("BASE_PATH")); requestURL = method.getAnnotation(RequestMapping.class).value()[0];
requestURL += method.getAnnotation(RequestMapping.class).value()[0]; pathMappingCache.addPathMapping(method, requestURL.toString());
cacheRequestURL.put(method, requestURL.toString());
} }
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
......
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册