wu-sheng closed pull request #771: Add [Agent] ServiceComb Plugin #437
URL: https://github.com/apache/incubator-skywalking/pull/771
 
 
   

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-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/table/register/ServerTypeDefine.java
 
b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/table/register/ServerTypeDefine.java
index 2810c2ab1..71e2ae346 100644
--- 
a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/table/register/ServerTypeDefine.java
+++ 
b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/table/register/ServerTypeDefine.java
@@ -32,7 +32,7 @@
     private ServerType[] serverTypes;
 
     private ServerTypeDefine() {
-        this.serverTypes = new ServerType[28];
+        this.serverTypes = new ServerType[29];
         this.serverTypeNames = new String[11];
         addServerType(new ServerType(ComponentsDefine.TOMCAT.getId(), 
Const.NONE, Const.EMPTY_STRING));
         addServerType(new ServerType(ComponentsDefine.HTTPCLIENT.getId(), 
Const.NONE, Const.EMPTY_STRING));
@@ -61,6 +61,7 @@ private ServerTypeDefine() {
         addServerType(new ServerType(ComponentsDefine.ROCKET_MQ.getId(), 9, 
ComponentsDefine.ROCKET_MQ.getName()));
         addServerType(new 
ServerType(ComponentsDefine.HTTP_ASYNC_CLIENT.getId(), Const.NONE, 
Const.EMPTY_STRING));
         addServerType(new ServerType(ComponentsDefine.KAFKA.getId(), 10, 
ComponentsDefine.KAFKA.getName()));
+        addServerType(new ServerType(ComponentsDefine.SERVICECOMB.getId(), 
Const.NONE, ComponentsDefine.SERVICECOMB.getName()));
     }
 
     public static ServerTypeDefine getInstance() {
diff --git 
a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java
 
b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java
index 220b1062c..fa6e9c24e 100644
--- 
a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java
+++ 
b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java
@@ -79,6 +79,8 @@
     public static final OfficialComponent HTTP_ASYNC_CLIENT = new 
OfficialComponent(26, "httpasyncclient");
 
     public static final OfficialComponent KAFKA = new OfficialComponent(27, 
"Kafka");
+  
+    public static final OfficialComponent SERVICECOMB = new 
OfficialComponent(28, "ServiceComb");
 
     private static ComponentsDefine INSTANCE = new ComponentsDefine();
 
@@ -89,7 +91,7 @@ public static ComponentsDefine getInstance() {
     }
 
     public ComponentsDefine() {
-        components = new String[28];
+        components = new String[29];
         addComponent(TOMCAT);
         addComponent(HTTPCLIENT);
         addComponent(DUBBO);
@@ -117,6 +119,7 @@ public ComponentsDefine() {
         addComponent(ROCKET_MQ);
         addComponent(HTTP_ASYNC_CLIENT);
         addComponent(KAFKA);
+        addComponent(SERVICECOMB);
     }
 
     private void addComponent(OfficialComponent component) {
diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml 
b/apm-sniffer/apm-sdk-plugin/pom.xml
index a4acaca4b..be0a02e80 100644
--- a/apm-sniffer/apm-sdk-plugin/pom.xml
+++ b/apm-sniffer/apm-sdk-plugin/pom.xml
@@ -54,6 +54,7 @@
         <module>mongodb-2.x-plugin</module>
         <module>httpasyncclient-4.x-plugin</module>
         <module>kafka-v1-plugin</module>
+        <module>servicecomb-plugin</module>
     </modules>
     <packaging>pom</packaging>
 
diff --git a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml
new file mode 100644
index 000000000..1668c7020
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <parent>
+        <artifactId>apm-sdk-plugin</artifactId>
+        <groupId>org.apache.skywalking</groupId>
+        <version>5.0.0-alpha</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>servicecomb-plugin</artifactId>
+
+    <modules>
+        <module>servicecomb-java-chassis-0.x-plugin</module>
+    </modules>
+    <packaging>pom</packaging>
+
+    <name>apm-sdk-plugin</name>
+    <url>http://maven.apache.org</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <sdk.plugin.related.dir>/..</sdk.plugin.related.dir>
+    </properties>
+</project>
\ No newline at end of file
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/pom.xml
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/pom.xml
new file mode 100644
index 000000000..bdac6da88
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/pom.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <parent>
+        <artifactId>servicecomb-plugin</artifactId>
+        <groupId>org.apache.skywalking</groupId>
+        <version>5.0.0-alpha</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>apm-servicecomb-java-chassis-0.x-plugin</artifactId>
+    <packaging>jar</packaging>
+
+    <name>servicecomb-java-chassis-0.x-plugin</name>
+    <url>http://maven.apache.org</url>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>io.servicecomb</groupId>
+            <artifactId>java-chassis-core</artifactId>
+            <version>0.5.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <!-- ???? -->
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <!-- ??????????????? -->
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <phase>none</phase>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptor.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptor.java
new file mode 100644
index 000000000..4c0e97840
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptor.java
@@ -0,0 +1,80 @@
+/*
+ * 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.servicecomb;
+
+import io.servicecomb.core.Invocation;
+import java.lang.reflect.Method;
+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;
+
+/**
+ * 
{org.apache.skywalking.apm.plugin.servicecomb.ProducerOperationHandlerInterceptor}
 define how to enhance class {@link
+ * ProducerOperationHandler#handle(io.servicecomb.core.Invocation, 
io.servicecomb.swagger.invocation.AsyncResponse)}.
+ *
+ * @author lytscu
+ */
+public class ProducerOperationHandlerInterceptor implements 
InstanceMethodsAroundInterceptor {
+
+    @Override
+    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] 
allArguments,
+        Class<?>[] argumentsTypes, MethodInterceptResult result) throws 
Throwable {
+        Invocation invocation = (Invocation)allArguments[0];
+        ContextCarrier contextCarrier = new ContextCarrier();
+        CarrierItem next = contextCarrier.items();
+        while (next.hasNext()) {
+            next = next.next();
+            next.setHeadValue(invocation.getContext().get(next.getHeadKey()));
+        }
+        String operationName = invocation.getMicroserviceQualifiedName();
+        AbstractSpan span = ContextManager.createEntrySpan(operationName, 
contextCarrier);
+        String url = invocation.getOperationMeta().getOperationPath();
+        Tags.URL.set(span, url);
+        span.setComponent(ComponentsDefine.SERVICECOMB);
+        SpanLayer.asRPCFramework(span);
+    }
+
+    @Override public Object afterMethod(EnhancedInstance objInst, Method 
method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Object ret) throws Throwable {
+        Invocation invocation = (Invocation)allArguments[0];
+        AbstractSpan span = ContextManager.activeSpan();
+        int statusCode = invocation.getStatus().getStatusCode();
+        if (statusCode >= 400) {
+            span.errorOccurred();
+            Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
+        }
+        ContextManager.stopSpan();
+        return ret;
+    }
+
+    @Override public void handleMethodException(EnhancedInstance objInst, 
Method method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Throwable t) {
+        AbstractSpan span = ContextManager.activeSpan();
+        span.errorOccurred();
+        span.log(t);
+    }
+
+}
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptor.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptor.java
new file mode 100644
index 000000000..c3d40d339
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptor.java
@@ -0,0 +1,104 @@
+/*
+ * 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.servicecomb;
+
+import io.servicecomb.core.Invocation;
+import java.lang.reflect.Method;
+import java.net.URI;
+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;
+
+/**
+ * {@link TransportClientHandlerInterceptor} define how to enhance class 
{@link TransportClientHandler#handle(io.servicecomb.core.Invocation,
+ * io.servicecomb.swagger.invocation.AsyncResponse)}.
+ *
+ * @author lytscu
+ */
+public class TransportClientHandlerInterceptor implements 
InstanceMethodsAroundInterceptor {
+
+    @Override
+    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] 
allArguments,
+        Class<?>[] argumentsTypes, MethodInterceptResult result) throws 
Throwable {
+        Invocation invocation = (Invocation)allArguments[0];
+        if (!checkRegisterStatus(invocation)) {
+            return;
+        }
+        URI uri = new URI(invocation.getEndpoint().toString());
+        String peer = uri.getHost() + ":" + uri.getPort();
+        String operationName = invocation.getMicroserviceQualifiedName();
+        final ContextCarrier contextCarrier = new ContextCarrier();
+        AbstractSpan span = ContextManager.createExitSpan(operationName, 
contextCarrier, peer);
+        CarrierItem next = contextCarrier.items();
+        while (next.hasNext()) {
+            next = next.next();
+            invocation.getContext().put(next.getHeadKey(), 
next.getHeadValue());
+        }
+        String url = invocation.getOperationMeta().getOperationPath();
+        Tags.URL.set(span, url);
+        span.setComponent(ComponentsDefine.SERVICECOMB);
+        SpanLayer.asRPCFramework(span);
+    }
+
+    @Override public Object afterMethod(EnhancedInstance objInst, Method 
method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Object ret) throws Throwable {
+        Invocation invocation = (Invocation)allArguments[0];
+        if (!checkRegisterStatus(invocation)) {
+            return ret;
+        }
+        AbstractSpan span = ContextManager.activeSpan();
+        int statusCode = invocation.getStatus().getStatusCode();
+        if (statusCode >= 400) {
+            span.errorOccurred();
+            Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
+        }
+        ContextManager.stopSpan();
+        return ret;
+    }
+
+    @Override public void handleMethodException(EnhancedInstance objInst, 
Method method, Object[] allArguments,
+        Class<?>[] argumentsTypes, Throwable t) {
+        Invocation invocation = (Invocation)allArguments[0];
+        if (!checkRegisterStatus(invocation)) {
+            return;
+        }
+        AbstractSpan span = ContextManager.activeSpan();
+        span.errorOccurred();
+        span.log(t);
+    }
+
+    /**
+     * Serviecomb chassis Consumers and providers need to register at the 
service center. If the consumer is not
+     * registered then return false.
+     */
+    private Boolean checkRegisterStatus(Invocation invocation) {
+        if (null == invocation.getOperationMeta() || null == 
invocation.getEndpoint()) {
+            return false;
+        }
+        return true;
+    }
+
+}
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/ProducerOperationHandlerInstrumentation.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/ProducerOperationHandlerInstrumentation.java
new file mode 100644
index 000000000..3151782b8
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/ProducerOperationHandlerInstrumentation.java
@@ -0,0 +1,74 @@
+/*
+ * 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.servicecomb.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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+
+/**
+ * {@link ProducerOperationHandlerInstrumentation} represents that skywalking 
intercept {@link
+ * ProducerOperationHandler#handle(io.servicecomb.core.Invocation, 
io.servicecomb.swagger.invocation.AsyncResponse)}by
+ * using {@link ProducerOperationHandlerInterceptor}
+ *
+ * @author lytscu
+ */
+public class ProducerOperationHandlerInstrumentation extends 
ClassInstanceMethodsEnhancePluginDefine {
+
+    private static final String ENHANCE_CLASS = 
"io.servicecomb.core.handler.impl.ProducerOperationHandler";
+
+    private static final String INTERCEPT_CLASS = 
"org.apache.skywalking.apm.plugin.servicecomb.ProducerOperationHandlerInterceptor";
+
+    @Override
+    protected ClassMatch enhanceClass() {
+        return NameMatch.byName(ENHANCE_CLASS);
+    }
+
+    @Override
+    protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+        return new ConstructorInterceptPoint[0];
+    }
+
+    @Override
+    protected InstanceMethodsInterceptPoint[] 
getInstanceMethodsInterceptPoints() {
+        return new InstanceMethodsInterceptPoint[] {
+            new InstanceMethodsInterceptPoint() {
+                @Override
+                public ElementMatcher<MethodDescription> getMethodsMatcher() {
+                    return named("handle");
+                }
+
+                @Override
+                public String getMethodsInterceptor() {
+                    return INTERCEPT_CLASS;
+                }
+
+                @Override public boolean isOverrideArgs() {
+                    return false;
+                }
+            }
+        };
+    }
+}
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/TransportClientHandlerInstrumentation.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/TransportClientHandlerInstrumentation.java
new file mode 100644
index 000000000..5c0848243
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/define/TransportClientHandlerInstrumentation.java
@@ -0,0 +1,74 @@
+/*
+ * 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.servicecomb.define;
+
+import io.servicecomb.core.handler.impl.TransportClientHandler;
+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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+
+/**
+ * {@link TransportClientHandlerInstrumentation} represents that skywalking 
intercept {@link TransportClientHandler}by
+ * using {@linkTransportClientHandlerInterceptor }
+ *
+ * @author lytscu
+ */
+public class TransportClientHandlerInstrumentation extends 
ClassInstanceMethodsEnhancePluginDefine {
+
+    private static final String ENHANCE_CLASS = 
"io.servicecomb.core.handler.impl.TransportClientHandler";
+
+    private static final String INTERCEPT_CLASS = 
"org.apache.skywalking.apm.plugin.servicecomb.TransportClientHandlerInterceptor";
+
+    @Override
+    protected ClassMatch enhanceClass() {
+        return NameMatch.byName(ENHANCE_CLASS);
+    }
+
+    @Override
+    protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+        return new ConstructorInterceptPoint[0];
+    }
+
+    @Override
+    protected InstanceMethodsInterceptPoint[] 
getInstanceMethodsInterceptPoints() {
+        return new InstanceMethodsInterceptPoint[] {
+            new InstanceMethodsInterceptPoint() {
+                @Override
+                public ElementMatcher<MethodDescription> getMethodsMatcher() {
+                    return named("handle");
+                }
+
+                @Override
+                public String getMethodsInterceptor() {
+                    return INTERCEPT_CLASS;
+                }
+
+                @Override public boolean isOverrideArgs() {
+                    return false;
+                }
+            },
+        };
+    }
+}
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/resources/skywalking-plugin.def
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/resources/skywalking-plugin.def
new file mode 100644
index 000000000..950da2df7
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/main/resources/skywalking-plugin.def
@@ -0,0 +1,2 @@
+servicecomb-0.x=org.apache.skywalking.apm.plugin.servicecomb.define.ProducerOperationHandlerInstrumentation
+servicecomb-0.x=org.apache.skywalking.apm.plugin.servicecomb.define.TransportClientHandlerInstrumentation
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptorTest.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptorTest.java
new file mode 100644
index 000000000..f97d9e8e4
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/ProducerOperationHandlerInterceptorTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.servicecomb;
+
+import io.servicecomb.core.Endpoint;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.core.definition.OperationMeta;
+import io.servicecomb.core.definition.SchemaMeta;
+import io.servicecomb.swagger.invocation.InvocationType;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import java.lang.reflect.Method;
+import java.util.List;
+import javax.ws.rs.core.Response;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
+import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
+import org.apache.skywalking.apm.agent.core.context.util.KeyValuePair;
+import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
+import org.apache.skywalking.apm.agent.test.helper.SpanHelper;
+import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
+import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.modules.junit4.PowerMockRunnerDelegate;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockRunnerDelegate(TracingSegmentRunner.class)
+public class ProducerOperationHandlerInterceptorTest {
+
+    @Rule
+    public AgentServiceRule agentServiceRule = new AgentServiceRule();
+    @Mock
+    Response.StatusType statusType;
+    @SegmentStoragePoint
+    private SegmentStorage segmentStorage;
+    private ProducerOperationHandlerInterceptor invocationInterceptor;
+    @Mock
+    private OperationMeta operationMeta;
+    @Mock
+    private EnhancedInstance enhancedInstance;
+    @Mock
+    private Invocation invocation;
+    @Mock
+    private Endpoint endpoint;
+    @Mock
+    private Method method;
+    @Mock
+    private SwaggerInvocation swagger;
+    private Object[] allArguments;
+    private Class[] argumentsType;
+    private Object[] swaggerArguments;
+
+    @Mock
+    private SchemaMeta schemaMeta;
+
+    @Before
+    public void setUp() throws Exception {
+        ServiceManager.INSTANCE.boot();
+        invocationInterceptor = new ProducerOperationHandlerInterceptor();
+        PowerMockito.mock(Invocation.class);
+        when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+        when(endpoint.getAddress()).thenReturn("0.0.0.0:7777");
+        when(invocation.getEndpoint()).thenReturn(endpoint);
+        
when(invocation.getMicroserviceQualifiedName()).thenReturn("productorTest");
+        when(operationMeta.getOperationPath()).thenReturn("/bmi");
+        when(invocation.getOperationMeta()).thenReturn(operationMeta);
+        when(invocation.getStatus()).thenReturn(statusType);
+        when(statusType.getStatusCode()).thenReturn(200);
+        when(method.getName()).thenReturn("producer");
+        
when(invocation.getInvocationType()).thenReturn(InvocationType.PRODUCER);
+        Config.Agent.APPLICATION_CODE = "serviceComnTestCases-APP";
+
+        allArguments = new Object[] {invocation,};
+        argumentsType = new Class[] {};
+        swaggerArguments = new Class[] {};
+
+    }
+
+    @Test
+    public void testConsumer() throws Throwable {
+
+        invocationInterceptor.beforeMethod(enhancedInstance, method, 
allArguments, argumentsType, null);
+        invocationInterceptor.afterMethod(enhancedInstance, method, 
allArguments, argumentsType, null);
+
+        Assert.assertThat(segmentStorage.getTraceSegments().size(), is(1));
+        TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+
+        List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
+        assertCombSpan(spans.get(0));
+        verify(invocation, times(1)).getContext();
+    }
+
+    private void assertCombSpan(AbstractTracingSpan span) {
+        assertThat(span.getOperationName(), is("productorTest"));
+        assertThat(SpanHelper.getComponentId(span), is(28));
+        List<KeyValuePair> tags = SpanHelper.getTags(span);
+        assertThat(tags.get(0).getValue(), is("/bmi"));
+        assertThat(span.isEntry(), is(true));
+    }
+
+}
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptorTest.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptorTest.java
new file mode 100644
index 000000000..f31265751
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-0.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/TransportClientHandlerInterceptorTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.servicecomb;
+
+import io.servicecomb.core.Endpoint;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.core.definition.OperationMeta;
+import io.servicecomb.core.definition.SchemaMeta;
+import io.servicecomb.core.provider.consumer.ReferenceConfig;
+import io.servicecomb.swagger.invocation.InvocationType;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import java.util.List;
+import javax.ws.rs.core.Response;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
+import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
+import org.apache.skywalking.apm.agent.core.context.util.KeyValuePair;
+import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
+import org.apache.skywalking.apm.agent.test.helper.SpanHelper;
+import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
+import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.modules.junit4.PowerMockRunnerDelegate;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockRunnerDelegate(TracingSegmentRunner.class)
+public class TransportClientHandlerInterceptorTest {
+
+    @Rule
+    public AgentServiceRule agentServiceRule = new AgentServiceRule();
+    @Mock
+    Response.StatusType statusType;
+    @Mock
+    ReferenceConfig referenceConfig;
+    @SegmentStoragePoint
+    private SegmentStorage segmentStorage;
+    private TransportClientHandlerInterceptor nextInterceptor;
+    @Mock
+    private OperationMeta operationMeta;
+    @Mock
+    private EnhancedInstance enhancedInstance;
+    @Mock
+    private Invocation invocation;
+    @Mock
+    private Endpoint endpoint;
+    @Mock
+    private SwaggerInvocation swagger;
+    private Object[] allArguments;
+    private Class[] argumentsType;
+    private Object[] swaggerArguments;
+
+    @Mock
+    private SchemaMeta schemaMeta;
+
+    @Before
+    public void setUp() throws Exception {
+        ServiceManager.INSTANCE.boot();
+        nextInterceptor = new TransportClientHandlerInterceptor();
+        PowerMockito.mock(Invocation.class);
+        when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+        when(endpoint.getAddress()).thenReturn("0.0.0.0:7777");
+        when(invocation.getEndpoint()).thenReturn(endpoint);
+        
when(invocation.getMicroserviceQualifiedName()).thenReturn("consumerTest");
+        when(operationMeta.getOperationPath()).thenReturn("/bmi");
+        when(invocation.getOperationMeta()).thenReturn(operationMeta);
+        when(invocation.getStatus()).thenReturn(statusType);
+        when(statusType.getStatusCode()).thenReturn(200);
+        
when(invocation.getInvocationType()).thenReturn(InvocationType.CONSUMER);
+        Config.Agent.APPLICATION_CODE = "serviceComnTestCases-APP";
+
+        allArguments = new Object[] {invocation,};
+        argumentsType = new Class[] {};
+        swaggerArguments = new Class[] {};
+    }
+
+    @Test
+    public void testConsumer() throws Throwable {
+        nextInterceptor.beforeMethod(enhancedInstance, null, allArguments, 
argumentsType, null);
+        nextInterceptor.afterMethod(enhancedInstance, null, allArguments, 
argumentsType, null);
+        Assert.assertThat(segmentStorage.getTraceSegments().size(), is(1));
+        TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+        List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
+        assertCombSpan(spans.get(0));
+        verify(invocation, times(1)).getContext();
+    }
+
+    private void assertCombSpan(AbstractTracingSpan span) {
+        assertThat(span.getOperationName(), is("consumerTest"));
+        assertThat(SpanHelper.getComponentId(span), is(28));
+        List<KeyValuePair> tags = SpanHelper.getTags(span);
+        assertThat(tags.get(0).getValue(), is("/bmi"));
+        assertThat(span.isExit(), is(true));
+    }
+
+}


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to