From f377c3350e971d21173db8dcd60cbc0ee527fa09 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Thu, 25 Jan 2018 21:58:57 +0800 Subject: [PATCH] [Agent] support spring bean plugin --- apm-sniffer/apm-sdk-plugin/pom.xml | 7 ++- .../apm-sdk-plugin/spring-plugins/pom.xml | 1 + .../spring-annotation-plugin/README.md | 18 +++++++ .../spring-annotation-plugin/pom.xml | 44 +++++++++++++++ .../AbstractSpringBeanInstrumentation.java | 53 +++++++++++++++++++ .../SpringAnnotationInterceptor.java | 46 ++++++++++++++++ .../bean/SpringBeanInstrumentation.java | 34 ++++++++++++ .../SpringComponentInstrumentation.java | 31 +++++++++++ .../SpringRepositoryInstrumentation.java | 30 +++++++++++ .../SpringServicesInstrumentation.java | 31 +++++++++++ .../src/main/resources/skywalking-plugin.def | 4 ++ 11 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/README.md create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/AbstractSpringBeanInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/SpringAnnotationInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/bean/SpringBeanInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/component/SpringComponentInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/repository/SpringRepositoryInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/services/SpringServicesInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/resources/skywalking-plugin.def diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index 4b81d1d8c..dba83ca90 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -64,6 +64,9 @@ net.bytebuddy ${shade.package}.${shade.net.bytebuddy.source} + + ${project.build.directory}${sdk.plugin.related.dir}/../../../../packages/skywalking-agent + ${agent.package.dest.dir}/plugins @@ -167,10 +170,10 @@ + dir="${plugin.dest.dir}"/> diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml index 0aa9d4512..71764b541 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/pom.xml @@ -36,6 +36,7 @@ mvc-annotation-3.x-plugin core-patch mvc-annotation-commons + spring-annotation-plugin pom diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/README.md b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/README.md new file mode 100644 index 000000000..6cf0c4c1f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/README.md @@ -0,0 +1,18 @@ +# Spring Annotation Plugin (Optional) + +Date: 2018/01/24 + +## Purpose +Because of multiple project are used + +Spring annotation plugin is an Optional plugin, the plugin +and if you want to active the plugin, you should move the plugin jar to the plugins folder. +The plugin will intercept all public method in the classs annotation with the following annotation. +1. `@Bean` +2. `@Service` +3. `@Componet` +4. `@Repository` + +## Purpose + + diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/pom.xml new file mode 100644 index 000000000..df46249b9 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/pom.xml @@ -0,0 +1,44 @@ + + + + + + spring-plugins + org.apache.skywalking + 5.0.0-alpha + + 4.0.0 + + apm-spring-annotation-plugin + + + ${agent.package.dest.dir}/optional-plugins + + + + + org.springframework + spring-context + 3.2.0.RELEASE + provided + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/AbstractSpringBeanInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/AbstractSpringBeanInstrumentation.java new file mode 100644 index 000000000..1bee08a51 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/AbstractSpringBeanInstrumentation.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; + +import static net.bytebuddy.matcher.ElementMatchers.isPublic; + +public abstract class AbstractSpringBeanInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.spring.annotations.SpringAnnotationInterceptor"; + + @Override protected final ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override protected final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return isPublic(); + } + + @Override public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override public boolean isOverrideArgs() { + return false; + } + } + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/SpringAnnotationInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/SpringAnnotationInterceptor.java new file mode 100644 index 000000000..ad66e1d21 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/SpringAnnotationInterceptor.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; + +public class SpringAnnotationInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + String operationName = objInst.getClass().getName() + "." + method.getName(); + ContextManager.createLocalSpan(operationName); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/bean/SpringBeanInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/bean/SpringBeanInstrumentation.java new file mode 100644 index 000000000..366e3c55e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/bean/SpringBeanInstrumentation.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations.bean; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.spring.annotations.AbstractSpringBeanInstrumentation; + +import static org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch.byClassAnnotationMatch; + +/** + * + */ +public class SpringBeanInstrumentation extends AbstractSpringBeanInstrumentation { + + @Override protected ClassMatch enhanceClass() { + return byClassAnnotationMatch(new String[] {"org.springframework.context.annotation.Bean"}); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/component/SpringComponentInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/component/SpringComponentInstrumentation.java new file mode 100644 index 000000000..e65a98d25 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/component/SpringComponentInstrumentation.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations.component; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.spring.annotations.AbstractSpringBeanInstrumentation; + +import static org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch.byClassAnnotationMatch; + +public class SpringComponentInstrumentation extends AbstractSpringBeanInstrumentation { + + @Override protected ClassMatch enhanceClass() { + return byClassAnnotationMatch(new String[] {"org.springframework.stereotype.Component"}); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/repository/SpringRepositoryInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/repository/SpringRepositoryInstrumentation.java new file mode 100644 index 000000000..c97c32656 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/repository/SpringRepositoryInstrumentation.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations.repository; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.spring.annotations.AbstractSpringBeanInstrumentation; + +import static org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch.byClassAnnotationMatch; + +public class SpringRepositoryInstrumentation extends AbstractSpringBeanInstrumentation { + @Override protected ClassMatch enhanceClass() { + return byClassAnnotationMatch(new String[] {"org.springframework.stereotype.Repository"}); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/services/SpringServicesInstrumentation.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/services/SpringServicesInstrumentation.java new file mode 100644 index 000000000..b38bfcb50 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/services/SpringServicesInstrumentation.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.spring.annotations.services; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.spring.annotations.AbstractSpringBeanInstrumentation; + +import static org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch.byClassAnnotationMatch; + +public class SpringServicesInstrumentation extends AbstractSpringBeanInstrumentation { + + @Override protected ClassMatch enhanceClass() { + return byClassAnnotationMatch(new String[] {"org.springframework.stereotype.Service"}); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 000000000..e69e32aa2 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-annotation-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,4 @@ +spring-annotation=org.apache.skywalking.apm.plugin.spring.annotations.bean.SpringBeanInstrumentation +spring-annotation=org.apache.skywalking.apm.plugin.spring.annotations.component.SpringComponentInstrumentation +spring-annotation=org.apache.skywalking.apm.plugin.spring.annotations.repository.SpringRepositoryInstrumentation +spring-annotation=org.apache.skywalking.apm.plugin.spring.annotations.services.SpringServicesInstrumentation