Search in sources :

Example 6 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class DataSourceConnectionInterceptor method attachment.

void attachment(Advice advice) {
    try {
        HikariDataSource target = (HikariDataSource) advice.getTarget();
        ResourceManager.set(new Attachment(target.getJdbcUrl(), "hikari", Type.DataBaseType.types(), new HikariTemplate().setUrl(target.getJdbcUrl()).setUsername(target.getUsername()).setPassword(target.getPassword()).setDriverClassName(target.getDriverClassName()).setConnectionTimeout(target.getConnectionTimeout()).setConnectionTestQuery(target.getConnectionTestQuery()).setIdleTimeout(target.getIdleTimeout()).setMaxLifetime(target.getMaxLifetime()).setMinimumIdle(target.getIdleTimeout()).setMaximumPoolSize(target.getMaximumPoolSize()).setValidationTimeout(target.getValidationTimeout())));
    } catch (Throwable t) {
        logger.error(t.getMessage());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariTemplate(com.pamirs.attach.plugin.dynamic.template.HikariTemplate) Attachment(com.pamirs.attach.plugin.dynamic.Attachment)

Example 7 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class TraceTraceEncoder method attachment.

void attachment(AbstractContext ctx) {
    if (ctx.isClusterTest) {
        return;
    }
    try {
        if (ctx.ext != null) {
            Object t = ctx.ext;
            if (Attachment.class.isAssignableFrom(t.getClass())) {
                Attachment attachment = (Attachment) t;
                Object key = Converter.TemplateConverter.ofClass(attachment.getExt().getClass()).getKey();
                ctx.ext = key + "@##" + JSON.toJSONString(attachment);
            }
            return;
        }
        String middleware = PradarCoreUtils.makeLogSafe(ctx.getMiddlewareName() == null ? "" : ctx.getMiddlewareName());
        if (StringUtil.isEmpty(middleware)) {
            return;
        }
        Object t = ResourceManager.get(ctx.index, middleware);
        if (t == null) {
            String serviceName = PradarCoreUtils.makeLogSafe(ctx.getServiceName() == null ? "" : ctx.getServiceName());
            t = ResourceManager.get(serviceName, middleware);
            if (t == null) {
                String methodName = PradarCoreUtils.makeLogSafe(ctx.getMethodName() == null ? "" : ctx.getMethodName());
                t = ResourceManager.get(serviceName + methodName, middleware);
                if (t == null) {
                    return;
                }
            }
        }
        ctx.ext = t;
        if (ctx.ext instanceof Attachment) {
            Attachment attachment = (Attachment) ctx.ext;
            Object key = Converter.TemplateConverter.ofClass(attachment.getExt().getClass()).getKey();
            ctx.ext = key + "@##" + JSON.toJSONString(ctx.ext);
        }
    } catch (Throwable t) {
    }
}
Also used : Attachment(com.pamirs.attach.plugin.dynamic.Attachment)

Example 8 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class SpringRedisClientInfoCollector method attachment.

/**
 * 业务流量的信息采集
 *
 * @param advice
 */
protected void attachment(Advice advice) {
    try {
        if (Pradar.isClusterTest()) {
            return;
        }
        LettuceConnectionFactory biz = (LettuceConnectionFactory) advice.getTarget();
        Object standaloneConfiguration = biz.getStandaloneConfiguration();
        Object clusterConfiguration = biz.getClusterConfiguration();
        Object sentinelConfiguration = biz.getSentinelConfiguration();
        if (clusterConfiguration != null) {
            Set<RedisNode> nodeSet = ((RedisClusterConfiguration) clusterConfiguration).getClusterNodes();
            StringBuilder builder = new StringBuilder();
            for (RedisNode node : nodeSet) {
                String host = node.getHost();
                String port = String.valueOf(node.getPort());
                builder.append(host.concat(":").concat(port)).append(",");
            }
            String nodes = builder.deleteCharAt(builder.length() - 1).toString();
            String password = null;
            try {
                char[] passwdbyte = ((RedisClusterConfiguration) clusterConfiguration).getPassword().get();
                password = new String(passwdbyte);
            } catch (NoSuchElementException e) {
            // 
            }
            ResourceManager.set(new Attachment(Arrays.asList(nodes.split(",")), "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceClusterTemplate().setNodes(nodes).setPassword(password)));
        } else if (sentinelConfiguration != null) {
            RedisSentinelConfiguration redisSentinelConfiguration = (RedisSentinelConfiguration) sentinelConfiguration;
            String masterName = redisSentinelConfiguration.getMaster().getName();
            String password = null;
            try {
                char[] passwdbyte = redisSentinelConfiguration.getPassword().get();
                password = new String(passwdbyte);
            } catch (NoSuchElementException e) {
            // 
            }
            Integer database = Integer.parseInt(String.valueOf(redisSentinelConfiguration.getDatabase()));
            Set<RedisNode> nodeSet = redisSentinelConfiguration.getSentinels();
            StringBuilder builder = new StringBuilder();
            for (RedisNode node : nodeSet) {
                String host = node.getHost();
                String port = String.valueOf(node.getPort());
                builder.append(host.concat(":").concat(port)).append(",");
            }
            String nodes = builder.deleteCharAt(builder.length() - 1).toString();
            List<String> sentinelRemoteAddress = new ArrayList<String>();
            try {
                AbstractRedisClient client = Reflect.on(biz).get("client");
                ChannelGroup channelGroup = Reflect.on(client).get("channels");
                Iterator iterator = channelGroup.iterator();
                while (iterator.hasNext()) {
                    Object o = iterator.next();
                    if (o instanceof NioSocketChannel) {
                        NioSocketChannel channel = (NioSocketChannel) o;
                        InetSocketAddress inetSocketAddress = channel.remoteAddress();
                        String host = inetSocketAddress.getAddress().getHostName();
                        String port = String.valueOf(inetSocketAddress.getPort());
                        sentinelRemoteAddress.add(host.concat(":").concat(port));
                    }
                }
            } catch (Throwable t) {
                logger.error(Throwables.getStackTraceAsString(t));
            }
            sentinelRemoteAddress.addAll(Arrays.asList(nodes.split(",")));
            ResourceManager.set(new Attachment(sentinelRemoteAddress, "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceSentinelTemplate().setNodes(nodes).setPassword(password).setMaster(masterName).setDatabase(database)));
        } else /**
         *standalone肯定不为空 放后面 因为可能为localhost
         */
        if (standaloneConfiguration != null && !("localhost".equals(((RedisStandaloneConfiguration) standaloneConfiguration).getHostName()))) {
            RedisStandaloneConfiguration configuration = (RedisStandaloneConfiguration) standaloneConfiguration;
            String host = configuration.getHostName();
            Integer port = configuration.getPort();
            Integer db = configuration.getDatabase();
            String password = null;
            try {
                char[] passwdbyte = configuration.getPassword().get();
                password = new String(passwdbyte);
            } catch (NoSuchElementException e) {
            // 
            }
            String node = host.concat(":").concat(String.valueOf(port));
            ResourceManager.set(new Attachment(Arrays.asList(node), "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceSingleTemplate().setNodes(node).setPassword(password).setDatabase(db)));
        }
    } catch (Throwable t) {
        logger.error("[redis-lettuce] collector spring biz info error , {}", Throwables.getStackTraceAsString(t));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) RedisNode(org.springframework.data.redis.connection.RedisNode) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelGroup(io.netty.channel.group.ChannelGroup)

Example 9 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class RedisMasterSlaveAttachRedisURIsInterceptor method cacheNodesInfo.

void cacheNodesInfo(List<RedisURI> redisURIS) {
    boolean isSentinel = false;
    try {
        List<String> indexes = new ArrayList<String>();
        StringBuilder nodeBuilder = new StringBuilder();
        String password = null;
        String nodes = "";
        String db = "";
        for (RedisURI redisURI : redisURIS) {
            /**
             * 先判断是否哨兵模式,如果是哨兵模式,取里面的sentinels,masterId为sentinelMasterId
             */
            if (!StringUtil.isEmpty(redisURI.getSentinelMasterId())) {
                isSentinel = true;
                String sentinelMasterId = redisURI.getSentinelMasterId();
                List<RedisURI> lists = redisURI.getSentinels();
                for (RedisURI inner : lists) {
                    String node = inner.getHost().concat(":").concat(String.valueOf(inner.getPort()));
                    indexes.add(node);
                    nodeBuilder.append(node).append(",");
                    password = inner.getPassword() == null ? null : new String(inner.getPassword());
                    db = String.valueOf(inner.getDatabase());
                }
                nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
                Attachment attachment = new Attachment(indexes, "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceSentinelTemplate().setMaster(sentinelMasterId).setNodes(nodes).setPassword(password));
                ResourceManager.set(attachment);
            }
            if (isSentinel) {
                continue;
            }
            /**
             * 主从模式,lettuce的主从不需要用户关注master,为了页面展示,放一个节点到Master
             */
            String node = redisURI.getHost().concat(":").concat(String.valueOf(redisURI.getPort()));
            indexes.add(node);
            nodeBuilder.append(node).append(",");
            password = redisURI.getPassword() == null ? null : new String(redisURI.getPassword());
            db = String.valueOf(redisURI.getDatabase());
        }
        nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
        Attachment attachment = new Attachment(indexes, "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceMasterSlaveTemplate().setMaster(null).setNodes(nodes).setPassword(password));
        ResourceManager.set(attachment);
    } catch (Throwable t) {
        logger.error(ThrowableUtils.toString(t));
    }
}
Also used : RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) Attachment(com.pamirs.attach.plugin.dynamic.Attachment)

Example 10 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class BaseRedissonTimeSeriesMethodInterceptor method attachment.

/**
 * 入参是org.redisson.config.BaseConfig
 * 但是有的版本是私有类
 *
 * @param config
 */
private void attachment(Object config) {
    try {
        if (Pradar.isClusterTest() || config == null) {
            return;
        }
        ClusterServersConfig config1 = null;
        Reflect reflect = Reflect.on(config);
        String password = null;
        try {
            password = reflect.get("password");
        } catch (Throwable t) {
        }
        Integer database = null;
        try {
            database = Integer.parseInt(String.valueOf(reflect.get("database")));
        } catch (Throwable t) {
        }
        AbstractTemplate template;
        if (config instanceof ClusterServersConfig) {
            List nodeAddresses = reflect.get("nodeAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedisTemplate.RedissionClusterTemplate().setNodes(nodes).setPassword(password);
        } else if (config instanceof MasterSlaveServersConfig) {
            Set nodeAddresses = reflect.get("slaveAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            Object masterNodeObj = reflect.get("masterAddress");
            String masterAddr = "";
            if (masterNodeObj instanceof URI) {
                masterAddr = ((URI) masterNodeObj).getHost().concat(":").concat(String.valueOf(((URI) masterNodeObj).getPort()));
                ;
            } else {
                masterAddr = removePre(String.valueOf(masterNodeObj));
            }
            template = new RedissonMasterSlaveTemplate().setMaster(masterAddr).setNodes(nodes).setPassword(password).setDatabase(database);
        } else if (config instanceof SingleServerConfig) {
            String nodes = "";
            Object address = Reflect.on(config).get("address");
            if (String.class.isAssignableFrom(address.getClass())) {
                nodes = removePre((String) address);
            } else if (URI.class.isAssignableFrom(address.getClass())) {
                URI uri = (URI) address;
                nodes = uri.getHost().concat(":").concat(String.valueOf(uri.getPort()));
            }
            template = new RedissionSingleTemplate().setNodes(nodes).setPassword(password).setDatabase(database);
        } else if (config instanceof SentinelServersConfig) {
            List sentinelAddresses = reflect.get("sentinelAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : sentinelAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedissionSentinelTemplate().setNodes(nodes).setMaster(removePre(((SentinelServersConfig) config).getMasterName())).setNodes(nodes).setDatabase((database));
        } else if (config instanceof ReplicatedServersConfig) {
            List nodeAddresses = reflect.get("nodeAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedissonReplicatedTemplate().setDatabase(database).setNodes(nodes).setPassword(password);
        } else {
            LOGGER.error("Redisson not instanceof any know config:{}", config);
            return;
        }
        final Attachment attachment = new Attachment(null, RedissonConstants.PLUGIN_NAME, new String[] { RedissonConstants.MIDDLEWARE_NAME }, template);
        // ResourceManager.set(attachment);
        Pradar.getInvokeContext().setExt(attachment);
    } catch (Throwable t) {
        LOGGER.error("Redisson attachment error", t);
    }
}
Also used : RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) Set(java.util.Set) RedissonReplicatedTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate) ClusterServersConfig(org.redisson.config.ClusterServersConfig) SingleServerConfig(org.redisson.config.SingleServerConfig) RedissionSingleTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) RedissonMasterSlaveTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate) URI(java.net.URI) ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) AbstractTemplate(com.pamirs.attach.plugin.dynamic.template.AbstractTemplate) SentinelServersConfig(org.redisson.config.SentinelServersConfig) List(java.util.List) RedissionSentinelTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)

Aggregations

Attachment (com.pamirs.attach.plugin.dynamic.Attachment)14 RedisTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate)4 ArrayList (java.util.ArrayList)3 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 C3p0Template (com.pamirs.attach.plugin.dynamic.template.C3p0Template)2 DbcpTemplate (com.pamirs.attach.plugin.dynamic.template.DbcpTemplate)2 Reflect (com.shulie.instrument.simulator.api.reflect.Reflect)2 RedisURI (io.lettuce.core.RedisURI)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 AbstractTemplate (com.pamirs.attach.plugin.dynamic.template.AbstractTemplate)1 HbaseTemplate (com.pamirs.attach.plugin.dynamic.template.HbaseTemplate)1 HikariTemplate (com.pamirs.attach.plugin.dynamic.template.HikariTemplate)1 ProxoolTemplate (com.pamirs.attach.plugin.dynamic.template.ProxoolTemplate)1 RedissionSentinelTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)1 RedissionSingleTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate)1 RedissonMasterSlaveTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate)1 RedissonReplicatedTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1