diff --git a/skywalking-sdk-plugin/spring-plugin/pom.xml b/skywalking-sdk-plugin/spring-plugin/pom.xml index 7d6a8cafa..e01c1be16 100644 --- a/skywalking-sdk-plugin/spring-plugin/pom.xml +++ b/skywalking-sdk-plugin/spring-plugin/pom.xml @@ -76,15 +76,25 @@ ${project.version} test + + + + + + + + + + + + + + + - org.springframework - spring-web - 3.2.9.RELEASE - - - org.springframework - spring-context - 3.0.0.RELEASE + junit + junit + 4.12 diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/SpringTagNamespaceHandler.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/SpringTagNamespaceHandler.java index b7529cc7e..0a572d0c0 100644 --- a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/SpringTagNamespaceHandler.java +++ b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/SpringTagNamespaceHandler.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class SpringTagNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { - registerBeanDefinitionParser("tracing-bean", new TracingBeanParser()); + registerBeanDefinitionParser("tracing-bean", new TracingPatternParser()); registerBeanDefinitionParser("trace", new TraceParser()); } } diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TraceParser.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TraceParser.java index e1ebb9ac0..56c7184a8 100644 --- a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TraceParser.java +++ b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TraceParser.java @@ -7,23 +7,19 @@ import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; public class TraceParser implements BeanDefinitionParser { + + private final String TRACE_APPLICATION_BEAN_NAME = "TracingApplication"; + @Override public BeanDefinition parse(Element element, ParserContext parserContext) { + if (parserContext.getRegistry().containsBeanDefinition(TRACE_APPLICATION_BEAN_NAME)) { + // 只能存在一个 + throw new IllegalStateException("Duplicate spring bean id "); + } RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(); rootBeanDefinition.setLazyInit(false); rootBeanDefinition.setBeanClass(TracingEnhanceProcessor.class); - String id = null; - id = element.getAttribute("name"); - int counter = 2; - while (parserContext.getRegistry().containsBeanDefinition(id)) { - id = id + (counter++); - } - if (id != null && id.length() > 0) { - if (parserContext.getRegistry().containsBeanDefinition(id)) { - throw new IllegalStateException("Duplicate spring bean id " + id); - } - parserContext.getRegistry().registerBeanDefinition(id, rootBeanDefinition); - } + parserContext.getRegistry().registerBeanDefinition(TRACE_APPLICATION_BEAN_NAME, rootBeanDefinition); return rootBeanDefinition; } } diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/Tracing.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/Tracing.java deleted file mode 100644 index 30ce1217a..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/Tracing.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Tracing { -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingBeanParser.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingBeanParser.java deleted file mode 100644 index dd7e5818f..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingBeanParser.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.RuntimeBeanReference; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.beans.factory.xml.BeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; - -public class TracingBeanParser implements BeanDefinitionParser { - @Override - public BeanDefinition parse(Element element, ParserContext parserContext) { - // 获取Method并处理 - String methodPattern = element.getAttribute("method"); - if (methodPattern == null || methodPattern.length() == 0) { - throw new IllegalStateException("Miss method pattern"); - } - - String packageName = element.getAttribute("package"); - String className = element.getAttribute("class"); - - RootBeanDefinition beanDefinition = new RootBeanDefinition(); - beanDefinition.setBeanClass(TracingClassBean.class); - String id = null; - id =element.getAttribute("name"); - int counter = 2; - while(parserContext.getRegistry().containsBeanDefinition(id)) { - id = id +(counter++); - } - if (id != null &&id.length() > 0) { - if(parserContext.getRegistry().containsBeanDefinition(id)) { - throw new IllegalStateException("Duplicate spring bean id " + id); - } - parserContext.getRegistry().registerBeanDefinition(id,beanDefinition); - } - NamedNodeMap nnm =element.getAttributes(); - for (int i = 0; i beanSet = new ConcurrentHashSet(); - private ApplicationContext applicationContext; + private final Set beanSet = new ConcurrentHashSet(); - @Override - public void postProcessBeanFactory( - ConfigurableListableBeanFactory beanFactory) throws BeansException { - applicationContext.getBeansOfType(TracingClassBean.class); - } + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + beanSet.addAll(applicationContext.getBeansOfType(TracingPattern.class).values()); + } - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } - private enum MatchType { - METHOD, PACKAGE, CLASS; - } + private ApplicationContext applicationContext; - private boolean checkMatch(String value, String pattern, MatchType matchType) { - boolean result; - if ("*".equals(pattern)) { - return true; - } - if (matchType == MatchType.PACKAGE) { - if (pattern.endsWith(".*")) { - String newPattern = pattern.substring(0, - pattern.lastIndexOf(".*")); - result = value.startsWith(newPattern); - }else{ - result = value.equals(pattern); - } - } else { - if (pattern.endsWith("*")) { - String newPattern = pattern.substring(0, - pattern.lastIndexOf("*")); - result = value.startsWith(newPattern); - } else { - result = value.equals(pattern); - } - } - return result; - } + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - String packageName = bean.getClass().getPackage().getName(); - String className = bean.getClass().getSimpleName(); - TracingClassBean matchClassBean = null; - boolean isMatch = false; - for (TracingClassBean tracingClassBean : beanSet) { - if (checkMatch(packageName, tracingClassBean.getPackageName(), - MatchType.PACKAGE) - || checkMatch(className, tracingClassBean.getClassName(), - MatchType.CLASS)) { - isMatch = true; - matchClassBean = tracingClassBean; - continue; - } - } - if (!isMatch || matchClassBean == null) { - return bean; - } + public enum MatchType { + METHOD, PACKAGE, CLASS; + } - // 符合规范 - try { - ClassPool pool = ClassPool.getDefault(); - CtClass ctSource = pool.get(bean.getClass().getName()); - CtClass ctDestination = pool.makeClass( - generateProxyClassName(bean), ctSource); - // 拷贝所有的方法, - copyAllFields(ctSource, ctDestination); - // 拷贝所有的注解 - copyClassAnnotation(ctSource, ctDestination); - // 拷贝所有的方法,并增强 - ConstPool cp = ctDestination.getClassFile().getConstPool(); - for (CtMethod m : ctSource.getDeclaredMethods()) { - CtMethod newm = CtNewMethod.copy(m, m.getName(), ctDestination, - null); - copyMethodAnnotation(cp, m, newm); - // 是否符合规范,符合则增强 - if (checkMatch(m.getName(), matchClassBean.getMethod(), - MatchType.METHOD)) { - enhanceMethod(bean, newm); - } - ctDestination.addMethod(newm); - } + private boolean checkMatch(String value, String pattern, MatchType matchType) { + boolean result; + if ("*".equals(pattern)) { + return true; + } + if (matchType == MatchType.PACKAGE) { + if (pattern.endsWith(".*")) { + String newPattern = pattern.substring(0, + pattern.lastIndexOf(".*")); + result = value.startsWith(newPattern); + } else { + result = value.equals(pattern); + } + } else { + if (pattern.endsWith("*")) { + String newPattern = pattern.substring(0, + pattern.lastIndexOf("*")); + result = value.startsWith(newPattern); + } else { + result = value.equals(pattern); + } + } + return result; + } - Class generateClass = ctDestination.toClass(); - Object newBean = generateClass.newInstance(); - BeanUtils.copyProperties(bean, newBean); - return newBean; - } catch (NotFoundException e) { - throw new IllegalStateException("Class [" - + beanName.getClass().getName() + "] cannot be found", e); - } catch (CannotCompileException e) { - throw new IllegalStateException("Class [" - + beanName.getClass().getName() + "] cannot be compile", e); - } catch (InstantiationException e) { - throw new IllegalStateException("Failed to instance class[" - + beanName.getClass().getName() + "]", e); - } catch (IllegalAccessException e) { - throw new IllegalStateException("Failed to access class[" - + beanName.getClass().getName() + "]", e); - } - } + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + String packageName = bean.getClass().getPackage().getName(); + String className = bean.getClass().getSimpleName(); + TracingPattern matchClassBean = null; + boolean isMatch = false; + for (TracingPattern tracingPattern : beanSet) { + if (checkMatch(packageName, tracingPattern.getPackageName(), MatchType.PACKAGE) + && checkMatch(className, tracingPattern.getClassName(), MatchType.CLASS)) { + isMatch = true; + matchClassBean = tracingPattern; + continue; + } + } + if (!isMatch || matchClassBean == null) { + return bean; + } - private void copyMethodAnnotation(ConstPool cp, CtMethod m, CtMethod newm) { - AnnotationsAttribute invAnn = (AnnotationsAttribute) m.getMethodInfo() - .getAttribute(AnnotationsAttribute.invisibleTag); - AnnotationsAttribute visAnn = (AnnotationsAttribute) m.getMethodInfo() - .getAttribute(AnnotationsAttribute.visibleTag); - if (invAnn != null) { - newm.getMethodInfo().addAttribute(invAnn.copy(cp, null)); - } - if (visAnn != null) { - newm.getMethodInfo().addAttribute(visAnn.copy(cp, null)); - } - } + //符合规范 + try { + ClassPool pool = ClassPool.getDefault(); + CtClass ctSource = pool.get(bean.getClass().getName()); + CtClass ctDestination = pool.makeClass(generateProxyClassName(bean), ctSource); + //拷贝所有的方法, + copyAllFields(ctSource, ctDestination); + // 拷贝所有的注解 + copyClassAnnotation(ctSource, ctDestination); + //拷贝所有的方法,并增强 + ConstPool cp = ctDestination.getClassFile().getConstPool(); + for (CtMethod m : ctSource.getDeclaredMethods()) { + CtMethod newm = CtNewMethod.copy(m, m.getName(), ctDestination, null); + copyMethodAnnotation(cp, m, newm); + // 是否符合规范,符合则增强 + if (checkMatch(m.getName(), matchClassBean.getMethod(), MatchType.METHOD)) { + enhanceMethod(bean, newm); + } + ctDestination.addMethod(newm); + } - private String generateProxyClassName(Object bean) { - return bean.getClass().getName() + "$EnhanceBySWTracing$" - + ThreadLocalRandom.current().nextInt(100); - } + Class generateClass = ctDestination.toClass(); + Object newBean = generateClass.newInstance(); + BeanUtils.copyProperties(bean, newBean); + return newBean; + } catch (NotFoundException e) { + throw new IllegalStateException("Class [" + beanName.getClass().getName() + "] cannot be found", e); + } catch (CannotCompileException e) { + throw new IllegalStateException("Class [" + beanName.getClass().getName() + "] cannot be compile", e); + } catch (InstantiationException e) { + throw new IllegalStateException("Failed to instance class[" + beanName.getClass().getName() + "]", e); + } catch (IllegalAccessException e) { + throw new IllegalStateException("Failed to access class[" + beanName.getClass().getName() + "]", e); + } + } - private void copyAllFields(CtClass ctSource, CtClass ctDestination) - throws CannotCompileException, NotFoundException { - // copy fields - ConstPool cp = ctDestination.getClassFile().getConstPool(); - for (CtField ctSourceField : ctSource.getDeclaredFields()) { - CtClass fieldTypeClass = ClassPool.getDefault().get( - ctSourceField.getType().getName()); - CtField ctField = new CtField(fieldTypeClass, - ctSourceField.getName(), ctDestination); - // with annotations - copyAllFieldAnnotation(cp, ctSourceField, ctField); - ctDestination.addField(ctField); - } - } + private void copyMethodAnnotation(ConstPool cp, CtMethod m, CtMethod newm) { + AnnotationsAttribute invAnn = (AnnotationsAttribute) m.getMethodInfo().getAttribute( + AnnotationsAttribute.invisibleTag); + AnnotationsAttribute visAnn = (AnnotationsAttribute) m.getMethodInfo().getAttribute( + AnnotationsAttribute.visibleTag); + if (invAnn != null) { + newm.getMethodInfo().addAttribute(invAnn.copy(cp, null)); + } + if (visAnn != null) { + newm.getMethodInfo().addAttribute(visAnn.copy(cp, null)); + } + } - private void copyAllFieldAnnotation(ConstPool cp, CtField ctSourceField, - CtField ctDestinationField) throws CannotCompileException { - AnnotationsAttribute invAnn = (AnnotationsAttribute) ctSourceField - .getFieldInfo().getAttribute(AnnotationsAttribute.invisibleTag); - AnnotationsAttribute visAnn = (AnnotationsAttribute) ctSourceField - .getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag); + private String generateProxyClassName(Object bean) { + return bean.getClass().getName() + "$EnhanceBySWTracing$" + ThreadLocalRandom.current().nextInt(100); + } - if (invAnn != null) { - ctDestinationField.getFieldInfo().addAttribute( - invAnn.copy(cp, null)); - } - if (visAnn != null) { - ctDestinationField.getFieldInfo().addAttribute( - visAnn.copy(cp, null)); - } - } + private void copyAllFields(CtClass ctSource, CtClass ctDestination) throws CannotCompileException, NotFoundException { + // copy fields + ConstPool cp = ctDestination.getClassFile().getConstPool(); + for (CtField ctSourceField : ctSource.getDeclaredFields()) { + CtClass fieldTypeClass = ClassPool.getDefault().get(ctSourceField.getType().getName()); + CtField ctField = new CtField(fieldTypeClass, ctSourceField.getName(), ctDestination); + //with annotations + copyAllFieldAnnotation(cp, ctSourceField, ctField); + ctDestination.addField(ctField); + } + } - private void copyClassAnnotation(CtClass ctSource, CtClass ctDestination) { - ConstPool cp = ctDestination.getClassFile().getConstPool(); - AnnotationsAttribute invAnn = (AnnotationsAttribute) ctSource - .getClassFile().getAttribute(AnnotationsAttribute.invisibleTag); - AnnotationsAttribute visAnn = (AnnotationsAttribute) ctSource - .getClassFile().getAttribute(AnnotationsAttribute.visibleTag); - if (invAnn != null) { - ctDestination.getClassFile().addAttribute(invAnn.copy(cp, null)); - } - if (visAnn != null) { - ctDestination.getClassFile().addAttribute(visAnn.copy(cp, null)); - } - } + private void copyAllFieldAnnotation(ConstPool cp, CtField ctSourceField, CtField ctDestinationField) throws CannotCompileException { + AnnotationsAttribute invAnn = (AnnotationsAttribute) ctSourceField.getFieldInfo().getAttribute( + AnnotationsAttribute.invisibleTag); + AnnotationsAttribute visAnn = (AnnotationsAttribute) ctSourceField.getFieldInfo().getAttribute( + AnnotationsAttribute.visibleTag); - protected void enhanceMethod(Object bean, CtMethod method) - throws CannotCompileException, NotFoundException { - ClassPool cp = method.getDeclaringClass().getClassPool(); - method.addLocalVariable("___sender", - cp.get(LocalBuriedPointSender.class.getName())); - method.insertBefore("___sender = new " - + LocalBuriedPointSender.class.getName() - + "();___sender.beforeSend" - + generateBeforeSendParameter(bean, method)); - method.addCatch("new " + LocalBuriedPointSender.class.getName() - + "().handleException(e);throw e;", ClassPool.getDefault() - .getCtClass(Throwable.class.getName()), "e"); - method.insertAfter("new " + LocalBuriedPointSender.class.getName() - + "().afterSend();", true); - } + if (invAnn != null) { + ctDestinationField.getFieldInfo().addAttribute(invAnn.copy(cp, null)); + } + if (visAnn != null) { + ctDestinationField.getFieldInfo().addAttribute(visAnn.copy(cp, null)); + } + } - private String generateBeforeSendParameter(Object bean, CtMethod method) - throws NotFoundException { - StringBuilder builder = new StringBuilder("(" - + Identification.class.getName() + ".newBuilder().viewPoint(\"" - + bean.getClass().getName() + "." + method.getName()); - builder.append("("); - for (CtClass param : method.getParameterTypes()) { - builder.append(param.getSimpleName() + ","); - } - if (method.getParameterTypes().length > 0) { - builder = builder.delete(builder.length() - 1, builder.length()); - } - builder.append(")"); - builder.append("\").spanType('M').build());"); - return builder.toString(); - } + private void copyClassAnnotation(CtClass ctSource, CtClass ctDestination) { + ConstPool cp = ctDestination.getClassFile().getConstPool(); + AnnotationsAttribute invAnn = (AnnotationsAttribute) ctSource.getClassFile().getAttribute( + AnnotationsAttribute.invisibleTag); + AnnotationsAttribute visAnn = (AnnotationsAttribute) ctSource.getClassFile().getAttribute( + AnnotationsAttribute.visibleTag); + if (invAnn != null) { + ctDestination.getClassFile().addAttribute(invAnn.copy(cp, null)); + } + if (visAnn != null) { + ctDestination.getClassFile().addAttribute(visAnn.copy(cp, null)); + } + } - private boolean isClassNameMatch(Object bean, String tracingClassBean) { - // - String classNamePrefix = tracingClassBean; - if (tracingClassBean.endsWith("*")) { - classNamePrefix = tracingClassBean.substring(0, - tracingClassBean.indexOf('*')); - } - return bean.getClass().getSimpleName().startsWith(classNamePrefix); - } + protected void enhanceMethod(Object bean, CtMethod method) throws CannotCompileException, NotFoundException { + ClassPool cp = method.getDeclaringClass().getClassPool(); + method.addLocalVariable("___sender", cp.get(LocalBuriedPointSender.class.getName())); + method.insertBefore("___sender = new " + LocalBuriedPointSender.class.getName() + "();___sender.beforeSend" + + generateBeforeSendParameter(bean, method)); + method.addCatch("new " + LocalBuriedPointSender.class.getName() + "().handleException(e);throw e;", + ClassPool.getDefault().getCtClass(Throwable.class.getName()), "e"); + method.insertAfter("new " + LocalBuriedPointSender.class.getName() + "().afterSend();", true); + } - private boolean isPackageMatch(Object bean, String packageName) { - return bean.getClass().getPackage().equals(packageName); - } + private String generateBeforeSendParameter(Object bean, CtMethod method) throws NotFoundException { + StringBuilder builder = new StringBuilder("(" + Identification.class.getName() + ".newBuilder().viewPoint(\"" + + bean.getClass().getName() + "." + method.getName()); + builder.append("("); + for (CtClass param : method.getParameterTypes()) { + builder.append(param.getSimpleName() + ","); + } + if (method.getParameterTypes().length > 0) { + builder = builder.delete(builder.length() - 1, builder.length()); + } + builder.append(")"); + builder.append("\").spanType('M').build());"); + return builder.toString(); + } - @Override - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { - this.applicationContext = applicationContext; - } - @Override - public void destroy() throws Exception { + private boolean isClassNameMatch(Object bean, String tracingClassBean) { + // + String classNamePrefix = tracingClassBean; + if (tracingClassBean.endsWith("*")) { + classNamePrefix = tracingClassBean.substring(0, tracingClassBean.indexOf('*')); + } + return bean.getClass().getSimpleName().startsWith(classNamePrefix); + } - } + private boolean isPackageMatch(Object bean, String packageName) { + return bean.getClass().getPackage().equals(packageName); + } + + @Override + public void destroy() throws Exception { + + } } diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingClassBean.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPattern.java similarity index 94% rename from skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingClassBean.java rename to skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPattern.java index 2e60efdb3..7c5a4ba47 100644 --- a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingClassBean.java +++ b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPattern.java @@ -1,6 +1,6 @@ package com.ai.cloud.skywalking.plugin.spring; -public class TracingClassBean { +public class TracingPattern { private String packageName = ""; private String className = ""; private String method = ""; diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPatternParser.java b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPatternParser.java new file mode 100644 index 000000000..a33563a6b --- /dev/null +++ b/skywalking-sdk-plugin/spring-plugin/src/main/java/com/ai/cloud/skywalking/plugin/spring/TracingPatternParser.java @@ -0,0 +1,54 @@ +package com.ai.cloud.skywalking.plugin.spring; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +public class TracingPatternParser implements BeanDefinitionParser { + @Override + public BeanDefinition parse(Element element, ParserContext parserContext) { + // 获取Method并处理 + String methodPattern = element.getAttribute("method"); + if (methodPattern == null || methodPattern.length() == 0) { + throw new IllegalStateException("Miss method pattern"); + } + RootBeanDefinition beanDefinition = new RootBeanDefinition(); + beanDefinition.setBeanClass(TracingPattern.class); + String id = element.getAttribute("id"); + if (id == null || id.length() == 0) { + id = TracingPattern.class.getName(); + int counter = 2; + while (parserContext.getRegistry().containsBeanDefinition(id)) { + id = id + (counter++); + } + } + if (id != null && id.length() > 0) { + if (parserContext.getRegistry().containsBeanDefinition(id)) { + throw new IllegalStateException("Duplicate spring bean id " + id); + } + parserContext.getRegistry().registerBeanDefinition(id, beanDefinition); + } + NamedNodeMap nnm = element.getAttributes(); + for (int i = 0; i < nnm.getLength(); i++) { + Node node = nnm.item(i); + String key = node.getLocalName(); + String value = node.getNodeValue(); + if (key.equals("entity")) { + if (parserContext.getRegistry().containsBeanDefinition(value)) { + beanDefinition.getPropertyValues().add(key, parserContext.getRegistry().getBeanDefinition(value)); + } else { + beanDefinition.getPropertyValues().add(key, new RuntimeBeanReference(value)); + } + } else { + beanDefinition.getPropertyValues().add(key, value); + } + } + + return beanDefinition; + } +} diff --git a/skywalking-sdk-plugin/spring-plugin/src/main/resources/META-INF/skywalking.xsd b/skywalking-sdk-plugin/spring-plugin/src/main/resources/META-INF/skywalking.xsd index f8fea8f21..2d9ef589a 100644 --- a/skywalking-sdk-plugin/spring-plugin/src/main/resources/META-INF/skywalking.xsd +++ b/skywalking-sdk-plugin/spring-plugin/src/main/resources/META-INF/skywalking.xsd @@ -7,20 +7,16 @@ - + - + + + + - - - - - - - \ No newline at end of file diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/AspectBean.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/AspectBean.java deleted file mode 100644 index adf2ff3ce..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/AspectBean.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.aspect; - -public class AspectBean { - public void before() { - System.out.println("before aspect"); - } - - public void after() { - System.out.println("after aspect"); - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TestAspect.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TestAspect.java deleted file mode 100644 index 9ba0f5a00..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TestAspect.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.aspect; - -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class TestAspect { - - public static void main(String[] args) { - ClassPathXmlApplicationContext classPathXmlApplicationContext = - new ClassPathXmlApplicationContext("classpath*:springConfig-aspect.xml"); - TracingAspectBean testBuriedPointBean = classPathXmlApplicationContext.getBean(TracingAspectBean.class); - testBuriedPointBean.doBusiness(); - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TracingAspectBean.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TracingAspectBean.java deleted file mode 100644 index 300f59e0b..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/aspect/TracingAspectBean.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.aspect; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -public class TracingAspectBean { - - @Tracing - public String doBusiness() { - System.out.println("do business"); - return "Hello World"; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainA.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainA.java deleted file mode 100644 index 451f3d7cf..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainA.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainA { - private CallChainB callChainB; - private CallChainD callChainD; - private CallChainF callChainF; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - callChainB.doBusiness(); - callChainD.doBusiness(); - callChainF.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - } - - public CallChainB getCallChainB() { - return callChainB; - } - - public void setCallChainB(CallChainB callChainB) { - this.callChainB = callChainB; - } - - public CallChainD getCallChainD() { - return callChainD; - } - - public void setCallChainD(CallChainD callChainD) { - this.callChainD = callChainD; - } - - public CallChainF getCallChainF() { - return callChainF; - } - - public void setCallChainF(CallChainF callChainF) { - this.callChainF = callChainF; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainB.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainB.java deleted file mode 100644 index 4d5741faf..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainB.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainB { - - private CallChainC callChainC; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - callChainC.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - } - - public CallChainC getCallChainC() { - return callChainC; - } - - public void setCallChainC(CallChainC callChainC) { - this.callChainC = callChainC; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainC.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainC.java deleted file mode 100644 index 5e5ca8310..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainC.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainC { - - private CallChainE callChainE; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - callChainE.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - } - - public CallChainE getCallChainE() { - return callChainE; - } - - public void setCallChainE(CallChainE callChainE) { - this.callChainE = callChainE; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainD.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainD.java deleted file mode 100644 index e6e52d387..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainD.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainD { - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - //System.out.println("three"); - } - -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainE.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainE.java deleted file mode 100644 index 06aad71fb..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainE.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainE { - - private CallChainG callChainG; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - callChainG.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - } - - public CallChainG getCallChainG() { - return callChainG; - } - - public void setCallChainG(CallChainG callChainG) { - this.callChainG = callChainG; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainF.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainF.java deleted file mode 100644 index 6796772b7..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainF.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainF { - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - //System.out.println("me too"); - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainG.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainG.java deleted file mode 100644 index eca0406a1..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainG.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainG { - - private CallChainH callChainH; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - callChainH.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - - } - - public CallChainH getCallChainH() { - return callChainH; - } - - public void setCallChainH(CallChainH callChainH) { - this.callChainH = callChainH; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainH.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainH.java deleted file mode 100644 index e905defad..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainH.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainH { - - private CallChainI callChainI; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - callChainI.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - - } - - public CallChainI getCallChainI() { - return callChainI; - } - - public void setCallChainI(CallChainI callChainI) { - this.callChainI = callChainI; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainI.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainI.java deleted file mode 100644 index 4d825cc38..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainI.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainI { - private CallChainJ callChainJ; - - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - callChainJ.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - - } - - public CallChainJ getCallChainJ() { - return callChainJ; - } - - public void setCallChainJ(CallChainJ callChainJ) { - this.callChainJ = callChainJ; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainJ.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainJ.java deleted file mode 100644 index a33e3fc73..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainJ.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainJ { - private CallChainK callChainK; - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - callChainK.doBusiness(); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - - } - - public CallChainK getCallChainK() { - return callChainK; - } - - public void setCallChainK(CallChainK callChainK) { - this.callChainK = callChainK; - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainK.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainK.java deleted file mode 100644 index d440c1b6e..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/CallChainK.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import com.ai.cloud.skywalking.api.BusinessKeyAppender; -import com.ai.cloud.skywalking.plugin.spring.Tracing; - -import java.util.concurrent.ThreadLocalRandom; - -public class CallChainK { - @Tracing - public void doBusiness() throws InterruptedException { - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - BusinessKeyAppender.setBusinessKey2Trace("key-value"); - Thread.sleep(ThreadLocalRandom.current().nextInt(10)); - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/TestBuriedPoint.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/TestBuriedPoint.java deleted file mode 100644 index 395a85df0..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/common/TestBuriedPoint.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.ai.cloud.skywalking.plugin.spring.common; - -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicInteger; - -public class TestBuriedPoint extends Thread { - - private static final int MAX_THREAD_SIZE = 1; - private CallChainA callChainA; - AtomicInteger atomicInteger = new AtomicInteger(); - - public TestBuriedPoint(CallChainA callChainA) { - this.callChainA = callChainA; - } - - private Timer timer = new Timer(Thread.currentThread().getName()); - - public TestBuriedPoint() { - timer.schedule(new TimerTask() { - @Override - public void run() { - System.out.println(atomicInteger.get()); - } - }, 1000); - - } - - @Override - public void run() { - for (int i = 0 ; i < 1; i++) { - try { - Thread.sleep(2L); - } catch (InterruptedException e) { - e.printStackTrace(); - } - try { - callChainA.doBusiness(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.out.println(Thread.currentThread().getName() + "\t" + atomicInteger.incrementAndGet()); - } - } - - public static void main(String[] args) throws InterruptedException { - ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:springConfig-common.xml"); - CallChainA callChainA = classPathXmlApplicationContext.getBean(CallChainA.class); - CountDownLatch countDownLatch = new CountDownLatch(10); - for (int i = 0; i < MAX_THREAD_SIZE; i++) { - new TestBuriedPoint(callChainA).start(); - } - - countDownLatch.await(); - - } -} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/test/AppTest01.java b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/test/AppTest01.java new file mode 100644 index 000000000..768b2e98f --- /dev/null +++ b/skywalking-sdk-plugin/spring-plugin/src/test/java/com/ai/cloud/skywalking/plugin/spring/test/AppTest01.java @@ -0,0 +1,19 @@ +package com.ai.cloud.skywalking.plugin.spring.test; + +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class AppTest01 { + + public void testPrintln(String value){ + System.out.println(value); + } + + + @Test + public void testBean(){ + ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:springConfig-common.xml"); + AppTest01 appTest01 = classPathXmlApplicationContext.getBean(AppTest01.class); + appTest01.testPrintln("Hello World"); + } +} diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-aspect.xml b/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-aspect.xml deleted file mode 100644 index 09ad933a0..000000000 --- a/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-aspect.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-common.xml b/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-common.xml index 70d564414..4f29f58f8 100644 --- a/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-common.xml +++ b/skywalking-sdk-plugin/spring-plugin/src/test/resources/springConfig-common.xml @@ -2,40 +2,16 @@ + http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://cloud.asiainfo.com/schema/skywalking + http://cloud.asiainfo.com/schema/skywalking/skywalking.xsd"> - - - - - - - - - - - - + + + + + - - - - - - - - - - - - - - - - - - - \ No newline at end of file