Search in sources :

Example 6 with HeaderProcessor

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;
}
Also used : HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) Node(org.apache.kafka.common.Node) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) Field(java.lang.reflect.Field) Consumer(org.apache.kafka.clients.consumer.Consumer) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer)

Example 7 with HeaderProcessor

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;
}
Also used : ContextTransfer(com.pamirs.pradar.interceptor.ContextTransfer) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord)

Aggregations

HeaderProcessor (com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor)7 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)5 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)5 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)3 List (java.util.List)2 Consumer (org.apache.kafka.clients.consumer.Consumer)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 ConsumerMetaData (com.pamirs.attach.plugin.apache.kafka.origin.ConsumerMetaData)1 ContextTransfer (com.pamirs.pradar.interceptor.ContextTransfer)1 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)1 Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)1 Callback (org.apache.kafka.clients.producer.Callback)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 Node (org.apache.kafka.common.Node)1