Skip to content

Commit c4766ec

Browse files
leizhiyuanwu-sheng
authored andcommitted
Add sofa rpc plugin integration (apache#1210)
* sofa rpc integration * remove unused class and method,add component defination * only support after sofa rpc 5.4.0 * set sofarpc componentId be 32 * Add document of component library settings and incuating features abstract. (apache#1185) * sofa rpc integration * Update UI licenses (apache#1189) * Update UI licenses for beta version * Update FAQ doc (apache#1184) * update faq doc * update faq * update faq * Update readme for beta release. (apache#1191) * New english readme. * New chinese readme. * Fix apache#1186 Webapp read timeout config (apache#1192) * Updat ui (apache#1197) * Change the intercept point of block call way (apache#1190) * Change the buired point of block call way * Support future way and change the operation name of span * Update screenshot for new release. (apache#1209) * remove unused class and method,add component defination * only support after sofa rpc 5.4.0 * set sofarpc componentId be 32 * sofa rpc integration * remove unused class and method,add component defination * only support after sofa rpc 5.4.0 * set sofarpc componentId be 32 * sofa rpc integration * remove unused class and method,add component defination * change parent pom version * remove unused dependency
1 parent 7c305fe commit c4766ec

File tree

12 files changed

+800
-2
lines changed

12 files changed

+800
-2
lines changed

apm-collector/apm-collector-boot/src/main/resources/component-libraries.yml

+3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ kafka-consumer:
153153
mongodb-driver:
154154
id: 42
155155
languages: Java
156+
SOFARPC:
157+
id: 43
158+
languages: Java
156159

157160
# .NET/.NET Core components
158161
# [3000, 4000) for C#/.NET only

apm-collector/apm-collector-configuration/collector-configuration-provider/src/test/resources/component-libraries.yml

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Hystrix:
124124
Jedis:
125125
id: 30
126126
languages: Java
127+
SOFARPC:
128+
id: 43
129+
languages: Java
127130

128131

129132
# Component Server mapping defines the server display names of some components

apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class ComponentsDefine {
5959
public static final OfficialComponent GRPC = new OfficialComponent(23, "GRPC");
6060

6161
public static final OfficialComponent ELASTIC_JOB = new OfficialComponent(24, "ElasticJob");
62-
62+
6363
public static final OfficialComponent HTTP_ASYNC_CLIENT = new OfficialComponent(26, "httpasyncclient");
6464

6565
public static final OfficialComponent SERVICECOMB = new OfficialComponent(28, "ServiceComb");
@@ -90,6 +90,8 @@ public class ComponentsDefine {
9090

9191
public static final OfficialComponent MONGO_DRIVER = new OfficialComponent(42, "mongodb-driver");
9292

93+
public static final OfficialComponent SOFARPC = new OfficialComponent(43, "SOFARPC");
94+
9395
private static ComponentsDefine INSTANCE = new ComponentsDefine();
9496

9597
private String[] components;
@@ -99,7 +101,7 @@ public static ComponentsDefine getInstance() {
99101
}
100102

101103
public ComponentsDefine() {
102-
components = new String[43];
104+
components = new String[44];
103105
addComponent(TOMCAT);
104106
addComponent(HTTPCLIENT);
105107
addComponent(DUBBO);
@@ -132,6 +134,7 @@ public ComponentsDefine() {
132134
addComponent(KAFKA_PRODUCER);
133135
addComponent(KAFKA_CONSUMER);
134136
addComponent(MONGO_DRIVER);
137+
addComponent(SOFARPC);
135138
}
136139

137140
private void addComponent(OfficialComponent component) {

apm-sniffer/apm-sdk-plugin/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<module>kafka-v1-plugin</module>
5757
<module>servicecomb-plugin</module>
5858
<module>hystrix-1.x-plugin</module>
59+
<module>sofarpc-plugin</module>
5960
</modules>
6061
<packaging>pom</packaging>
6162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!--
2+
~ Licensed to the Apache Software Foundation (ASF) under one or more
3+
~ contributor license agreements. See the NOTICE file distributed with
4+
~ this work for additional information regarding copyright ownership.
5+
~ The ASF licenses this file to You under the Apache License, Version 2.0
6+
~ (the "License"); you may not use this file except in compliance with
7+
~ the License. You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<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">
20+
<parent>
21+
<artifactId>apm-sdk-plugin</artifactId>
22+
<groupId>org.apache.skywalking</groupId>
23+
<version>5.0.0-beta2-SNAPSHOT</version>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>sofa-rpc-plugin</artifactId>
28+
<packaging>jar</packaging>
29+
30+
<name>sofarpc-plugin</name>
31+
<url>http://maven.apache.org</url>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
</properties>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.alipay.sofa</groupId>
40+
<artifactId>sofa-rpc-all</artifactId>
41+
<version>5.4.0</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
</dependencies>
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<!-- 源码插件 -->
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-source-plugin</artifactId>
52+
<!-- 发布时自动将源码同时发布的配置 -->
53+
<executions>
54+
<execution>
55+
<id>attach-sources</id>
56+
<phase>none</phase>
57+
<goals>
58+
<goal>jar</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.sofarpc;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
26+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
27+
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
28+
29+
import static net.bytebuddy.matcher.ElementMatchers.named;
30+
31+
public class SofaRpcConsumerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
32+
33+
private static final String ENHANCE_CLASS = "com.alipay.sofa.rpc.filter.ConsumerInvoker";
34+
private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.sofarpc.SofaRpcConsumerInterceptor";
35+
36+
@Override
37+
protected ClassMatch enhanceClass() {
38+
return NameMatch.byName(ENHANCE_CLASS);
39+
}
40+
41+
@Override
42+
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
43+
return null;
44+
}
45+
46+
@Override
47+
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
48+
return new InstanceMethodsInterceptPoint[] {
49+
new InstanceMethodsInterceptPoint() {
50+
@Override
51+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
52+
return named("invoke");
53+
}
54+
55+
@Override
56+
public String getMethodsInterceptor() {
57+
return INTERCEPT_CLASS;
58+
}
59+
60+
@Override
61+
public boolean isOverrideArgs() {
62+
return false;
63+
}
64+
}
65+
};
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.sofarpc;
20+
21+
import com.alipay.sofa.rpc.client.ProviderInfo;
22+
import com.alipay.sofa.rpc.context.RpcInternalContext;
23+
import com.alipay.sofa.rpc.core.request.SofaRequest;
24+
import com.alipay.sofa.rpc.core.response.SofaResponse;
25+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
26+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
27+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
28+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
29+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
30+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
31+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
32+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
33+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
34+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
35+
36+
import java.lang.reflect.Method;
37+
38+
/**
39+
* @author leizhiyuan
40+
*/
41+
public class SofaRpcConsumerInterceptor implements InstanceMethodsAroundInterceptor {
42+
43+
public static final String SKYWALKING_PREFIX = "skywalking.";
44+
45+
@Override
46+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
47+
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
48+
SofaRequest sofaRequest = (SofaRequest) allArguments[0];
49+
RpcInternalContext rpcContext = RpcInternalContext.getContext();
50+
51+
ProviderInfo providerInfo = rpcContext.getProviderInfo();
52+
53+
AbstractSpan span = null;
54+
55+
final String host = providerInfo.getHost();
56+
final int port = providerInfo.getPort();
57+
final ContextCarrier contextCarrier = new ContextCarrier();
58+
final String operationName = generateOperationName(providerInfo, sofaRequest);
59+
span = ContextManager.createExitSpan(operationName, contextCarrier, host + ":" + port);
60+
CarrierItem next = contextCarrier.items();
61+
while (next.hasNext()) {
62+
next = next.next();
63+
String key = next.getHeadKey();
64+
String skyWalkingKey = SKYWALKING_PREFIX + key;
65+
sofaRequest.addRequestProp(skyWalkingKey, next.getHeadValue());
66+
}
67+
68+
Tags.URL.set(span, generateRequestURL(providerInfo, sofaRequest));
69+
span.setComponent(ComponentsDefine.SOFARPC);
70+
SpanLayer.asRPCFramework(span);
71+
}
72+
73+
@Override
74+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
75+
Class<?>[] argumentsTypes, Object ret) throws Throwable {
76+
SofaResponse result = (SofaResponse) ret;
77+
if (result != null && result.isError()) {
78+
dealException((Throwable) result.getAppResponse());
79+
}
80+
81+
ContextManager.stopSpan();
82+
return ret;
83+
}
84+
85+
@Override
86+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
87+
Class<?>[] argumentsTypes, Throwable t) {
88+
dealException(t);
89+
}
90+
91+
/**
92+
* Log the throwable, which occurs in Dubbo RPC service.
93+
*/
94+
private void dealException(Throwable throwable) {
95+
AbstractSpan span = ContextManager.activeSpan();
96+
span.errorOccurred();
97+
span.log(throwable);
98+
}
99+
100+
/**
101+
* Format operation name. e.g. org.apache.skywalking.apm.plugin.test.Test.test(String)
102+
*
103+
* @return operation name.
104+
*/
105+
private String generateOperationName(ProviderInfo providerInfo, SofaRequest sofaRequest) {
106+
StringBuilder operationName = new StringBuilder();
107+
operationName.append(sofaRequest.getInterfaceName());
108+
operationName.append("." + sofaRequest.getMethodName() + "(");
109+
for (String arg : sofaRequest.getMethodArgSigs()) {
110+
operationName.append(arg + ",");
111+
}
112+
113+
if (sofaRequest.getMethodArgs().length > 0) {
114+
operationName.delete(operationName.length() - 1, operationName.length());
115+
}
116+
117+
operationName.append(")");
118+
119+
return operationName.toString();
120+
}
121+
122+
/**
123+
* Format request url.
124+
* e.g. bolt://127.0.0.1:20880/org.apache.skywalking.apm.plugin.test.Test.test(String).
125+
*
126+
* @return request url.
127+
*/
128+
private String generateRequestURL(ProviderInfo providerInfo, SofaRequest sofaRequest) {
129+
StringBuilder requestURL = new StringBuilder();
130+
requestURL.append(providerInfo.getProtocolType() + "://");
131+
requestURL.append(providerInfo.getHost());
132+
requestURL.append(":" + providerInfo.getPort() + "/");
133+
requestURL.append(generateOperationName(providerInfo, sofaRequest));
134+
return requestURL.toString();
135+
}
136+
}

0 commit comments

Comments
 (0)