diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerConstructorInterceptor.java index 8900112d5473eaded5a0dcb284da8a69357cc7ba..5f3d39ed04396a6803dda0f3defd6b0b91acccb5 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerConstructorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerConstructorInterceptor.java @@ -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 ControllerConstructorInterceptor 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, Why? + * + * 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 cacheRequestPath = new HashMap(); - cacheRequestPath.put("BASE_PATH", basePath); - objInst.setSkyWalkingDynamicField(cacheRequestPath); + PathMappingCache pathMappingCache = new PathMappingCache(basePath); + objInst.setSkyWalkingDynamicField(pathMappingCache); } } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerServiceMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerServiceMethodInterceptor.java index 91731648afd276c852eb3693ab260ad1c2fb71b9..32b4cb42d5058c9aaeecd72fbc698fd901985c74 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerServiceMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/ControllerServiceMethodInterceptor.java @@ -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 ControllerServiceMethodInterceptor 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 cacheRequestURL = (Map)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(); diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/PathMappingCache.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/PathMappingCache.java new file mode 100644 index 0000000000000000000000000000000000000000..5918914334f5944b4c0249c4f4d37e69d192d081 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/PathMappingCache.java @@ -0,0 +1,28 @@ +package org.skywalking.apm.plugin.spring.mvc; + +import java.lang.reflect.Method; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The PathMappingCache represents a field + * + * + * @author wusheng + */ +public class PathMappingCache { + private String classPath = ""; + + private ConcurrentHashMap methodPathMapping = new ConcurrentHashMap(); + + 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); + } +}