Search in sources :

Example 1 with MQTraceContext

use of com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext in project LinkAgent by shulieTech.

the class SendMessageHookImpl method sendMessageBefore.

@Override
public void sendMessageBefore(SendMessageContext context) {
    try {
        if (context == null || context.getMessage() == null) {
            return;
        }
        Message message = context.getMessage();
        if (message == null) {
            return;
        }
        ClusterTestUtils.validateClusterTest();
        MQTraceBean traceBean = new MQTraceBean();
        traceBean.setTopic(message.getTopic());
        traceBean.setOriginMsgId(MessageAccessor.getOriginMessageId(message));
        traceBean.setTags(message.getTags());
        traceBean.setKeys(message.getKeys());
        traceBean.setBuyerId(message.getBuyerId());
        traceBean.setTransferFlag(MessageAccessor.getTransferFlag(message));
        traceBean.setCorrectionFlag(MessageAccessor.getCorrectionFlag(message));
        traceBean.setBodyLength(message.getBody().length);
        traceBean.setBornHost(context.getBornHost());
        String brokerAddr = context.getBrokerAddr();
        String port = "";
        int index = -1;
        if (brokerAddr != null && (index = brokerAddr.indexOf(':')) != -1) {
            port = brokerAddr.substring(index + 1);
            brokerAddr = brokerAddr.substring(0, index);
        }
        traceBean.setStoreHost(brokerAddr);
        traceBean.setPort(port);
        traceBean.setBrokerName(context.getMq().getBrokerName());
        // 上游AppName,记录当前应用的AppName
        traceBean.setProps(context.getProps());
        ArrayList<MQTraceBean> traceBeans = new ArrayList<MQTraceBean>(1);
        traceBeans.add(traceBean);
        MQTraceContext mqTraceContext = new MQTraceContext();
        contexts.put(context, mqTraceContext);
        mqTraceContext.setMqType(MQType.ROCKETMQ);
        mqTraceContext.setTopic(context.getMq().getTopic());
        mqTraceContext.setGroup(context.getProducerGroup());
        mqTraceContext.setAsync(CommunicationMode.ASYNC == context.getCommunicationMode());
        mqTraceContext.setTraceBeans(traceBeans);
        Map<String, String> rpcContext = new HashMap<String, String>();
        for (String key : Pradar.getInvokeContextTransformKeys()) {
            String value = message.getProperty(key);
            if (value != null) {
                rpcContext.put(key, value);
            }
        }
        traceBean.setContext(rpcContext);
        traceBean.setClusterTest(message.getUserProperty(PradarService.PRADAR_CLUSTER_TEST_KEY));
        MQSendMessageTraceLog.sendMessageBefore(mqTraceContext);
        for (Map.Entry<String, String> entry : traceBean.getContext().entrySet()) {
            putUserProperty(message, entry.getKey(), entry.getValue());
        }
    } 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(com.alibaba.rocketmq.common.message.Message) PradarException(com.pamirs.pradar.exception.PradarException) HashMap(java.util.HashMap) ConcurrentWeakHashMap(com.shulie.instrument.simulator.message.ConcurrentWeakHashMap) ArrayList(java.util.ArrayList) MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean) MQTraceContext(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentWeakHashMap(com.shulie.instrument.simulator.message.ConcurrentWeakHashMap)

Example 2 with MQTraceContext

use of com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext in project LinkAgent by shulieTech.

the class SendMessageHookImpl method sendMessageAfter.

@Override
public void sendMessageAfter(SendMessageContext context) {
    try {
        if (context == null || context.getMessage() == null) {
            return;
        }
        MQTraceContext mqTraceContext = contexts.remove(context);
        if (mqTraceContext == null) {
            return;
        }
        MQTraceBean traceBean = mqTraceContext.getTraceBeans().get(0);
        if (traceBean != null && context.getSendResult() != null) {
            traceBean.setQueueId(context.getMq().getQueueId());
            traceBean.setMsgId(context.getSendResult().getMsgId());
            traceBean.setOffset(context.getSendResult().getQueueOffset());
            mqTraceContext.setSuccess(true);
            mqTraceContext.setStatus(context.getSendResult().getSendStatus().toString());
        } else {
            if (context.getException() != null) {
                String msg = context.getException().getMessage();
                mqTraceContext.setErrorMsg(StringUtils.substring(msg, 0, msg.indexOf("\n")));
            }
        }
        MQSendMessageTraceLog.sendMessageAfter(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 : MQTraceContext(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)

Example 3 with MQTraceContext

use of com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext in project LinkAgent by shulieTech.

the class PullConsumeMessageHookImpl method consumeMessageAfter.

@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
    try {
        if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
            return;
        }
        MQTraceContext mqTraceContext = contexts.remove(context);
        if (mqTraceContext == null) {
            return;
        }
        mqTraceContext.setSuccess(context.isSuccess());
        mqTraceContext.setStatus(context.getStatus());
        MQConsumeMessageTraceLog.consumeMessageAfter(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 : MQTraceContext(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 4 with MQTraceContext

use of com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext in project LinkAgent by shulieTech.

the class PullConsumeMessageHookImpl method consumeMessageBefore.

@Override
public void consumeMessageBefore(ConsumeMessageContext context) {
    try {
        if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
            return;
        }
        MQTraceContext mqTraceContext = new MQTraceContext();
        contexts.put(context, mqTraceContext);
        mqTraceContext.setMqType(MQType.ROCKETMQ);
        mqTraceContext.setTopic(context.getMq().getTopic());
        mqTraceContext.setGroup(context.getConsumerGroup());
        boolean silence = PradarService.isSilence();
        List<MQTraceBean> beans = new ArrayList<MQTraceBean>();
        for (MessageExt msg : context.getMsgList()) {
            if (msg == null) {
                continue;
            }
            MQTraceBean traceBean = new MQTraceBean();
            boolean isClusterTest = msg.getTopic() != null && (Pradar.isClusterTestPrefix(msg.getTopic()) || Pradar.isClusterTestPrefix(msg.getTopic(), RETRYSTR) || Pradar.isClusterTestPrefix(msg.getTopic(), DLQSTR));
            // 消息的properties是否包含Pradar.PRADAR_CLUSTER_TEST_KEY
            isClusterTest = isClusterTest || ClusterTestUtils.isClusterTestRequest(msg.getProperty(PradarService.PRADAR_CLUSTER_TEST_KEY));
            if (isClusterTest) {
                traceBean.setClusterTest(Boolean.TRUE.toString());
            }
            if (silence && isClusterTest) {
                throw new PressureMeasureError(this.getClass().getName() + "silence module ! can not handle cluster test data");
            }
            Map<String, String> rpcContext = new HashMap<String, String>();
            for (String key : Pradar.getInvokeContextTransformKeys()) {
                String value = msg.getProperty(key);
                if (value != null) {
                    rpcContext.put(key, value);
                }
            }
            traceBean.setContext(rpcContext);
            traceBean.setTopic(msg.getTopic());
            traceBean.setMsgId(msg.getMsgId());
            traceBean.setOriginMsgId(MessageAccessor.getOriginMessageId(msg));
            traceBean.setTags(msg.getTags());
            traceBean.setKeys(msg.getKeys());
            traceBean.setBuyerId(msg.getBuyerId());
            traceBean.setTransferFlag(MessageAccessor.getTransferFlag(msg));
            traceBean.setCorrectionFlag(MessageAccessor.getCorrectionFlag(msg));
            traceBean.setBodyLength(msg.getBody().length);
            traceBean.setBornHost(StringUtils.substring(msg.getBornHost().toString(), 1));
            String storeHost = "";
            String port = "";
            if (msg.getStoreHost() != null && msg.getStoreHost() instanceof InetSocketAddress) {
                InetSocketAddress address = (InetSocketAddress) msg.getStoreHost();
                storeHost = address.getAddress() == null ? null : address.getAddress().getHostAddress();
                port = String.valueOf(address.getPort());
            } else {
                storeHost = StringUtils.substring(msg.getStoreHost().toString(), 1);
            }
            traceBean.setStoreHost(storeHost);
            traceBean.setPort(port);
            traceBean.setStoreTime(msg.getStoreTimestamp());
            traceBean.setBrokerName(context.getMq().getBrokerName());
            traceBean.setQueueId(msg.getQueueId());
            traceBean.setOffset(msg.getQueueOffset());
            traceBean.setRetryTimes(msg.getReconsumeTimes());
            traceBean.setProps(context.getProps());
            beans.add(traceBean);
        }
        mqTraceContext.setTraceBeans(beans);
        MQConsumeMessageTraceLog.consumeMessageBefore(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 : PradarException(com.pamirs.pradar.exception.PradarException) HashMap(java.util.HashMap) ConcurrentWeakHashMap(com.shulie.instrument.simulator.message.ConcurrentWeakHashMap) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean) MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MQTraceContext(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 5 with MQTraceContext

use of com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext in project LinkAgent by shulieTech.

the class PushConsumeMessageHookImpl method consumeMessageAfter.

@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
    try {
        if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
            return;
        }
        MQTraceContext mqTraceContext = contexts.remove(context);
        if (mqTraceContext == null) {
            return;
        }
        mqTraceContext.setSuccess(context.isSuccess());
        mqTraceContext.setStatus(context.getStatus());
        MQConsumeMessageTraceLog.consumeMessageAfter(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 : MQTraceContext(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Aggregations

MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)6 PradarException (com.pamirs.pradar.exception.PradarException)6 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)6 MQTraceBean (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)4 ConcurrentWeakHashMap (com.shulie.instrument.simulator.message.ConcurrentWeakHashMap)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)2 InetSocketAddress (java.net.InetSocketAddress)2 Message (com.alibaba.rocketmq.common.message.Message)1 Map (java.util.Map)1