Search in sources :

Example 1 with MQTraceBean

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

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

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

the class MQConsumeMessageTraceLog method consumeMessageBefore.

public static void consumeMessageBefore(MQTraceContext ctx) {
    if (ctx == null || ctx.getTraceBeans() == null || ctx.getTraceBeans().size() == 0 || ctx.getTraceBeans().get(0) == null) {
        return;
    }
    // 记录第一条消息处理开始的时间
    ctx.setStartTime(System.currentTimeMillis());
    // 记录第一条消息开始消费
    // 由于是批量处理,没办法区分消费的调用具体属于哪个消息的调用链
    // 因此,后续消费的调用挂在第一条消息所在的调用链上
    MQTraceBean MQTraceBean = ctx.getTraceBeans().get(0);
    recordSingleMsgBeforeConsume(ctx, MQTraceBean, null);
    ClusterTestUtils.validateClusterTest();
}
Also used : MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)

Example 4 with MQTraceBean

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

the class MQSendMessageTraceLog method sendMessageBefore.

public static void sendMessageBefore(MQTraceContext ctx) {
    if (ctx == null || ctx.getTraceBeans() == null || ctx.getTraceBeans().size() == 0 || ctx.getTraceBeans().get(0) == null) {
        return;
    }
    String service = PradarLogUtils.getService(ctx);
    String method = PradarLogUtils.getMethod(ctx);
    Pradar.startClientInvoke(service, method);
    MQTraceBean traceBean = ctx.getTraceBeans().get(0);
    traceBean.setContext(Pradar.getInvokeContextMap());
    // 如果使用消息头模式传递压测标,则需要显示从消费发送端设置
    Pradar.requestSize(traceBean.getBodyLength());
    Pradar.remoteIp(traceBean.getStoreHost());
    Pradar.remotePort(traceBean.getPort());
    Pradar.middlewareName(RocketmqConstants.PLUGIN_NAME);
    // ThreadLocal 中)
    if (ctx.isAsync()) {
        ctx.setRpcContextInner(Pradar.getInvokeContextMap());
        Pradar.popInvokeContext();
    }
}
Also used : MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)

Example 5 with MQTraceBean

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

the class MQConsumeMessageTraceLog method consumeMessageAfter.

public static void consumeMessageAfter(MQTraceContext ctx) {
    if (ctx == null || ctx.getTraceBeans() == null || ctx.getTraceBeans().size() == 0) {
        return;
    }
    // 批量消息全部处理完毕的时间
    long currentTime = System.currentTimeMillis();
    // 批量消息全部处理完毕的平均耗时
    long costTime = (currentTime - ctx.getStartTime()) / ctx.getTraceBeans().size();
    ctx.setCostTime(costTime);
    String firstTrace = Pradar.getTraceId();
    // 记录第一条消息结束消费,默认配置一次只消费一条消息时本方法到此结束
    if (MQType.ROCKETMQ.equals(ctx.getMqType())) {
        Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_SUCCESS, RocketmqConstants.PLUGIN_TYPE);
    }
    // 如果是批量消息需要特殊处理,后面只是记录一个处理结束的点
    if (ctx.getTraceBeans().size() > 1) {
        // 批量埋点,因为第一条消息结束消费时已记录,所以从第二条消息开始记录
        for (int i = 1; i < ctx.getTraceBeans().size(); i++) {
            MQTraceBean MQTraceBean = ctx.getTraceBeans().get(i);
            recordSingleMsgBeforeConsume(ctx, MQTraceBean, firstTrace);
            Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_SUCCESS, MiddlewareType.TYPE_MQ);
        }
    }
}
Also used : MQTraceBean(com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)

Aggregations

MQTraceBean (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceBean)7 MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)4 PradarException (com.pamirs.pradar.exception.PradarException)4 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)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