Search in sources :

Example 1 with MasterSlaveConnectionProvider

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);
    }
}
Also used : MasterSlaveConnectionProvider(io.lettuce.core.masterslave.MasterSlaveConnectionProvider) DefaultEndpoint(io.lettuce.core.protocol.DefaultEndpoint) RedisURI(io.lettuce.core.RedisURI) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) List(java.util.List) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

RedisURI (io.lettuce.core.RedisURI)1 MasterSlaveConnectionProvider (io.lettuce.core.masterslave.MasterSlaveConnectionProvider)1 DefaultEndpoint (io.lettuce.core.protocol.DefaultEndpoint)1 Channel (io.netty.channel.Channel)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1