Search in sources :

Example 6 with PradarException

use of com.pamirs.pradar.exception.PradarException in project LinkAgent by shulieTech.

the class OrderlyTraceContextInterceptor method doBefore.

@Override
public void doBefore(Advice advice) throws Throwable {
    try {
        MessageQueue messageQueue = getMessageQueue(advice.getTarget());
        String consumeGroup = getConsumeGroup(advice.getTarget());
        ConsumeMessageContext consumeMessageContext = new ConsumeMessageContext();
        consumeMessageContext.setConsumerGroup(consumeGroup);
        consumeMessageContext.setMq(messageQueue);
        consumeMessageContext.setSuccess(false);
        OrderlyTraceContexts.set(consumeMessageContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    }
}
Also used : ConsumeMessageContext(org.apache.rocketmq.client.hook.ConsumeMessageContext) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 7 with PradarException

use of com.pamirs.pradar.exception.PradarException in project LinkAgent by shulieTech.

the class OrderlyTraceContextInterceptor method doAfter.

@Override
public void doAfter(Advice advice) throws Throwable {
    try {
        ConsumeMessageContext consumeMessageContext = OrderlyTraceContexts.get();
        if (consumeMessageContext == null || consumeMessageContext.getMsgList() == null) {
            return;
        }
        consumeMessageContext.setSuccess(true);
        consumeMessageContext.setStatus("SUCCESS");
        // 兜底,以免after没有执行(consumeMessageContext.getMsgList() != null 说明before 已经执行了)
        PushConsumeMessageHookImpl.getInstance().consumeMessageAfter(consumeMessageContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    } finally {
        OrderlyTraceContexts.remove();
    }
}
Also used : ConsumeMessageContext(org.apache.rocketmq.client.hook.ConsumeMessageContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 8 with PradarException

use of com.pamirs.pradar.exception.PradarException in project LinkAgent by shulieTech.

the class PulsarTraceConsumerInterceptor method doBefore.

@Override
public void doBefore(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    try {
        if (!(target instanceof org.apache.pulsar.client.impl.ConsumerBase)) {
            return;
        }
        if (args == null || args.length != 1) {
            return;
        }
        Object messageObj = args[0];
        if (!(messageObj instanceof Message)) {
            return;
        }
        Message<?> message = (Message<?>) messageObj;
        MQTraceContext mqTraceContext = new MQTraceContext();
        mqTraceContext.setMqType(MQType.PULSAR);
        mqTraceContext.setGroup(message.getProducerName() + ":" + message.getTopicName());
        MQTraceBean traceBean = new MQTraceBean();
        Map<String, String> properties = message.getProperties();
        if (properties != null) {
            Map<String, String> rpcContext = new HashMap<String, String>();
            for (String key : Pradar.getInvokeContextTransformKeys()) {
                String value = ObjectUtils.toString(properties.get(key));
                if (value != null) {
                    rpcContext.put(key, value);
                }
            }
            traceBean.setContext(rpcContext);
        }
        traceBean.setTopic(message.getTopicName());
        traceBean.setKeys(message.getKey());
        traceBean.setBodyLength(message.getData().length);
        // topic是否PT_开头
        boolean isClusterTest = message.getTopicName() != null && (Pradar.isClusterTestPrefix(message.getTopicName()));
        // 消息的properties是否包含Pradar.PRADAR_CLUSTER_TEST_KEY
        if (properties != null) {
            isClusterTest = isClusterTest || StringUtils.equalsIgnoreCase(Boolean.TRUE.toString(), ObjectUtils.toString(properties.get(PradarService.PRADAR_CLUSTER_TEST_KEY)));
        }
        if (isClusterTest) {
            traceBean.setClusterTest(Boolean.TRUE.toString());
        }
        ArrayList<MQTraceBean> traceBeans = new ArrayList<MQTraceBean>(1);
        traceBeans.add(traceBean);
        mqTraceContext.setTraceBeans(traceBeans);
        MQConsumeMessageTraceLog.consumeMessageBefore(mqTraceContext);
        threadLocal.set(mqTraceContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) PradarException(com.pamirs.pradar.exception.PradarException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQTraceBean(com.pamirs.attach.plugin.pulsar.common.MQTraceBean) MQTraceContext(com.pamirs.attach.plugin.pulsar.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 9 with PradarException

use of com.pamirs.pradar.exception.PradarException in project LinkAgent by shulieTech.

the class ZookeeperRegister method init.

@Override
public void init(RegisterOptions registerOptions) {
    if (registerOptions == null) {
        throw new NullPointerException("RegisterOptions is null");
    }
    this.basePath = registerOptions.getRegisterBasePath();
    this.appName = registerOptions.getAppName();
    this.md5 = registerOptions.getMd5();
    this.simulatorConfig = registerOptions.getSimulatorConfig();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[pradar-register] prepare to init register zookeeper node. {}", Pradar.AGENT_ID_CONTAIN_USER_INFO);
    }
    String registerBasePath = null;
    if (StringUtils.endsWith(basePath, "/")) {
        registerBasePath = this.basePath + appName;
    } else {
        registerBasePath = this.basePath + '/' + appName;
    }
    try {
        ZkClientSpec zkClientSpec = new ZkClientSpec();
        zkClientSpec.setZkServers(registerOptions.getZkServers());
        zkClientSpec.setConnectionTimeoutMillis(registerOptions.getConnectionTimeoutMillis());
        zkClientSpec.setSessionTimeoutMillis(registerOptions.getSessionTimeoutMillis());
        zkClientSpec.setThreadName("heartbeat");
        this.zkClient = NetflixCuratorZkClientFactory.getInstance().create(zkClientSpec);
    } catch (PradarException e) {
        LOGGER.error("[pradar-register] ZookeeperRegister init error.", e);
        throw e;
    } catch (Throwable e) {
        LOGGER.error("[pradar-register] ZookeeperRegister init error.", e);
        throw new PradarException(e);
    }
    String client = Pradar.AGENT_ID_CONTAIN_USER_INFO;
    try {
        this.zkClient.ensureDirectoryExists(registerBasePath);
    } catch (Throwable e) {
        LOGGER.error("[register] ensureDirectoryExists err:{}", registerBasePath, e);
    }
    this.heartbeatPath = registerBasePath + '/' + client;
    try {
        if (this.zkClient.exists(this.heartbeatPath)) {
            this.zkClient.deleteQuietly(this.heartbeatPath);
        }
    } catch (Throwable e) {
    }
    cleanExpiredNodes(registerBasePath);
    this.heartbeatNode = this.zkClient.createHeartbeatNode(this.heartbeatPath);
    PradarSwitcher.registerListener(new PradarSwitcher.PradarSwitcherListener() {

        @Override
        public void onListen(Event event) {
            if (event instanceof PradarSwitchEvent || event instanceof ErrorEvent) {
                refresh();
            }
        }
    });
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[pradar-register] init register zookeeper node successful. {}", Pradar.AGENT_ID_CONTAIN_USER_INFO);
    }
}
Also used : PradarException(com.pamirs.pradar.exception.PradarException) ZkClientSpec(com.shulie.instrument.module.register.zk.impl.ZkClientSpec) ErrorEvent(com.pamirs.pradar.event.ErrorEvent) ErrorEvent(com.pamirs.pradar.event.ErrorEvent) Event(com.pamirs.pradar.event.Event) PradarSwitchEvent(com.pamirs.pradar.event.PradarSwitchEvent) PradarSwitchEvent(com.pamirs.pradar.event.PradarSwitchEvent)

Example 10 with PradarException

use of com.pamirs.pradar.exception.PradarException in project LinkAgent by shulieTech.

the class NetflixCuratorZkClientFactory method create.

public ZkClient create(final ZkClientSpec spec) throws Exception {
    if (StringUtils.isBlank(spec.getZkServers())) {
        throw new PradarException("zookeeper servers is empty.");
    }
    String path = ZooKeeper.class.getProtectionDomain().getCodeSource().getLocation().toString();
    if (logger.isInfoEnabled()) {
        logger.info("Load ZooKeeper from {}", path);
    }
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(spec.getZkServers()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).connectionTimeoutMs(spec.getConnectionTimeoutMillis()).sessionTimeoutMs(spec.getSessionTimeoutMillis()).threadFactory(new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, spec.getThreadName());
            t.setDaemon(true);
            t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    logger.error("Thread {} caught a unknow exception with UncaughtExceptionHandler", t.getName(), e);
                }
            });
            return t;
        }
    }).build();
    client.start();
    if (logger.isInfoEnabled()) {
        logger.info("ZkClient started: {}", spec.getZkServers());
    }
    NetflixCuratorZkClient theClient = new NetflixCuratorZkClient(client, spec.getZkServers());
    return theClient;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeper(org.apache.zookeeper.ZooKeeper) PradarException(com.pamirs.pradar.exception.PradarException) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Aggregations

PradarException (com.pamirs.pradar.exception.PradarException)46 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)34 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)7 ConsumeMessageContext (com.alibaba.rocketmq.client.hook.ConsumeMessageContext)6 MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)6 ZipException (java.util.zip.ZipException)6 ConsumeMessageContext (org.apache.rocketmq.client.hook.ConsumeMessageContext)6 KeeperException (org.apache.zookeeper.KeeperException)6 MQTraceContext (com.pamirs.attach.plugin.apache.rocketmq.common.MQTraceContext)5 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)4 MQTraceBean (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)4 MQTraceBean (com.pamirs.attach.plugin.apache.rocketmq.common.MQTraceBean)4 InetSocketAddress (java.net.InetSocketAddress)4 MessageExt (org.apache.rocketmq.common.message.MessageExt)4 MQTraceContext (com.pamirs.attach.plugin.pulsar.common.MQTraceContext)3 ConcurrentWeakHashMap (com.shulie.instrument.simulator.message.ConcurrentWeakHashMap)3 List (java.util.List)3 Map (java.util.Map)3 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)3