Skip to content

Commit

Permalink
Merge pull request #491 from clevertension/my-main
Browse files Browse the repository at this point in the history
add rest mapping such as GetMapping
  • Loading branch information
wu-sheng committed Oct 8, 2017
2 parents a2d2f41 + 423efc1 commit 8eed412
Show file tree
Hide file tree
Showing 11 changed files with 799 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ target/
.DS_Store
*~
packages/
**/dependency-reduced-pom.xml
@@ -1,8 +1,5 @@
package org.skywalking.apm.plugin.spring.mvc;

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.skywalking.apm.agent.core.context.CarrierItem;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
Expand All @@ -13,33 +10,30 @@
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
* The <code>ControllerServiceMethodInterceptor</code> only use the first mapping value.
* the abstract method inteceptor
*/
public class ControllerServiceMethodInterceptor implements InstanceMethodsAroundInterceptor {
public abstract class AbstractMethodInteceptor implements InstanceMethodsAroundInterceptor {
public abstract String getRequestURL(Method method);
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
MethodInterceptResult result) throws Throwable {
PathMappingCache pathMappingCache = (PathMappingCache)objInst.getSkyWalkingDynamicField();
String requestURL = pathMappingCache.findPathMapping(method);
if (requestURL == null) {
RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);
if (methodRequestMapping.value().length > 0) {
requestURL = methodRequestMapping.value()[0];
} else if (methodRequestMapping.path().length > 0) {
requestURL = methodRequestMapping.path()[0];
} else {
requestURL = "";
}
requestURL = getRequestURL(method);
pathMappingCache.addPathMapping(method, requestURL);
requestURL = pathMappingCache.findPathMapping(method);
}

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

ContextCarrier contextCarrier = new ContextCarrier();
CarrierItem next = contextCarrier.items();
Expand All @@ -57,7 +51,7 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
Object ret) throws Throwable {
HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();

AbstractSpan span = ContextManager.activeSpan();
Expand All @@ -69,8 +63,9 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA
return ret;
}

@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
@@ -0,0 +1,24 @@
package org.skywalking.apm.plugin.spring.mvc;

import java.lang.reflect.Method;

import org.springframework.web.bind.annotation.RequestMapping;

/**
* The <code>RequestMappingMethodInterceptor</code> only use the first mapping value.
* it will inteceptor with <code>@RequestMapping</code>
* @author clevertension
*/
public class RequestMappingMethodInterceptor extends AbstractMethodInteceptor {
@Override
public String getRequestURL(Method method) {
String requestURL = "";
RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);
if (methodRequestMapping.value().length > 0) {
requestURL = methodRequestMapping.value()[0];
} else if (methodRequestMapping.path().length > 0) {
requestURL = methodRequestMapping.path()[0];
}
return requestURL;
}
}
@@ -0,0 +1,56 @@
package org.skywalking.apm.plugin.spring.mvc;

import java.lang.reflect.Method;

import org.springframework.web.bind.annotation.*;

/**
* The <code>RestMappingMethodInterceptor</code> only use the first mapping value.
* it will inteceptor with
* <code>@GetMapping</code>, <code>@PostMapping</code>, <code>@PutMapping</code>
* <code>@DeleteMapping</code>, <code>@PatchMapping</code>
* @author clevertension
*/
public class RestMappingMethodInterceptor extends AbstractMethodInteceptor {
@Override
public String getRequestURL(Method method) {
String requestURL = "";
GetMapping getMapping = method.getAnnotation(GetMapping.class);
PostMapping postMapping = method.getAnnotation(PostMapping.class);
PutMapping putMapping = method.getAnnotation(PutMapping.class);
DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
if (getMapping != null) {
if (getMapping.value().length > 0) {
requestURL = getMapping.value()[0];
} else if (getMapping.path().length > 0) {
requestURL = getMapping.path()[0];
}
} else if (postMapping != null) {
if (postMapping.value().length > 0) {
requestURL = postMapping.value()[0];
} else if (postMapping.path().length > 0) {
requestURL = postMapping.path()[0];
}
} else if (putMapping != null) {
if (putMapping.value().length > 0) {
requestURL = putMapping.value()[0];
} else if (putMapping.path().length > 0) {
requestURL = putMapping.path()[0];
}
} else if (deleteMapping != null) {
if (deleteMapping.value().length > 0) {
requestURL = deleteMapping.value()[0];
} else if (deleteMapping.path().length > 0) {
requestURL = deleteMapping.path()[0];
}
} else if (patchMapping != null) {
if (patchMapping.value().length > 0) {
requestURL = patchMapping.value()[0];
} else if (patchMapping.path().length > 0) {
requestURL = patchMapping.path()[0];
}
}
return requestURL;
}
}
Expand Up @@ -20,8 +20,8 @@
* <code>org.skywalking.apm.plugin.spring.mvc.ControllerConstructorInterceptor</code> set the controller base path to
* dynamic field before execute constructor.
*
* <code>org.skywalking.apm.plugin.spring.mvc.ControllerServiceMethodInterceptor</code> get the request path from
* dynamic field first, if not found, <code>ControllerServiceMethodInterceptor</code> generate request path that
* <code>org.skywalking.apm.plugin.spring.mvc.RequestMappingMethodInterceptor</code> get the request path from
* dynamic field first, if not found, <code>RequestMappingMethodInterceptor</code> generate request path that
* combine the path value of current annotation on current method and the base path and set the new path to the dynamic
* filed
*
Expand Down Expand Up @@ -56,7 +56,26 @@ public ElementMatcher<MethodDescription> getMethodsMatcher() {

@Override
public String getMethodsInterceptor() {
return "org.skywalking.apm.plugin.spring.mvc.ControllerServiceMethodInterceptor";
return "org.skywalking.apm.plugin.spring.mvc.RequestMappingMethodInterceptor";
}

@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return isAnnotatedWith(named("org.springframework.web.bind.annotation.GetMapping"))
.or(isAnnotatedWith(named("org.springframework.web.bind.annotation.PostMapping")))
.or(isAnnotatedWith(named("org.springframework.web.bind.annotation.PutMapping")))
.or(isAnnotatedWith(named("org.springframework.web.bind.annotation.DeleteMapping")))
.or(isAnnotatedWith(named("org.springframework.web.bind.annotation.PatchMapping")));
}
@Override
public String getMethodsInterceptor() {
return "org.skywalking.apm.plugin.spring.mvc.RestMappingMethodInterceptor";
}

