use of io.lettuce.core.masterslave.MasterSlaveConnectionProvider in project LinkAgent by shulieTech.
the class LettuceMethodInterceptor method appendEndPoint.
private void appendEndPoint(Object target, final SpanRecord spanRecord) {
try {
final Object connection = Reflect.on(target).get(LettuceConstants.REFLECT_FIELD_CONNECTION);
final Object t = Reflect.on(connection).get(LettuceConstants.REFLECT_FIELD_CHANNEL_WRITER);
;
DefaultEndpoint endpoint = null;
if ("io.lettuce.core.masterslave.MasterSlaveChannelWriter".equals(t.getClass().getName())) {
try {
/**
* 这是主从的
*/
MasterSlaveConnectionProvider provider = Reflect.on(t).get("masterSlaveConnectionProvider");
RedisURI redisUri = Reflect.on(provider).get("initialRedisUri");
spanRecord.setRemoteIp(redisUri.getHost());
spanRecord.setPort(redisUri.getPort());
} catch (Throwable thx) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
return;
/**
* 这是哨兵的
*/
} else if ("io.lettuce.core.masterslave.SentinelConnector$1".equals(t.getClass().getName())) {
try {
Object sentinelConnector = Reflect.on(t).get("this$0");
RedisURI redisURI = Reflect.on(sentinelConnector).get("redisURI");
List<RedisURI> sentinels = redisURI.getSentinels();
RedisURI current = sentinels.get(0);
spanRecord.setRemoteIp(current.getHost());
spanRecord.setPort(current.getPort());
} catch (Throwable thx) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
return;
}
if (t instanceof DefaultEndpoint) {
endpoint = (DefaultEndpoint) t;
} else {
try {
endpoint = Reflect.on(t).get(LettuceConstants.REFLECT_FIELD_DEFAULT_WRITER);
} catch (Throwable w) {
endpoint = Reflect.on(t).get(LettuceConstants.REFLECT_FIELD_WRITER);
}
}
if (endpoint == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
Channel channel = Reflect.on(endpoint).get(LettuceConstants.REFLECT_FIELD_CHANNEL);
if (channel == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
SocketAddress socketAddress = channel.remoteAddress();
if (socketAddress == null) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
if (!(socketAddress instanceof InetSocketAddress)) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
return;
}
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
spanRecord.setRemoteIp(inetSocketAddress.getAddress().getHostAddress());
spanRecord.setPort(inetSocketAddress.getPort());
} catch (Throwable e) {
spanRecord.setRemoteIp(LettuceConstants.ADDRESS_UNKNOW);
}
}
Aggregations