You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2018/08/08 14:40:40 UTC

[GitHub] wu-sheng closed pull request #950: [Agent] Provide plugin for Tomcat 6.x

wu-sheng closed pull request #950: [Agent] Provide plugin for Tomcat 6.x
URL: https://github.com/apache/incubator-skywalking/pull/950
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml
index 5f780d72e..e991303f4 100644
--- a/apm-sniffer/apm-sdk-plugin/pom.xml
+++ b/apm-sniffer/apm-sdk-plugin/pom.xml
@@ -56,6 +56,7 @@
         <module>kafka-v1-plugin</module>
         <module>servicecomb-plugin</module>
         <module>hystrix-1.x-plugin</module>
+        <module>tomcat-6.x-plugin</module>
     </modules>
     <packaging>pom</packaging>
 
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/pom.xml
new file mode 100644
index 000000000..0492db402
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/pom.xml
@@ -0,0 +1,55 @@
+<!--
+  ~ 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.
+  ~
+  -->
+
+<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>apm-sdk-plugin</artifactId>
+        <groupId>org.apache.skywalking</groupId>
+        <version>5.0.0-alpha-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>tomcat-6.x-plugin</artifactId>
+    <packaging>jar</packaging>
+
+    <name>tomcat-6.x-plugin</name>
+    <url>http://maven.apache.org</url>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>2.5.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+            <version>6.0.20</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatExceptionInterceptor.java b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatExceptionInterceptor.java
new file mode 100644
index 000000000..f32e5715a
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatExceptionInterceptor.java
@@ -0,0 +1,44 @@
+/*
+ * 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.tomcat6x;
+
+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 TomcatExceptionInterceptor implements InstanceMethodsAroundInterceptor {
+    @Override
+    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+        MethodInterceptResult result) throws Throwable {
+        ContextManager.activeSpan().errorOccurred().log((Throwable)allArguments[2]);
+    }
+
+    @Override
+    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+        Object ret) throws Throwable {
+        return ret;
+    }
+
+    @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Throwable t) {
+
+    }
+}
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatInvokeInterceptor.java
new file mode 100644
index 000000000..c3e54e1b5
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/TomcatInvokeInterceptor.java
@@ -0,0 +1,77 @@
+/*
+ * 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.tomcat6x;
+
+import java.lang.reflect.Method;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.catalina.connector.Response;
+import org.apache.skywalking.apm.agent.core.context.CarrierItem;
+import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.tag.Tags;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
+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;
+import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
+
+/**
+ * Fetch the serialized context data by using <code>javax.servlet.http.HttpServletRequest</code>.
+ */
+public class TomcatInvokeInterceptor implements InstanceMethodsAroundInterceptor {
+
+    @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
+        Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
+        HttpServletRequest request = (HttpServletRequest)allArguments[0];
+        ContextCarrier contextCarrier = new ContextCarrier();
+
+        CarrierItem next = contextCarrier.items();
+        while (next.hasNext()) {
+            next = next.next();
+            next.setHeadValue(request.getHeader(next.getHeadKey()));
+        }
+
+        AbstractSpan span = ContextManager.createEntrySpan(request.getRequestURI(), contextCarrier);
+        Tags.URL.set(span, request.getRequestURL().toString());
+        Tags.HTTP.METHOD.set(span, request.getMethod());
+        span.setComponent(ComponentsDefine.TOMCAT);
+        SpanLayer.asHttp(span);
+
+    }
+
+    @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Object ret) throws Throwable {
+        Response response = (Response)allArguments[1];
+        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) {
+        AbstractSpan span = ContextManager.activeSpan();
+        span.log(t);
+        span.errorOccurred();
+    }
+}
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/define/TomcatInstrumentation.java b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/define/TomcatInstrumentation.java
new file mode 100644
index 000000000..3f3e5979e
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat6x/define/TomcatInstrumentation.java
@@ -0,0 +1,94 @@
+/*
+ * 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.tomcat6x.define;
+
+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 org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
+
+/**
+ * @author liyuntao
+ */
+public class TomcatInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
+
+    private static final String ENHANCE_CLASS = "org.apache.catalina.core.StandardWrapperValve";
+    private static final String INVOKE_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.tomcat6x.TomcatInvokeInterceptor";
+    private static final String EXCEPTION_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.tomcat6x.TomcatExceptionInterceptor";
+
+    @Override
+    protected ClassMatch enhanceClass() {
+        return byName(ENHANCE_CLASS);
+    }
+
+    @Override
+    protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+        return null;
+    }
+
+    @Override
+    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
+        return new InstanceMethodsInterceptPoint[] {
+            new InstanceMethodsInterceptPoint() {
+                @Override
+                public ElementMatcher<MethodDescription> getMethodsMatcher() {
+                    return named("invoke");
+                }
+
+                @Override
+                public String getMethodsInterceptor() {
+                    return INVOKE_INTERCEPT_CLASS;
+                }
+
+                @Override
+                public boolean isOverrideArgs() {
+                    return false;
+                }
+            },
+            new InstanceMethodsInterceptPoint() {
+                @Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
+                    return named("exception");
+                }
+
+                @Override public String getMethodsInterceptor() {
+                    return EXCEPTION_INTERCEPT_CLASS;
+                }
+
+                @Override public boolean isOverrideArgs() {
+                    return false;
+                }
+            }
+        };
+    }
+
+    @Override
+    protected String[] witnessClasses() {
+        /**
+         * @see <code>org.apache.AnnotationProcessor<code/>
+         */
+        return new String[] {
+            "org.apache.AnnotationProcessor"
+        };
+    }
+}
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/resources/skywalking-plugin.def
new file mode 100644
index 000000000..215d8c9d7
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-6.x-plugin/src/main/resources/skywalking-plugin.def
@@ -0,0 +1 @@
+tomcat-6.x=org.apache.skywalking.apm.plugin.tomcat6x.define.TomcatInstrumentation
diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/define/TomcatInstrumentation.java b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/define/TomcatInstrumentation.java
index 23d127383..7a58dd509 100644
--- a/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/define/TomcatInstrumentation.java
+++ b/apm-sniffer/apm-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat78x/define/TomcatInstrumentation.java
@@ -93,4 +93,13 @@ public boolean isOverrideArgs() {
             }
         };
     }