@Override
Expand Down
@@ -0,0 +1,107 @@
package org.skywalking.apm.plugin.spring.mvc;


import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ControllerConstructorInterceptorTest {
private ControllerConstructorInterceptor controllerConstructorInterceptor;
private final MockEnhancedInstance1 inst1 = new MockEnhancedInstance1();
private final MockEnhancedInstance2 inst2 = new MockEnhancedInstance2();
private final MockEnhancedInstance3 inst3 = new MockEnhancedInstance3();
@Before
public void setUp() throws Exception {
controllerConstructorInterceptor = new ControllerConstructorInterceptor();
}

@Test
public void testOnConstruct_Accuracy1() throws Throwable {
controllerConstructorInterceptor.onConstruct(inst1, null);
PathMappingCache cache = (PathMappingCache)inst1.getSkyWalkingDynamicField();
Assert.assertNotNull(cache);

Object obj = new Object();
Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString");

Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1#toString");
}

@Test
public void testOnConstruct_Accuracy2() throws Throwable {
controllerConstructorInterceptor.onConstruct(inst2, null);
PathMappingCache cache = (PathMappingCache)inst2.getSkyWalkingDynamicField();
Assert.assertNotNull(cache);

Object obj = new Object();
Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString");

Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "#toString");
}

@Test
public void testOnConstruct_Accuracy3() throws Throwable {
controllerConstructorInterceptor.onConstruct(inst3, null);
PathMappingCache cache = (PathMappingCache)inst3.getSkyWalkingDynamicField();
Assert.assertNotNull(cache);

Object obj = new Object();
Method m = obj.getClass().getMethods()[0];
cache.addPathMapping(m, "#toString");

Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3#toString");
}

@RequestMapping(value="/test1")
private class MockEnhancedInstance1 implements EnhancedInstance {
private Object value;
@Override
public Object getSkyWalkingDynamicField() {
return value;
}

@Override
public void setSkyWalkingDynamicField(Object value) {
this.value = value;
}
}

private class MockEnhancedInstance2 implements EnhancedInstance {
private Object value;
@Override
public Object getSkyWalkingDynamicField() {
return value;
}

@Override
public void setSkyWalkingDynamicField(Object value) {
this.value = value;
}
}

@RequestMapping(path="/test3")
private class MockEnhancedInstance3 implements EnhancedInstance {
private Object value;
@Override
public Object getSkyWalkingDynamicField() {
return value;
}

@Override
public void setSkyWalkingDynamicField(Object value) {
this.value = value;
}
}
}
@@ -0,0 +1,34 @@
package org.skywalking.apm.plugin.spring.mvc;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;

import java.lang.reflect.Method;

import static org.powermock.api.mockito.PowerMockito.whenNew;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class PathMappingCacheTest {
private PathMappingCache pathMappingCache;

@Before
public void setUp() throws Exception {
pathMappingCache = new PathMappingCache("org.skywalking.apm.plugin.spring.mvc");
}

@Test
public void testAddPathMapping1() throws Throwable {
Object obj = new Object();
Method m = obj.getClass().getMethods()[0];
pathMappingCache.addPathMapping(m, "#toString");

Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "org.skywalking.apm.plugin.spring.mvc#toString");

}
}

0 comments on commit 8eed412

Please sign in to comment.