Search in sources :

Example 71 with PressureMeasureError

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

the class PushConsumeMessageHookImpl method consumeMessageBefore.

@Override
public void consumeMessageBefore(ConsumeMessageContext context) {
    try {
        if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
            return;
        }
        MQTraceContext mqTraceContext = new MQTraceContext();
        context.setMqTraceContext(mqTraceContext);
        mqTraceContext.setMqType(MQType.ROCKETMQ);
        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();
            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.setStoreTime(msg.getStoreTimestamp());
            traceBean.setBrokerName(context.getMq().getBrokerName());
            traceBean.setQueueId(msg.getQueueId());
            traceBean.setOffset(msg.getQueueOffset());
            traceBean.setRetryTimes(msg.getReconsumeTimes());
            traceBean.setProps(context.getProps());
            /**
             * 如果有压测标则设置压测标,没有则根据topic是否是PT_开头,如果是则设置为压测流量
             */
            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");
            }
            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) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) MQTraceBean(com.pamirs.attach.plugin.apache.rocketmq.common.MQTraceBean) MessageExt(org.apache.rocketmq.common.message.MessageExt) MQTraceContext(com.pamirs.attach.plugin.apache.rocketmq.common.MQTraceContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 72 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError 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 = (MQTraceContext) context.getMqTraceContext();
        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.apache.rocketmq.common.MQTraceContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) MQTraceBean(com.pamirs.attach.plugin.apache.rocketmq.common.MQTraceBean)

Example 73 with PressureMeasureError

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

the class AbstractUseShadowConsumerReplaceInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) {
    if (!Pradar.isClusterTest()) {
        return CutOffResult.passed();
    }
    /**
     * 主要负责Consumer 注册,每一批的消息消费都会经过此方法
     * 如果是已经注册过的,则忽略
     */
    DefaultMQPushConsumer defaultMQPushConsumer = (DefaultMQPushConsumer) advice.getTarget();
    /**
     * 如果影子消费者,也忽略
     */
    if (ConsumerRegistry.isShadowConsumer(defaultMQPushConsumer)) {
        return CutOffResult.passed();
    }
    if (!ConsumerRegistry.hasRegistered(defaultMQPushConsumer)) {
        throw new PressureMeasureError(String.format("Apache-RocketMQ: %s err, can't found shadow consumer, queue: %s ", advice.getBehaviorName(), advice.getParameterArray()[0]));
    }
    DefaultMQPushConsumer consumer = ConsumerRegistry.getConsumer(defaultMQPushConsumer);
    if (consumer == null) {
        throw new PressureMeasureError("Apache-RocketMQ: shadow consumer is null this should never happened!");
    }
    return execute(consumer, advice);
}
Also used : PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Example 74 with PressureMeasureError

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

the class ConcurrentlyTraceInterceptor method doBefore.

@Override
public void doBefore(Advice advice) throws Throwable {
    try {
        MessageQueue messageQueue = getMessageQueue(advice.getTarget());
        List<MessageExt> messageExts = getMessages(advice.getTarget());
        String consumeGroup = getConsumeGroup(advice.getTarget());
        if (messageExts == null || messageExts.isEmpty()) {
            return;
        }
        ConsumeMessageContext consumeMessageContext = new ConsumeMessageContext();
        consumeMessageContext.setConsumerGroup(consumeGroup);
        consumeMessageContext.setMq(messageQueue);
        consumeMessageContext.setMsgList(messageExts);
        consumeMessageContext.setSuccess(false);
        hook.consumeMessageBefore(consumeMessageContext);
        contextThreadLocal.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 : MessageExt(org.apache.rocketmq.common.message.MessageExt) 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 75 with PressureMeasureError

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

the class ConsumerPollInterceptor method doWithSpringIntercept.

public void doWithSpringIntercept(Advice advice) {
    try {
        if (!PradarSwitcher.isClusterTestEnabled()) {
            return;
        }
        if (advice.getReturnObj() == null) {
            return;
        }
        ConsumerRecords consumerRecords = (ConsumerRecords) advice.getReturnObj();
        if (consumerRecords.count() <= 0) {
            return;
        }
        Iterator iterator = consumerRecords.iterator();
        Object next = iterator.next();
        if (!(next instanceof ConsumerRecord)) {
            return;
        }
        ConsumerRecord record = (ConsumerRecord) next;
        String topic = record.topic();
        Pradar.setClusterTest(false);
        boolean isClusterTest = Pradar.isClusterTestPrefix(topic);
        if (PradarSwitcher.isKafkaMessageHeadersEnabled()) {
            Headers headers = record.headers();
            Header header = headers.lastHeader(PradarService.PRADAR_CLUSTER_TEST_KEY);
            if (header != null) {
                isClusterTest = isClusterTest || ClusterTestUtils.isClusterTestRequest(BytesUtils.toString(header.value()));
            }
        }
        if (isClusterTest) {
            Pradar.setClusterTest(true);
        }
    } 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 : Header(org.apache.kafka.common.header.Header) PradarException(com.pamirs.pradar.exception.PradarException) Headers(org.apache.kafka.common.header.Headers) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Iterator(java.util.Iterator) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Aggregations

PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)150 PradarException (com.pamirs.pradar.exception.PradarException)34 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)14 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)11 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)8 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)8 HashMap (java.util.HashMap)8 MongoNamespace (com.mongodb.MongoNamespace)7 RequestIndexRename (com.pamirs.attach.plugin.es.common.RequestIndexRename)7 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)7 MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)6 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)6 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)6 List (java.util.List)6 ConsumeMessageContext (org.apache.rocketmq.client.hook.ConsumeMessageContext)6 ConsumeMessageContext (com.alibaba.rocketmq.client.hook.ConsumeMessageContext)5 WrapperRequest (com.pamirs.attach.plugin.hessian.common.WrapperRequest)5