use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.
the class ConsumerRecordEntryPointInterceptor method beforeTrace.
@Override
public SpanRecord beforeTrace(Advice advice) {
Object[] args = advice.getParameterArray();
ConsumerRecord consumerRecord = (ConsumerRecord) args[0];
Object consumer = advice.getParameterArray()[2];
if (consumer instanceof Consumer && consumer.getClass().getName().equals("brave.kafka.clients.TracingConsumer")) {
consumer = Reflect.on(consumer).get("delegate");
}
long consumerCell = System.currentTimeMillis() - consumerRecord.timestamp();
String remoteAddress = null;
if (args.length >= 3) {
remoteAddress = KafkaUtils.getRemoteAddress(args[2], manager);
}
if (remoteAddress == null) {
Object metadata = Reflect.on(consumer).get("metadata");
Object cluster = ReflectUtil.reflectSlience(metadata, "cluster");
Iterable<Node> nodes = null;
if (cluster != null) {
nodes = Reflect.on(cluster).get("nodes");
} else {
Object cache = ReflectUtil.reflectSlience(metadata, "cache");
if (cache != null) {
nodes = ReflectUtil.reflectSlience(cache, "nodes");
}
}
StringBuilder sb = new StringBuilder();
if (nodes != null) {
for (Node node : nodes) {
sb.append(Reflect.on(node).get("host").toString()).append(":").append(Reflect.on(node).get("port").toString()).append(",");
}
remoteAddress = sb.toString();
}
}
String group = null;
if (groupGlobal == null) {
if (args.length >= 3) {
group = manager.removeField(args[2], KafkaConstants.DYNAMIC_FIELD_GROUP);
}
if (group == null) {
try {
Field groupId = ReflectUtil.getField(consumer, "groupId");
group = (String) groupId.get(consumer);
} catch (Throwable e) {
Object coordinator = Reflect.on(consumer).get(KafkaConstants.REFLECT_FIELD_COORDINATOR);
group = ReflectUtil.reflectSlience(coordinator, KafkaConstants.REFLECT_FIELD_GROUP_ID);
groupGlobal = Thread.currentThread().getName().split("-")[0];
}
}
}
SpanRecord spanRecord = new SpanRecord();
spanRecord.setRemoteIp(remoteAddress == null ? "127.0.0.1:9092" : remoteAddress);
if (PradarSwitcher.isKafkaMessageHeadersEnabled()) {
HeaderProcessor headerProcessor = HeaderProvider.getHeaderProcessor(consumerRecord);
Map<String, String> ctx = headerProcessor.getHeaders(consumerRecord);
spanRecord.setContext(ctx);
}
spanRecord.setRequest(consumerRecord);
spanRecord.setService(consumerRecord.topic());
spanRecord.setMethod(group == null ? groupGlobal : group);
spanRecord.setCallbackMsg(consumerCell + "");
return spanRecord;
}
use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.
the class ProducerSendInterceptor method getContextTransfer.
@Override
protected ContextTransfer getContextTransfer(Advice advice) {
Object[] args = advice.getParameterArray();
final ProducerRecord producerRecord = (ProducerRecord) args[0];
if (PradarSwitcher.isKafkaMessageHeadersEnabled() && !isHeadReadOnly(producerRecord)) {
return new ContextTransfer() {
@Override
public void transfer(String key, String value) {
HeaderProcessor headerProcessor = HeaderProvider.getHeaderProcessor(producerRecord);
headerProcessor.setHeader(producerRecord, key, value);
}
};
}
return null;
}
Aggregations