Skip to content

Commit

Permalink
Merge branch 'master' into feature/new-register-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
peng-yongsheng committed Oct 8, 2017
2 parents 1f60db7 + 8eed412 commit a20d6ea
Show file tree
Hide file tree
Showing 28 changed files with 1,240 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ target/
.DS_Store
*~
packages/
**/dependency-reduced-pom.xml
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -59,7 +59,7 @@ This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDU
<img src="https://sky-walking.github.io/page-resources/3.2.1/instance_health.png"/>

- JVM Detail.
<img src="https://sky-walking.github.io/page-resources/3.2.1/instance_graph.png"/>
<img src="https://sky-walking.github.io/page-resources/3.2/instance_graph.png"/>

- Services Dependency Tree.
<img src="https://sky-walking.github.io/page-resources/3.2.1/service_dependency_tree.png"/>
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Expand Up @@ -60,7 +60,7 @@ This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDU
<img src="https://sky-walking.github.io/page-resources/3.2.1/instance_health.png"/>

- JVM明细信息
<img src="https://sky-walking.github.io/page-resources/3.2.1/instance_graph.png"/>
<img src="https://sky-walking.github.io/page-resources/3.2/instance_graph.png"/>

- 服务依赖树.
<img src="https://sky-walking.github.io/page-resources/3.2.1/service_dependency_tree.png"/>
Expand Down
Expand Up @@ -43,8 +43,10 @@ public class ComponentsDefine {

public static final OfficialComponent JETTY_CLIENT = new OfficialComponent(18, "JettyClient");

public static final OfficialComponent JETTY_SERVER = new OfficialComponent(19, "JettyServer");

public static final OfficialComponent JETTY_SERVER = new OfficialComponent(19, "JettyServer");

public static final OfficialComponent MEMCACHE = new OfficialComponent(20, "Memcache");

private static ComponentsDefine instance = new ComponentsDefine();

private String[] components;
Expand All @@ -54,7 +56,7 @@ public static ComponentsDefine getInstance() {
}

public ComponentsDefine() {
components = new String[20];
components = new String[21];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
Expand All @@ -74,6 +76,7 @@ public ComponentsDefine() {
addComponent(NUTZ_HTTP);
addComponent(JETTY_CLIENT);
addComponent(JETTY_SERVER);
addComponent(MEMCACHE);
}

private void addComponent(OfficialComponent component) {
Expand All @@ -87,4 +90,4 @@ public String getComponentName(int componentId) {
return components[componentId];
}
}
}
}
Expand Up @@ -26,7 +26,7 @@ public abstract class AbstractClassEnhancePluginDefine {
* @throws PluginException, when set builder failure.
*/
public DynamicType.Builder<?> define(String transformClassName,
DynamicType.Builder<?> builder, ClassLoader classLoader) throws PluginException {
DynamicType.Builder<?> builder, ClassLoader classLoader, EnhanceContext context) throws PluginException {
String interceptorDefineClassName = this.getClass().getName();

if (StringUtil.isEmpty(transformClassName)) {
Expand All @@ -53,15 +53,16 @@ public DynamicType.Builder<?> define(String transformClassName,
/**
* find origin class source code for interceptor
*/
DynamicType.Builder<?> newClassBuilder = this.enhance(transformClassName, builder, classLoader);
DynamicType.Builder<?> newClassBuilder = this.enhance(transformClassName, builder, classLoader, context);

context.initializationStageCompleted();
logger.debug("enhance class {} by {} completely.", transformClassName, interceptorDefineClassName);

return newClassBuilder;
}

protected abstract DynamicType.Builder<?> enhance(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader) throws PluginException;
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader, EnhanceContext context) throws PluginException;

/**
* Define the {@link ClassMatch} for filtering class.
Expand Down
@@ -0,0 +1,34 @@
package org.skywalking.apm.agent.core.plugin;

/**
* The <code>EnhanceContext</code> represents the context or status for processing a class.
*
* Based on this context, the plugin core {@link org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine}
* knows how to process the specific steps for every particular plugin.
*
* @author wusheng
*/
public class EnhanceContext {
private boolean isEnhanced = false;
/**
* The object has already been enhanced or extended.
* e.g. added the new field, or implemented the new interface
*/
private boolean objectExtended = false;

public boolean isEnhanced() {
return isEnhanced;
}

public void initializationStageCompleted() {
isEnhanced = true;
}

public boolean isObjectExtended() {
return objectExtended;
}

public void extendObjectCompleted() {
objectExtended = true;
}
}
Expand Up @@ -22,7 +22,7 @@
* @author wusheng
*/
public class PluginFinder {
private final Map<String, AbstractClassEnhancePluginDefine> nameMatchDefine = new HashMap<String, AbstractClassEnhancePluginDefine>();
private final Map<String, LinkedList<AbstractClassEnhancePluginDefine>> nameMatchDefine = new HashMap<String, LinkedList<AbstractClassEnhancePluginDefine>>();
private final List<AbstractClassEnhancePluginDefine> signatureMatchDefine = new LinkedList<AbstractClassEnhancePluginDefine>();

public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {
Expand All @@ -35,28 +35,34 @@ public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {

if (match instanceof NameMatch) {
NameMatch nameMatch = (NameMatch)match;
nameMatchDefine.put(nameMatch.getClassName(), plugin);
LinkedList<AbstractClassEnhancePluginDefine> pluginDefines = nameMatchDefine.get(nameMatch.getClassName());
if (pluginDefines == null) {
pluginDefines = new LinkedList<AbstractClassEnhancePluginDefine>();
nameMatchDefine.put(nameMatch.getClassName(), pluginDefines);
}
pluginDefines.add(plugin);
} else {
signatureMatchDefine.add(plugin);
}
}
}

public AbstractClassEnhancePluginDefine find(TypeDescription typeDescription,
public List<AbstractClassEnhancePluginDefine> find(TypeDescription typeDescription,
ClassLoader classLoader) {
List<AbstractClassEnhancePluginDefine> matchedPlugins = new LinkedList<AbstractClassEnhancePluginDefine>();
String typeName = typeDescription.getTypeName();
if (nameMatchDefine.containsKey(typeName)) {
return nameMatchDefine.get(typeName);
matchedPlugins.addAll(nameMatchDefine.get(typeName));
}

for (AbstractClassEnhancePluginDefine pluginDefine : signatureMatchDefine) {
IndirectMatch match = (IndirectMatch)pluginDefine.enhanceClass();
if (match.isMatch(typeDescription)) {
return pluginDefine;
matchedPlugins.add(pluginDefine);
}
}

return null;
return matchedPlugins;
}

public ElementMatcher<? super TypeDescription> buildMatch() {
Expand Down
Expand Up @@ -6,6 +6,7 @@
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.Morph;
import org.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.EnhanceContext;
import org.skywalking.apm.agent.core.plugin.PluginException;
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.skywalking.apm.agent.core.plugin.interceptor.EnhanceException;
Expand Down Expand Up @@ -46,10 +47,11 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
*/
@Override
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader) throws PluginException {
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
EnhanceContext context) throws PluginException {
newClassBuilder = this.enhanceClass(enhanceOriginClassName, newClassBuilder, classLoader);

newClassBuilder = this.enhanceInstance(enhanceOriginClassName, newClassBuilder, classLoader);
newClassBuilder = this.enhanceInstance(enhanceOriginClassName, newClassBuilder, classLoader, context);

return newClassBuilder;
}
Expand All @@ -62,7 +64,8 @@ protected DynamicType.Builder<?> enhance(String enhanceOriginClassName,
* @return new byte-buddy's builder for further manipulation.
*/
private DynamicType.Builder<?> enhanceInstance(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader) throws PluginException {
DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
EnhanceContext context) throws PluginException {
ConstructorInterceptPoint[] constructorInterceptPoints = getConstructorsInterceptPoints();
InstanceMethodsInterceptPoint[] instanceMethodsInterceptPoints = getInstanceMethodsInterceptPoints();

Expand All @@ -83,16 +86,21 @@ private DynamicType.Builder<?> enhanceInstance(String enhanceOriginClassName,
}

/**
* alter class source code.<br/>
* Manipulate class source code.<br/>
*
* new class need:<br/>
* 1.Add field, name {@link #CONTEXT_ATTR_NAME}.
* 2.Add a field accessor for this field.
*
* And make sure the source codes manipulation only occurs once.
*
*/
newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, Object.class, ACC_PRIVATE)
.implement(EnhancedInstance.class)
.intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
if (!context.isObjectExtended()) {
newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, Object.class, ACC_PRIVATE)
.implement(EnhancedInstance.class)
.intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
context.extendObjectCompleted();
}

/**
* 2. enhance constructors
Expand Down
5 changes: 5 additions & 0 deletions apm-sniffer/apm-agent/pom.xml
Expand Up @@ -125,6 +125,11 @@
<artifactId>apm-jetty-server-9.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-spymemcached-2.x-plugin</artifactId>
<version>${project.version}</version>
</dependency>

<!-- activation -->
<dependency>
Expand Down
@@ -1,6 +1,7 @@
package org.skywalking.apm.agent;

import java.lang.instrument.Instrumentation;
import java.util.List;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
Expand All @@ -9,6 +10,7 @@
import org.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
import org.skywalking.apm.agent.core.logging.EasyLogResolver;
import org.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
import org.skywalking.apm.agent.core.plugin.EnhanceContext;
import org.skywalking.apm.agent.core.plugin.PluginBootstrap;
import org.skywalking.apm.agent.core.plugin.PluginException;
import org.skywalking.apm.agent.core.plugin.PluginFinder;
Expand Down Expand Up @@ -54,13 +56,21 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
ClassLoader classLoader, JavaModule module) {
AbstractClassEnhancePluginDefine pluginDefine = pluginFinder.find(typeDescription, classLoader);
if (pluginDefine != null) {
DynamicType.Builder<?> newBuilder = pluginDefine.define(typeDescription.getTypeName(), builder, classLoader);
if (newBuilder != null) {
List<AbstractClassEnhancePluginDefine> pluginDefines = pluginFinder.find(typeDescription, classLoader);
if (pluginDefines.size() > 0) {
DynamicType.Builder<?> newBuilder = builder;
EnhanceContext context = new EnhanceContext();
for (AbstractClassEnhancePluginDefine define : pluginDefines) {
DynamicType.Builder<?> possibleNewBuilder = define.define(typeDescription.getTypeName(), newBuilder, classLoader, context);
if (possibleNewBuilder != null) {
newBuilder = possibleNewBuilder;
}
}
if (context.isEnhanced()) {
logger.debug("Finish the prepare stage for {}.", typeDescription.getName());
return newBuilder;
}

return newBuilder;
}

logger.debug("Matched class {}, but ignore by finding mechanism.", typeDescription.getTypeName());
Expand Down
1 change: 1 addition & 0 deletions apm-sniffer/apm-sdk-plugin/pom.xml
Expand Up @@ -26,6 +26,7 @@
<module>struts2-2.x-plugin</module>
<module>nutz-plugins</module>
<module>jetty-plugin</module>
<module>spymemcached-2.x-plugin</module>
</modules>
<packaging>pom</packaging>

Expand Down
@@ -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 comments on commit a20d6ea

Please sign in to comment.