wu-sheng closed pull request #826: Add [Agent] V1 servicecomb  plugin
URL: https://github.com/apache/incubator-skywalking/pull/826
 
 
   

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/servicecomb-plugin/pom.xml 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml
index 1668c7020..ae14a3306 100644
--- a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml
+++ b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/pom.xml
@@ -13,6 +13,7 @@
 
     <modules>
         <module>servicecomb-java-chassis-0.x-plugin</module>
+        <module>servicecomb-java-chassis-1.x-plugin</module>
     </modules>
     <packaging>pom</packaging>
 
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/pom.xml
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/pom.xml
new file mode 100644
index 000000000..0d3aa169e
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/pom.xml
@@ -0,0 +1,57 @@
+<?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-1.x-plugin</artifactId>
+    <packaging>jar</packaging>
+
+    <name>servicecomb-java-chassis-1.x-plugin</name>
+    <url>http://maven.apache.org</url>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.servicecomb</groupId>
+            <artifactId>java-chassis-core</artifactId>
+            <version>1.0.0-m1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+    <repositories>
+        <repository>
+            <id>apache-servicecomb</id>
+            <url>
+                
https://repository.apache.org/content/repositories/snapshots/org/apache/servicecomb/java-chassis-core/1.0.0-m1-SNAPSHOT/
+            </url>
+            <snapshots>
+                <enabled>true</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
+    <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-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/ProducerOperationHandlerInterceptor.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/ProducerOperationHandlerInterceptor.java
new file mode 100644
index 000000000..90f437d70
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1;
+
+import java.lang.reflect.Method;
+import org.apache.servicecomb.core.Invocation;
+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;
+
+/**
+ * {ProducerOperationHandlerInterceptor} define how to enhance class {@link
+ * ProducerOperationHandler#handle(org.apache.servicecomb.core.Invocation, 
org.apache.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-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/TransportClientHandlerInterceptor.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/TransportClientHandlerInterceptor.java
new file mode 100644
index 000000000..6079976c6
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1;
+
+import java.lang.reflect.Method;
+import java.net.URI;
+import org.apache.servicecomb.core.Invocation;
+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(org.apache.servicecomb.core.Invocation,
+ * org.apache.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-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/define/ProducerOperationHandlerInstrumentation.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/define/ProducerOperationHandlerInstrumentation.java
new file mode 100644
index 000000000..ff55dc307
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1.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(org.apache.servicecomb.core.Invocation, 
org.apache.servicecomb.swagger.invocation.AsyncResponse)}by
+ * using {@link ProducerOperationHandlerInterceptor}
+ *
+ * @author lytscu
+ */
+public class ProducerOperationHandlerInstrumentation extends 
ClassInstanceMethodsEnhancePluginDefine {
+
+    private static final String ENHANCE_CLASS = 
"org.apache.servicecomb.core.handler.impl.ProducerOperationHandler";
+
+    private static final String INTERCEPT_CLASS = 
"org.apache.skywalking.apm.plugin.servicecomb.v1.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-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/define/TransportClientHandlerInstrumentation.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/define/TransportClientHandlerInstrumentation.java
new file mode 100644
index 000000000..888300218
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1.define;
+
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+import org.apache.servicecomb.core.handler.impl.TransportClientHandler;
+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 = 
"org.apache.servicecomb.core.handler.impl.TransportClientHandler";
+
+    private static final String INTERCEPT_CLASS = 
"org.apache.skywalking.apm.plugin.servicecomb.v1.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-1.x-plugin/src/main/resources/skywalking-plugin.def
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/resources/skywalking-plugin.def
new file mode 100644
index 000000000..ea567e625
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/main/resources/skywalking-plugin.def
@@ -0,0 +1,2 @@
+servicecomb-1.x=org.apache.skywalking.apm.plugin.servicecomb.v1.define.ProducerOperationHandlerInstrumentation
+servicecomb-1.x=org.apache.skywalking.apm.plugin.servicecomb.v1.define.TransportClientHandlerInstrumentation
diff --git 
a/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/ProducerOperationHandlerInterceptorTest.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/ProducerOperationHandlerInterceptorTest.java
new file mode 100644
index 000000000..1ab8e7483
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.swagger.invocation.InvocationType;
+import org.apache.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-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/TransportClientHandlerInterceptorTest.java
 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/TransportClientHandlerInterceptorTest.java
new file mode 100644
index 000000000..0f6972029
--- /dev/null
+++ 
b/apm-sniffer/apm-sdk-plugin/servicecomb-plugin/servicecomb-java-chassis-1.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/servicecomb/v1/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.v1;
+
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.swagger.invocation.InvocationType;
+import org.apache.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:
[email protected]


With regards,
Apache Git Services

Reply via email to