提交 f594c99a 编写于 作者: A ascrutae

1.Redis集群插件提交

上级 3865edc2
package com.ai.cloud.skywalking.jedis.v2.plugin;
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.assist.FirstInvokeInterceptor;
public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
protected static final String REDIS_CONN_INFO_KEY = "redisClusterConnInfo";
private static RPCBuriedPointSender sender = new RPCBuriedPointSender();
@Override
public void beforeMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext) {
if (this.isFirstBeforeMethod(context)) {
/**
* redis server wouldn't process rpc context. ignore the
* return(ContextData) of sender's beforeSend
*/
Identification.IdentificationBuilder builder = Identification
.newBuilder()
.viewPoint(
context.get(REDIS_CONN_INFO_KEY, String.class)
+ " " + interceptorContext.methodName())
.spanType(RedisBuriedPointType.instance());
if (interceptorContext.allArguments().length > 0
&& interceptorContext.allArguments()[0] instanceof String) {
builder.businessKey("key="
+ interceptorContext.allArguments()[0]);
}
sender.beforeSend(builder.build());
}
}
@Override
public Object afterMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
if (this.isLastAfterMethod(context)) {
sender.afterSend();
}
return ret;
}
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
sender.handleException(t);
}
}
package com.ai.cloud.skywalking.jedis.v2.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import redis.clients.jedis.HostAndPort;
import java.util.Set;
/**
* Created by xin on 16-6-12.
*/
public class JedisClusterInterceptor extends JedisBaseInterceptor {
@Override
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
StringBuilder redisConnInfo = new StringBuilder();
if (interceptorContext.allArguments().length > 0) {
if (interceptorContext.allArguments()[0] instanceof Set) {
Set<HostAndPort> hostAndPorts = (Set<HostAndPort>) interceptorContext.allArguments()[0];
for (HostAndPort hostAndPort : hostAndPorts) {
redisConnInfo.append(hostAndPort.toString()).append(";");
}
} else if (interceptorContext.allArguments()[0] instanceof HostAndPort) {
HostAndPort hostAndPort = (HostAndPort) interceptorContext.allArguments()[0];
redisConnInfo.append(hostAndPort.toString()).append(";");
}
}
context.set(REDIS_CONN_INFO_KEY, redisConnInfo.toString());
}
}
package com.ai.cloud.skywalking.jedis.v2.plugin;
import java.net.URI;
import redis.clients.jedis.JedisShardInfo;
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.model.Identification.IdentificationBuilder;
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.assist.FirstInvokeInterceptor;
public class JedisInterceptor extends FirstInvokeInterceptor {
private static final String REDIS_CONN_INFO_KEY = "redisConnInfo";
private static RPCBuriedPointSender sender = new RPCBuriedPointSender();
@Override
public void onConstruct(EnhancedClassInstanceContext context,
ConstructorInvokeContext interceptorContext) {
String redisConnInfo = "";
if (interceptorContext.allArguments().length > 0) {
if (interceptorContext.allArguments()[0] instanceof String) {
redisConnInfo = (String) interceptorContext.allArguments()[0];
if (interceptorContext.allArguments().length > 1) {
redisConnInfo += ":"
+ (Integer) interceptorContext.allArguments()[1];
}
} else if (interceptorContext.allArguments()[0] instanceof JedisShardInfo) {
JedisShardInfo shardInfo = (JedisShardInfo) interceptorContext
.allArguments()[0];
redisConnInfo = shardInfo.getHost() + ":" + shardInfo.getPort();
} else if (interceptorContext.allArguments()[0] instanceof URI) {
URI uri = (URI) interceptorContext.allArguments()[0];
redisConnInfo = uri.getHost() + ":" + uri.getPort();
}
}
context.set(REDIS_CONN_INFO_KEY, redisConnInfo);
}
@Override
public void beforeMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext) {
if (this.isFirstBeforeMethod(context)) {
/**
* redis server wouldn't process rpc context. ignore the
* return(ContextData) of sender's beforeSend
*/
IdentificationBuilder builder = Identification
.newBuilder()
.viewPoint(
context.get(REDIS_CONN_INFO_KEY, String.class)
+ " " + interceptorContext.methodName())
.spanType(RedisBuriedPointType.instance());
if (interceptorContext.allArguments().length > 0
&& interceptorContext.allArguments()[0] instanceof String) {
builder.businessKey("key="
+ interceptorContext.allArguments()[0]);
}
sender.beforeSend(builder.build());
}
}
import redis.clients.jedis.JedisShardInfo;
@Override
public Object afterMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
if (this.isLastAfterMethod(context)) {
sender.afterSend();
}
return ret;
}
import java.net.URI;
@Override
public void handleMethodException(Throwable t,
EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
sender.handleException(t);
}
public class JedisInterceptor extends JedisBaseInterceptor {
@Override
public void onConstruct(EnhancedClassInstanceContext context,
ConstructorInvokeContext interceptorContext) {
String redisConnInfo = "";
if (interceptorContext.allArguments().length > 0) {
if (interceptorContext.allArguments()[0] instanceof String) {
redisConnInfo = (String) interceptorContext.allArguments()[0];
if (interceptorContext.allArguments().length > 1) {
redisConnInfo += ":"
+ (Integer) interceptorContext.allArguments()[1];
}
} else if (interceptorContext.allArguments()[0] instanceof JedisShardInfo) {
JedisShardInfo shardInfo = (JedisShardInfo) interceptorContext
.allArguments()[0];
redisConnInfo = shardInfo.getHost() + ":" + shardInfo.getPort();
} else if (interceptorContext.allArguments()[0] instanceof URI) {
URI uri = (URI) interceptorContext.allArguments()[0];
redisConnInfo = uri.getHost() + ":" + uri.getPort();
}
}
context.set(REDIS_CONN_INFO_KEY, redisConnInfo);
}
}
package com.ai.cloud.skywalking.jedis.v2.plugin.define;
import com.ai.cloud.skywalking.jedis.v2.plugin.JedisClusterInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public class JedisClusterPluginDefine implements InterceptorDefine {
@Override
public String getBeInterceptedClassName() {
return "redis.clients.jedis.JedisCluster";
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
return new MethodMatcher[]{
new SimpleMethodMatcher("set")
};
}
@Override
public IAroundInterceptor instance() {
return new JedisClusterInterceptor();
}
}
com.ai.cloud.skywalking.jedis.v2.plugin.define.JedisPluginDefine
\ No newline at end of file
com.ai.cloud.skywalking.jedis.v2.plugin.define.JedisPluginDefine
com.ai.cloud.skywalking.jedis.v2.plugin.define.JedisClusterPluginDefine
\ No newline at end of file
package com.ai.cloud.skywalking.jedis.v2.plugin;
import com.ai.cloud.skywalking.plugin.TracingBootstrap;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
public class JedisClusterTest {
@Test
public void test() throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException,
SecurityException, ClassNotFoundException {
TracingBootstrap
.main(new String[]{"com.ai.cloud.skywalking.jedis.v2.plugin.JedisClusterTest"});
}
public static void main(String[] args) throws ClassNotFoundException,
SQLException, InterruptedException {
JedisCluster jedisCluster = null;
try {
jedisCluster = new JedisCluster(getHostAndPorts());
long start = System.currentTimeMillis();
jedisCluster.set("11111", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
long end = System.currentTimeMillis();
System.out.println(end - start + "ms");
jedisCluster.del("11111");
} catch (Exception e) {
e.printStackTrace();
}
}
private static Set<HostAndPort> getHostAndPorts() {
Set<HostAndPort> redisEnv = new HashSet<HostAndPort>();
redisEnv.add(new HostAndPort("127.0.0.1", 7000));
redisEnv.add(new HostAndPort("127.0.0.1", 7001));
redisEnv.add(new HostAndPort("127.0.0.1", 7002));
redisEnv.add(new HostAndPort("127.0.0.1", 7003));
redisEnv.add(new HostAndPort("127.0.0.1", 7004));
redisEnv.add(new HostAndPort("127.0.0.1", 7005));
return redisEnv;
}
public void testNormal() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, InterruptedException {
JedisClusterTest.main(null);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册