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);
}
}
}
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);
}
}
}
Aggregations