Search in sources :

Example 6 with MQTraceBean

use of com.pamirs.attach.plugin.pulsar.common.MQTraceBean 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 7 with MQTraceBean

use of com.pamirs.attach.plugin.pulsar.common.MQTraceBean in project LinkAgent by shulieTech.

the class PulsarTraceProducerInterceptor method doBefore.

@Override
public void doBefore(Advice advice) {
    Object[] args = advice.getParameterArray();
    String methodName = advice.getBehaviorName();
    Object target = advice.getTarget();
    try {
        if (!(target instanceof org.apache.pulsar.client.impl.ProducerImpl) && methodName.equals("sendAsync")) {
            return;
        }
        ProducerImpl producer = (ProducerImpl) target;
        if (args == null || args.length != 2) {
            return;
        }
        Object messageObj = args[0];
        MessageImpl<?> message = null;
        if (messageObj instanceof MessageImpl) {
            message = (MessageImpl<?>) messageObj;
        }
        if (messageObj instanceof TopicMessageImpl) {
            TopicMessageImpl<?> topicMessage = (TopicMessageImpl<?>) messageObj;
            message = (MessageImpl<?>) topicMessage.getMessage();
        }
        if (message == null) {
            return;
        }
        MQTraceBean traceBean = new MQTraceBean();
        traceBean.setTopic(Pradar.addClusterTestPrefix(producer.getTopic()));
        traceBean.setKeys(message.getKey());
        traceBean.setBodyLength(message.getData().length);
        traceBean.setClusterTest(String.valueOf(Pradar.isClusterTest()));
        ArrayList<MQTraceBean> traceBeans = new ArrayList<MQTraceBean>(1);
        traceBeans.add(traceBean);
        MQTraceContext mqTraceContext = new MQTraceContext();
        mqTraceContext.setMqType(MQType.PULSAR);
        mqTraceContext.setGroup(producer.getProducerName() + ":" + producer.getTopic());
        mqTraceContext.setTraceBeans(traceBeans);
        MQSendMessageTraceLog.sendMessageBefore(mqTraceContext);
        PulsarApi.MessageMetadata.Builder builder = message.getMessageBuilder();
        for (Map.Entry<String, String> entry : traceBean.getContext().entrySet()) {
            builder.addProperties(org.apache.pulsar.common.api.proto.PulsarApi.KeyValue.newBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build());
        }
        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 : ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) PradarException(com.pamirs.pradar.exception.PradarException) ArrayList(java.util.ArrayList) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) MQTraceBean(com.pamirs.attach.plugin.pulsar.common.MQTraceBean) ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) MQTraceContext(com.pamirs.attach.plugin.pulsar.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) Map(java.util.Map)

Aggregations

MQTraceBean (com.pamirs.attach.plugin.pulsar.common.MQTraceBean)7 MQTraceContext (com.pamirs.attach.plugin.pulsar.common.MQTraceContext)4 PradarException (com.pamirs.pradar.exception.PradarException)2 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Message (org.apache.pulsar.client.api.Message)1 MessageImpl (org.apache.pulsar.client.impl.MessageImpl)1 ProducerImpl (org.apache.pulsar.client.impl.ProducerImpl)1 TopicMessageImpl (org.apache.pulsar.client.impl.TopicMessageImpl)1