Skip to content

Commit e31b9bb

Browse files
authored
Add nacos-client 2.x plugin (#593)
1 parent e7394a8 commit e31b9bb

File tree

26 files changed

+1086
-0
lines changed

26 files changed

+1086
-0
lines changed

.github/workflows/plugins-test.2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ jobs:
8080
- jersey-2.0.x-2.25.x-scenario
8181
- jersey-2.26.x-2.39.x-scenario
8282
- websphere-liberty-23.x-scenario
83+
- nacos-client-2.x-scenario
8384
steps:
8485
- uses: actions/checkout@v2
8586
with:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Callable {
151151
* Fix Jedis-2.x plugin bug and add test for Redis cluster scene
152152
* Merge two instrumentation classes to avoid duplicate enhancements in MySQL plugins.
153153
* Support asynchronous invocation in jetty client 9.0 and 9.x plugin
154+
* Add nacos-client 2.x plugin
154155

155156
#### Documentation
156157

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,5 @@ public class ComponentsDefine {
241241

242242
public static final OfficialComponent AEROSPIKE = new OfficialComponent(149, "Aerospike");
243243

244+
public static final OfficialComponent NACOS = new OfficialComponent(150, "Nacos");
244245
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
20+
<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">
21+
<parent>
22+
<groupId>org.apache.skywalking</groupId>
23+
<artifactId>optional-plugins</artifactId>
24+
<version>9.0.0-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>apm-nacos-client-2.x-plugin</artifactId>
29+
<name>nacos-client-2.x-plugin</name>
30+
31+
<properties>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
<nacos-client.version>2.0.3</nacos-client.version>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.alibaba.nacos</groupId>
39+
<artifactId>nacos-client</artifactId>
40+
<version>${nacos-client.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<artifactId>maven-deploy-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.nacos.v2;
20+
21+
import com.alibaba.nacos.api.remote.request.Request;
22+
import com.alibaba.nacos.common.remote.client.RpcClient;
23+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
27+
28+
import java.lang.reflect.Method;
29+
30+
public class ClientHandleServerRequestInterceptor implements InstanceMethodsAroundInterceptor {
31+
32+
@Override
33+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
34+
String peer = getPeer(objInst);
35+
NacosRequestOpt.getHandler(allArguments[0].getClass()).ifPresent(o -> o.buildRequestSpanInfo((Request) allArguments[0], peer));
36+
}
37+
38+
@Override
39+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
40+
if (ContextManager.isActive()) {
41+
ContextManager.stopSpan();
42+
}
43+
return ret;
44+
}
45+
46+
@Override
47+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
48+
if (ContextManager.isActive()) {
49+
ContextManager.activeSpan().log(t);
50+
}
51+
}
52+
53+
private String getPeer(EnhancedInstance objInst) {
54+
Object dynamicField = objInst.getSkyWalkingDynamicField();
55+
if (dynamicField instanceof RpcClient.ServerInfo) {
56+
RpcClient.ServerInfo serverInfo = (RpcClient.ServerInfo) dynamicField;
57+
return serverInfo.getAddress();
58+
}
59+
return "no peer";
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.nacos.v2;
20+
21+
import com.alibaba.nacos.api.remote.response.Response;
22+
import com.alibaba.nacos.common.remote.client.RpcClient;
23+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
24+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
25+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
30+
31+
import java.lang.reflect.Method;
32+
33+
public class ClientServerCheckInterceptor implements InstanceMethodsAroundInterceptor {
34+
35+
@Override
36+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
37+
String ip = (String) allArguments[0];
38+
int port = (Integer) allArguments[1];
39+
objInst.setSkyWalkingDynamicField(new RpcClient.ServerInfo(ip, port));
40+
AbstractSpan span = ContextManager.createExitSpan("Nacos/serverCheck", ip + ":" + port);
41+
SpanLayer.asRPCFramework(span);
42+
span.setComponent(ComponentsDefine.NACOS);
43+
}
44+
45+
@Override
46+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
47+
if (ret == null || !((Response) ret).isSuccess()) {
48+
ContextManager.activeSpan().errorOccurred();
49+
}
50+
ContextManager.stopSpan();
51+
return ret;
52+
}
53+
54+
@Override
55+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
56+
ContextManager.activeSpan().log(t);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.nacos.v2;
20+
21+
import com.alibaba.nacos.api.remote.request.Request;
22+
import com.alibaba.nacos.api.remote.response.Response;
23+
import com.alibaba.nacos.common.remote.client.grpc.GrpcConnection;
24+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
25+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
30+
import java.lang.reflect.Method;
31+
32+
public class GrpcConnectionInterceptor implements InstanceMethodsAroundInterceptor {
33+
34+
@Override
35+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
36+
GrpcConnection connection = (GrpcConnection) objInst;
37+
NacosRequestOpt.getHandler(allArguments[0].getClass()).ifPresent(o -> o.buildRequestSpanInfo((Request) allArguments[0], connection.getChannel().authority()));
38+
}
39+
40+
@Override
41+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
42+
if (ContextManager.isActive()) {
43+
if (ret == null || !((Response) ret).isSuccess()) {
44+
AbstractSpan span = ContextManager.activeSpan();
45+
span.errorOccurred();
46+
}
47+
ContextManager.stopSpan();
48+
}
49+
return ret;
50+
}
51+
52+
@Override
53+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
54+
if (ContextManager.isActive()) {
55+
ContextManager.activeSpan().log(t);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)