use of com.pamirs.attach.plugin.apache.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();
int port = -1;
int indexOfColon = -1;
if (brokerAddr != null && (indexOfColon = brokerAddr.indexOf(':')) != -1) {
try {
port = Integer.parseInt(brokerAddr.substring(indexOfColon + 1));
} catch (NumberFormatException e) {
}
brokerAddr = brokerAddr.substring(0, indexOfColon);
}
final List<String> nameServerAddressList = context.getProducer().getmQClientFactory().getMQClientAPIImpl().getNameServerAddressList();
Collections.sort(nameServerAddressList);
traceBean.setStoreHost(StringUtils.join(nameServerAddressList, ","));
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();
context.setMqTraceContext(mqTraceContext);
mqTraceContext.setMqType(MQType.ROCKETMQ);
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());
}
putUserProperty(message, RocketmqConstants.NAME_SERVER_ADDRESS, traceBean.getStoreHost());
} 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);
}
}
}
Aggregations