提交 fe383773 编写于 作者: A ascrutae

support spring mvc annotation plugin

上级 9f3434e8
......@@ -31,7 +31,9 @@ public class ComponentsDefine {
public static final OfficialComponent OKHTTP = new OfficialComponent(12, "OKHttp");
public static final OfficialComponent REST_TEMPLATE = new OfficialComponent(13, "RestTemplate");
public static final OfficialComponent SPRING_REST_TEMPLATE = new OfficialComponent(13, "SpringRestTemplate");
public static final OfficialComponent SPRING_MVC_ANNOTATION = new OfficialComponent(14, "SpringMVCAnnotation");
private static ComponentsDefine instance = new ComponentsDefine();
......@@ -42,7 +44,7 @@ public class ComponentsDefine {
}
public ComponentsDefine() {
components = new String[14];
components = new String[15];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
......@@ -55,7 +57,8 @@ public class ComponentsDefine {
addComponent(RESIN);
addComponent(FEIGN);
addComponent(OKHTTP);
addComponent(REST_TEMPLATE);
addComponent(SPRING_REST_TEMPLATE);
addComponent(SPRING_MVC_ANNOTATION);
}
private void addComponent(OfficialComponent component) {
......
package org.skywalking.apm.agent.core.plugin.match;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.bytebuddy.description.annotation.AnnotationDescription;
......@@ -43,7 +44,7 @@ public class ClassAnnotationMatch implements IndirectMatch {
@Override
public boolean isMatch(TypeDescription typeDescription) {
List<String> annotationList = Arrays.asList(annotations);
List<String> annotationList = new ArrayList<String>(Arrays.asList(annotations));
AnnotationList declaredAnnotations = typeDescription.getDeclaredAnnotations();
for (AnnotationDescription annotation : declaredAnnotations) {
annotationList.remove(annotation.getAnnotationType().getActualName());
......
......@@ -90,6 +90,11 @@
<artifactId>apm-spring-concurrent-util-4.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-springmvc-annotation-4.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<!-- activation -->
<dependency>
......
......@@ -24,7 +24,7 @@ public class FailureCallbackInterceptor implements InstanceMethodsAroundIntercep
URI uri = (URI)cacheValues[0];
AbstractSpan span = ContextManager.createLocalSpan("future/failureCallback:" + uri.getPath());
span.errorOccurred().log((Throwable)allArguments[0]).setComponent(ComponentsDefine.REST_TEMPLATE).setLayer(SpanLayer.HTTP);
span.errorOccurred().log((Throwable)allArguments[0]).setComponent(ComponentsDefine.SPRING_REST_TEMPLATE).setLayer(SpanLayer.HTTP);
Tags.URL.set(span, uri.getPath());
ContextManager.continued((ContextSnapshot)cacheValues[2]);
}
......
......@@ -24,7 +24,7 @@ public class SuccessCallbackInterceptor implements InstanceMethodsAroundIntercep
URI uri = (URI)cacheValues[0];
AbstractSpan span = ContextManager.createLocalSpan("future/successCallback:" + uri.getPath());
span.setComponent(ComponentsDefine.REST_TEMPLATE).setLayer(SpanLayer.HTTP);
span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE).setLayer(SpanLayer.HTTP);
Tags.URL.set(span, uri.getPath());
ContextManager.continued((ContextSnapshot)cacheValues[2]);
}
......
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-plugins</artifactId>
<groupId>org.skywalking</groupId>
<version>3.2-2017</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-springmvc-annotation-4.x-plugin</artifactId>
<packaging>jar</packaging>
<name>mvc-annotation-4.x-plugin</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
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;
public class ControllerConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String basePath = "";
RequestMapping basePathRequestMapping = objInst.getClass().getAnnotation(RequestMapping.class);
if (basePathRequestMapping != null) {
basePath = basePathRequestMapping.value()[0];
}
Map<Object, String> cacheRequestPath = new HashMap<Object, String>();
cacheRequestPath.put("BASE_PATH", basePath);
objInst.setSkyWalkingDynamicField(cacheRequestPath);
}
}
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;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.tag.Tags;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
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;
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);
if (requestURL == null) {
requestURL = new String(cacheRequestURL.get("BASE_PATH"));
requestURL += method.getAnnotation(RequestMapping.class).value()[0];
cacheRequestURL.put(method, requestURL.toString());
}
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String tracingHeaderValue = request.getHeader(Config.Plugin.Propagation.HEADER_NAME);
ContextCarrier contextCarrier = new ContextCarrier().deserialize(tracingHeaderValue);
AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier);
Tags.URL.set(span, request.getRequestURL().toString());
Tags.HTTP.METHOD.set(span, request.getMethod());
span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION);
SpanLayer.asHttp(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
AbstractSpan span = ContextManager.activeSpan();
if (response.getStatus() >= 400) {
span.errorOccurred();
Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus()));
}
ContextManager.stopSpan();
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
package org.skywalking.apm.plugin.spring.mvc.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch.byClassAnnotationMatch;
/**
* {@link ControllerInstrumentation} enhance all constructor and method annotated with
* <code>org.springframework.web.bind.annotation.RequestMapping</code> that class has
* <code>org.springframework.stereotype.Controller</code> annotation.
*
* <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
* combine the path value of current annotation on current method and the base path and set the new path to the dynamic
* filed
*
* @author zhangxin
*/
public abstract class AbstractControllerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
@Override
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override
public String getConstructorInterceptor() {
return "org.skywalking.apm.plugin.spring.mvc.ControllerConstructorInterceptor";
}
}
};
}
@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return isAnnotatedWith(named("org.springframework.web.bind.annotation.RequestMapping"));
}
@Override
public String getMethodsInterceptor() {
return "org.skywalking.apm.plugin.spring.mvc.ControllerServiceMethodInterceptor";
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return byClassAnnotationMatch(getEnhanceAnnotations());
}
protected abstract String[] getEnhanceAnnotations();
}
package org.skywalking.apm.plugin.spring.mvc.define;
public class ControllerInstrumentation extends AbstractControllerInstrumentation {
public static final String ENHANCE_ANNOTATION = "org.springframework.stereotype.Controller";
@Override protected String[] getEnhanceAnnotations() {
return new String[] {ENHANCE_ANNOTATION};
}
}
package org.skywalking.apm.plugin.spring.mvc.define;
public class RestControllerInstrumentation extends AbstractControllerInstrumentation {
public static final String ENHANCE_ANNOTATION = "org.springframework.web.bind.annotation.RestController";
@Override protected String[] getEnhanceAnnotations() {
return new String[] {ENHANCE_ANNOTATION};
}
}
spring-mvc-annotation-4.x=org.skywalking.apm.plugin.spring.mvc.define.ControllerInstrumentation
spring-mvc-annotation-4.x=org.skywalking.apm.plugin.spring.mvc.define.RestControllerInstrumentation
\ No newline at end of file
......@@ -13,6 +13,7 @@
<modules>
<module>concurrent-util-4.x-plugin</module>
<module>resttemplate-4.x-plugin</module>
<module>mvc-annotation-4.x-plugin</module>
</modules>
<packaging>pom</packaging>
......
......@@ -24,7 +24,7 @@ public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor
String remotePeer = requestURL.getHost() + ":" + requestURL.getPort();
AbstractSpan span = ContextManager.createExitSpan(requestURL.getPath(), contextCarrier, remotePeer);
span.setComponent(ComponentsDefine.REST_TEMPLATE);
span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE);
Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + ":" + requestURL.getPort() + requestURL.getPath());
Tags.HTTP.METHOD.set(span, httpMethod.toString());
SpanLayer.asHttp(span);
......
......@@ -24,7 +24,7 @@ public class RestExecuteInterceptor implements InstanceMethodsAroundInterceptor
String remotePeer = requestURL.getHost() + ":" + requestURL.getPort();
AbstractSpan span = ContextManager.createExitSpan(requestURL.getPath(), contextCarrier, remotePeer);
span.setComponent(ComponentsDefine.REST_TEMPLATE);
span.setComponent(ComponentsDefine.SPRING_REST_TEMPLATE);
Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + ":" + requestURL.getPort() + requestURL.getPath());
Tags.HTTP.METHOD.set(span, httpMethod.toString());
SpanLayer.asHttp(span);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册