提交 cdb02d56 编写于 作者: 于玉桔 提交者: wu-sheng

redisson plugin support high version (#3780)

* redisson support high lv version

* bugfix

* add comment
上级 c002a6b3
......@@ -67,7 +67,7 @@ pipeline {
sh './mvnw -f test/plugin/pom.xml clean package -DskipTests docker:build'
}
}
stage('Test Cases Report (198)') {
stage('Test Cases Report (212)') {
steps {
echo "Test Cases Report"
}
......@@ -82,7 +82,7 @@ pipeline {
parallel {
stage('Group1') {
stages {
stage('redisson-scenario 3.x (23)') {
stage('redisson-scenario 3.x (37)') {
steps {
sh 'bash test/plugin/run.sh redisson-scenario'
}
......
......@@ -15,19 +15,18 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3;
import org.apache.skywalking.apm.agent.core.context.util.PeerFormat;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
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.agent.core.context.util.PeerFormat;
import org.apache.skywalking.apm.plugin.redisson.v3.util.ClassUtil;
import org.redisson.config.*;
import org.redisson.connection.ConnectionManager;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Collection;
......@@ -51,33 +50,33 @@ public class ConnectionManagerInterceptor implements InstanceMethodsAroundInterc
ConnectionManager connectionManager = (ConnectionManager) objInst;
Config config = connectionManager.getCfg();
SentinelServersConfig sentinelServersConfig = (SentinelServersConfig) getServersConfig(config, "sentinelServersConfig");
MasterSlaveServersConfig masterSlaveServersConfig = (MasterSlaveServersConfig) getServersConfig(config, "masterSlaveServersConfig");
ClusterServersConfig clusterServersConfig = (ClusterServersConfig) getServersConfig(config, "clusterServersConfig");
ReplicatedServersConfig replicatedServersConfig = (ReplicatedServersConfig) getServersConfig(config, "replicatedServersConfig");
Object sentinelServersConfig = ClassUtil.getObjectField(config, "sentinelServersConfig");
Object masterSlaveServersConfig = ClassUtil.getObjectField(config, "masterSlaveServersConfig");
Object clusterServersConfig = ClassUtil.getObjectField(config, "clusterServersConfig");
Object replicatedServersConfig = ClassUtil.getObjectField(config, "replicatedServersConfig");
StringBuilder peer = new StringBuilder();
EnhancedInstance retInst = (EnhancedInstance) ret;
if (sentinelServersConfig != null) {
appendAddresses(peer, sentinelServersConfig.getSentinelAddresses());
appendAddresses(peer, (Collection) ClassUtil.getObjectField(sentinelServersConfig, "sentinelAddresses"));
retInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
return ret;
}
if (masterSlaveServersConfig != null) {
URI masterAddress = masterSlaveServersConfig.getMasterAddress();
peer.append(masterAddress.getHost()).append(":").append(masterAddress.getPort());
appendAddresses(peer, masterSlaveServersConfig.getSlaveAddresses());
Object masterAddress = ClassUtil.getObjectField(masterSlaveServersConfig, "masterAddress");
peer.append(getPeer(masterAddress));
appendAddresses(peer, (Collection) ClassUtil.getObjectField(masterSlaveServersConfig, "slaveAddresses"));
retInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
return ret;
}
if (clusterServersConfig != null) {
appendAddresses(peer, clusterServersConfig.getNodeAddresses());
appendAddresses(peer, (Collection) ClassUtil.getObjectField(clusterServersConfig, "nodeAddresses"));
retInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
return ret;
}
if (replicatedServersConfig != null) {
appendAddresses(peer, replicatedServersConfig.getNodeAddresses());
appendAddresses(peer, (Collection) ClassUtil.getObjectField(replicatedServersConfig, "nodeAddresses"));
retInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
return ret;
}
......@@ -87,20 +86,33 @@ public class ConnectionManagerInterceptor implements InstanceMethodsAroundInterc
return ret;
}
private Object getServersConfig(Config config, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = config.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(config);
}
private void appendAddresses(StringBuilder peer, Collection<URI> nodeAddresses) {
private void appendAddresses(StringBuilder peer, Collection nodeAddresses) {
if (nodeAddresses != null && !nodeAddresses.isEmpty()) {
for (URI uri : nodeAddresses) {
peer.append(uri.getHost()).append(":").append(uri.getPort()).append(";");
for (Object uri : nodeAddresses) {
peer.append(getPeer(uri)).append(";");
}
}
}
/**
* In some high versions of redisson, such as 3.11.1.
* The attribute address in the RedisClientConfig class is changed from the lower version of the URI to the String.
* So use the following code for compatibility.
* @param obj Address object
* @return the sw peer
*/
private String getPeer(Object obj) {
if (obj instanceof String) {
return ((String) obj).replace("redis://", "");
} else if (obj instanceof URI) {
URI uri = (URI) obj;
return uri.getHost() + ":" + uri.getPort();
} else {
logger.warn("redisson not support this version");
return null;
}
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
......
......@@ -15,8 +15,6 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
......
......@@ -15,8 +15,6 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3;
import io.netty.buffer.ByteBuf;
......@@ -25,11 +23,14 @@ 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.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
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;
import org.apache.skywalking.apm.plugin.redisson.v3.util.ClassUtil;
import org.redisson.client.RedisClient;
import org.redisson.client.RedisConnection;
import org.redisson.client.protocol.CommandData;
......@@ -43,6 +44,8 @@ import java.net.InetSocketAddress;
*/
public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
private static final ILog logger = LogManager.getLogger(RedisConnectionMethodInterceptor.class);
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
......@@ -106,7 +109,19 @@ public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundIn
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String peer = (String) ((EnhancedInstance) allArguments[0]).getSkyWalkingDynamicField();
if (peer == null) {
peer = ((RedisClient) allArguments[0]).getConfig().getAddress().getAuthority();
try {
/*
In some high versions of redisson, such as 3.11.1.
The attribute address in the RedisClientConfig class changed from a lower version of the URI to a RedisURI.
But they all have the host and port attributes, so use the following code for compatibility.
*/
Object address = ClassUtil.getObjectField(((RedisClient) allArguments[0]).getConfig(), "address");
String host = (String) ClassUtil.getObjectField(address, "host");
String port = String.valueOf(ClassUtil.getObjectField(address, "port"));
peer = host + ":" + port;
} catch (Exception e) {
logger.warn("RedisConnection create peer error: ", e);
}
}
objInst.setSkyWalkingDynamicField(peer);
}
......
......@@ -15,8 +15,6 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3.define;
import net.bytebuddy.description.method.MethodDescription;
......
......@@ -15,8 +15,6 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3.define;
import net.bytebuddy.description.method.MethodDescription;
......
......@@ -15,8 +15,6 @@
* limitations under the License.
*
*/
package org.apache.skywalking.apm.plugin.redisson.v3.define;
import net.bytebuddy.description.method.MethodDescription;
......
/*
* 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.redisson.v3.util;
import java.lang.reflect.Field;
/**
* @author zhaoyuguang
*/
public class ClassUtil {
/**
* This method should only be used in low frequency.
* It should not use in trace context, but just in the metadata preparation stage.
*/
public static Object getObjectField(Object obj, String name) throws NoSuchFieldException, IllegalAccessException {
Field field = obj.getClass().getDeclaredField(name);
field.setAccessible(true);
return field.get(obj);
}
}
......@@ -29,7 +29,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.version>1.8</compiler.version>
<spring-boot-version>1.5.2.RELEASE</spring-boot-version>
<test.framework.version>3.6.0</test.framework.version>
<test.framework.version>3.11.5</test.framework.version>
<docker.image.version>${test.framework.version}</docker.image.version>
</properties>
......
......@@ -14,6 +14,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
3.11.5
3.11.4
3.11.3
3.11.2
3.11.1
3.11.0
3.10.7
3.10.6
3.10.5
3.10.4
3.10.3
3.10.2
3.10.1
3.10.0
3.9.1
3.9.0
3.8.2
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册