+    @Override
+    protected String[] witnessClasses() {
+        /**
+         * @see <code>javax.servlet.annotation.HandlesTypes<code/>
+         */
+        return new String[] {
+            "javax.servlet.annotation.HandlesTypes"
+        };
+    }
 }
diff --git a/ci-dependencies/catalina.jar b/ci-dependencies/catalina.jar
new file mode 100644
index 000000000..09ca71777
Binary files /dev/null and b/ci-dependencies/catalina.jar differ
diff --git a/ci-dependencies/servlet-api.jar b/ci-dependencies/servlet-api.jar
new file mode 100644
index 000000000..2b48572cd
Binary files /dev/null and b/ci-dependencies/servlet-api.jar differ
diff --git a/pom.xml b/pom.xml
index b86474fb2..1753c7c9b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -225,6 +225,46 @@
                             <workingDirectory>${project.basedir}/ci-dependencies</workingDirectory>
                         </configuration>
                     </execution>
+                    <execution>
+                        <id>install-tomcat6</id>
+                        <inherited>false</inherited>
+                        <phase>process-resources</phase>
+                        <goals>
+                            <goal>exec</goal>
+                        </goals>
+                        <configuration>
+                            <executable>mvn</executable>
+                            <arguments>
+                                <argument>install:install-file</argument>
+                                <argument>-Dfile=catalina.jar</argument>
+                                <argument>-DgroupId=org.apache.tomcat.embed</argument>
+                                <argument>-DartifactId=tomcat-embed-core</argument>
+                                <argument>-Dversion=6.0.20</argument>
+                                <argument>-Dpackaging=jar</argument>
+                            </arguments>
+                            <workingDirectory>${project.basedir}/ci-dependencies</workingDirectory>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>install-servlet-api</id>
+                        <inherited>false</inherited>
+                        <phase>process-resources</phase>
+                        <goals>
+                            <goal>exec</goal>
+                        </goals>
+                        <configuration>
+                            <executable>mvn</executable>
+                            <arguments>
+                                <argument>install:install-file</argument>
+                                <argument>-Dfile=servlet-api.jar</argument>
+                                <argument>-DgroupId=javax.servlet</argument>
+                                <argument>-DartifactId=javax.servlet-api</argument>
+                                <argument>-Dversion=2.5.0</argument>
+                                <argument>-Dpackaging=jar</argument>
+                            </arguments>
+                            <workingDirectory>${project.basedir}/ci-dependencies</workingDirectory>
+                        </configuration>
+                    </execution>
                 </executions>
             </plugin>
             <plugin>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services