提交 64b6c922 编写于 作者: W wendal chen

add: support nutz mvc and nutz http

上级 065994a5
......@@ -35,6 +35,10 @@ public class ComponentsDefine {
public static final OfficialComponent SPRING_MVC_ANNOTATION = new OfficialComponent(14, "SpringMVCAnnotation");
public static final OfficialComponent NUTZ_MVC_ANNOTATION = new OfficialComponent(15, "NutzMVCAnnotation");
public static final OfficialComponent NUTZ_HTTP = new OfficialComponent(16, "NutzHttp");
private static ComponentsDefine instance = new ComponentsDefine();
private String[] components;
......@@ -44,7 +48,7 @@ public class ComponentsDefine {
}
public ComponentsDefine() {
components = new String[15];
components = new String[17];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
......@@ -59,6 +63,8 @@ public class ComponentsDefine {
addComponent(OKHTTP);
addComponent(SPRING_REST_TEMPLATE);
addComponent(SPRING_MVC_ANNOTATION);
addComponent(NUTZ_MVC_ANNOTATION);
addComponent(NUTZ_HTTP);
}
private void addComponent(OfficialComponent component) {
......
......@@ -100,6 +100,16 @@
<artifactId>apm-spring-cloud-feign-1.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-nutz-mvc-annotation-1.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-nutz-http-1.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<!-- activation -->
<dependency>
......
<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>nutz-plugins</artifactId>
<groupId>org.skywalking</groupId>
<version>3.2-2017</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-nutz-http-1.x-plugin</artifactId>
<packaging>jar</packaging>
<name>http-1.x-plugin</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.62</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package org.skywalking.apm.plugin.nutz.http.sync;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URI;
import org.nutz.http.Request;
import org.nutz.http.Request.METHOD;
import org.nutz.http.Response;
import org.nutz.http.Sender;
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;
public class SenderSendInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
Field field = Sender.class.getDeclaredField("request");
field.setAccessible(true);
Request req = (Request) field.get(objInst);
final URI requestURL = req.getUrl().toURI();
final METHOD httpMethod = req.getMethod();
final ContextCarrier contextCarrier = new ContextCarrier();
String remotePeer = requestURL.getHost() + ":" + requestURL.getPort();
AbstractSpan span = ContextManager.createExitSpan(requestURL.getPath(), contextCarrier, remotePeer);
span.setComponent(ComponentsDefine.NUTZ_HTTP);
Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + ":" + requestURL.getPort() + requestURL.getPath());
Tags.HTTP.METHOD.set(span, httpMethod.toString());
SpanLayer.asHttp(span);
req.getHeader().set(Config.Plugin.Propagation.HEADER_NAME, contextCarrier.serialize());
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
Response response = (Response)ret;
int statusCode = response.getStatus();
AbstractSpan span = ContextManager.activeSpan();
if (statusCode >= 400) {
span.errorOccurred();
Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
}
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.nutz.http.sync.define;
import static net.bytebuddy.matcher.ElementMatchers.named;
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 org.skywalking.apm.agent.core.plugin.match.HierarchyMatch;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
/**
* {@link NutzHttpInstrumentation} enhance the <code>doExecute</code> method,<code>handleResponse</code> method and
* <code>handleResponse</code> method of <code>org.springframework.web.client.RestTemplate</code> by
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestExecuteInterceptor</code>,
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestResponseInterceptor</code> and
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestRequestInterceptor</code>.
*
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestResponseInterceptor</code> set context to header for
* propagate trace context after execute <code>createRequest</code>.
*
* @author wendal
*/
public class NutzHttpInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.nutz.http.Sender";
private static final String DO_EXECUTE_METHOD_NAME = "send";
private static final String DO_EXECUTE_INTERCEPTOR = "org.skywalking.apm.plugin.nutz.http.sync.SenderSendInterceptor";
@Override
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(DO_EXECUTE_METHOD_NAME);
}
@Override public String getMethodsInterceptor() {
return DO_EXECUTE_INTERCEPTOR;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return HierarchyMatch.byHierarchyMatch(new String[]{ENHANCE_CLASS});
}
}
nutz-http-1.x=org.skywalking.apm.plugin.nutz.http.sync.define.NutzHttpInstrumentation
<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>nutz-plugins</artifactId>
<groupId>org.skywalking</groupId>
<version>3.2-2017</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-nutz-mvc-annotation-1.x-plugin</artifactId>
<packaging>jar</packaging>
<name>mvc-annotation-1.x-plugin</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.62</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package org.skywalking.apm.plugin.nutz.mvc;
import org.nutz.mvc.annotation.At;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
/**
* 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 Nutz MVC core codes, that is not a good choice for now.
*
* Comment by @wu-sheng
*/
public class ControllerConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String basePath = "";
At basePathRequestMapping = objInst.getClass().getAnnotation(At.class);
if (basePathRequestMapping != null) {
basePath = basePathRequestMapping.value()[0];
}
PathMappingCache pathMappingCache = new PathMappingCache(basePath);
objInst.setSkyWalkingDynamicField(pathMappingCache);
}
}
package org.skywalking.apm.plugin.nutz.mvc;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
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;
/**
* 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 {
PathMappingCache pathMappingCache = (PathMappingCache)objInst.getSkyWalkingDynamicField();
String requestURL = pathMappingCache.findPathMapping(method);
if (requestURL == null) {
At methodRequestMapping = method.getAnnotation(At.class);
if (methodRequestMapping.value().length > 0) {
requestURL = methodRequestMapping.value()[0];
} else {
requestURL = "";
}
pathMappingCache.addPathMapping(method, requestURL);
requestURL = pathMappingCache.findPathMapping(method);
}
HttpServletRequest request = Mvcs.getReq();
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.NUTZ_MVC_ANNOTATION);
SpanLayer.asHttp(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
HttpServletResponse response = Mvcs.getResp();
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.nutz.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);
}
}
package org.skywalking.apm.plugin.nutz.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;
/**
*
* @author wendal
*/
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.nutz.mvc.ControllerConstructorInterceptor";
}
}
};
}
@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return isAnnotatedWith(named("org.nutz.mvc.annotation.At"));
}
@Override
public String getMethodsInterceptor() {
return "org.skywalking.apm.plugin.nutz.mvc.ControllerServiceMethodInterceptor";
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return byClassAnnotationMatch(getEnhanceAnnotations());
}
protected abstract String[] getEnhanceAnnotations();
}
package org.skywalking.apm.plugin.nutz.mvc.define;
public class ControllerInstrumentation extends AbstractControllerInstrumentation {
public static final String ENHANCE_ANNOTATION = "org.nutz.mvc.annotation.At";
@Override protected String[] getEnhanceAnnotations() {
return new String[] {ENHANCE_ANNOTATION};
}
}
nutz-mvc-annotation-1.x=org.skywalking.apm.plugin.nutz.mvc.define.ControllerInstrumentation
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.skywalking</groupId>
<artifactId>apm-sdk-plugin</artifactId>
<version>3.2-2017</version>
</parent>
<artifactId>nutz-plugins</artifactId>
<modules>
<!-- <module>dao-1.x-plugin</module> -->
<module>http-1.x-plugin</module>
<module>mvc-annotation-1.x-plugin</module>
</modules>
<packaging>pom</packaging>
<name>apm-sdk-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
......@@ -23,6 +23,7 @@
<module>resin-3.x-plugin</module>
<module>resin-4.x-plugin</module>
<module>spring-plugins</module>
<module>nutz-plugins</module>
</modules>
<packaging>pom</packaging>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册