Search in sources :

Example 1 with HeaderProcessor

use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.

the class ConsumerRecordEntryPointInterceptor2 method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    ConsumerRecord consumerRecord = (ConsumerRecord) args[0];
    String group = null;
    long consumerCell = System.currentTimeMillis() - consumerRecord.timestamp();
    String remoteAddress = null;
    if (args.length >= 3) {
        remoteAddress = KafkaUtils.getRemoteAddress(args[2], manager);
    }
    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 : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 2 with HeaderProcessor

use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.

the class ConsumerTraceInterceptor method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object result = advice.getReturnObj();
    KafkaConsumer kafkaConsumer = (KafkaConsumer) advice.getTarget();
    if (ConsumerHolder.isWorkWithOtherFramework(kafkaConsumer)) {
        return null;
    }
    ConsumerMetaData consumerMetaData = ConsumerHolder.getConsumerMetaData(kafkaConsumer);
    ConsumerRecords consumerRecords = (ConsumerRecords) result;
    if (consumerRecords.isEmpty()) {
        lastPollHasRecordsThreadLocal.set(false);
        return null;
    }
    Iterator iterator = consumerRecords.iterator();
    Object next = iterator.next();
    if (!(next instanceof ConsumerRecord)) {
        return null;
    }
    lastPollHasRecordsThreadLocal.set(true);
    ConsumerRecord consumerRecord = (ConsumerRecord) next;
    SpanRecord spanRecord = new SpanRecord();
    if (PradarSwitcher.isKafkaMessageHeadersEnabled()) {
        HeaderProcessor headerProcessor = HeaderProvider.getHeaderProcessor(consumerRecord);
        Map<String, String> ctx = headerProcessor.getHeaders(consumerRecord);
        spanRecord.setContext(ctx);
    }
    String topic = consumerRecord.topic();
    // TODO 原生这里的kafka服务器信息 和通过spring获取的服务器信息,集群节点顺序不一致
    spanRecord.setRemoteIp(consumerMetaData.getBootstrapServers());
    spanRecord.setRequest(consumerRecords.count());
    spanRecord.setService(consumerRecord.topic());
    boolean clusterTestPrefix = Pradar.isClusterTestPrefix(topic);
    spanRecord.setMethod(clusterTestPrefix ? consumerMetaData.getPtGroupId() : consumerMetaData.getGroupId());
    return spanRecord;
}
Also used : ConsumerMetaData(com.pamirs.attach.plugin.apache.kafka.origin.ConsumerMetaData) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) Iterator(java.util.Iterator) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 3 with HeaderProcessor

use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.

the class ProducerSendInterceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) {
    if (!Pradar.isClusterTest()) {
        return;
    }
    Object[] args = advice.getParameterArray();
    ClusterTestUtils.validateClusterTest();
    try {
        final Callback callback = (Callback) advice.getParameterArray()[1];
        final Map<String, String> context = Pradar.getInvokeContextMap();
        if (callback != null) {
            advice.changeParameter(1, new Callback() {

                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    boolean clear = false;
                    if (PradarService.getInvokeContext().isEmpty()) {
                        PradarInternalService.setInvokeContext(context);
                        clear = true;
                    }
                    callback.onCompletion(metadata, exception);
                    if (clear) {
                        PradarInternalService.clearInvokeContext();
                    }
                }
            });
        }
    } catch (Throwable e) {
        LOGGER.warn("SIMULATOR: kafka send message wrap callback failed.", e);
    }
    ProducerRecord producerRecord = (ProducerRecord) args[0];
    if (null != producerRecord && Pradar.isClusterTest()) {
        String topic = producerRecord.topic();
        if (!Pradar.isClusterTestPrefix(topic)) {
            topic = Pradar.addClusterTestPrefix(topic);
        }
        initTopicField(producerRecord);
        setTopic(producerRecord, topic);
        if (PradarSwitcher.isKafkaMessageHeadersEnabled() && !isHeadReadOnly(producerRecord)) {
            HeaderProcessor headerProcessor = HeaderProvider.getHeaderProcessor(producerRecord);
            headerProcessor.setHeader(producerRecord, PradarService.PRADAR_CLUSTER_TEST_KEY, Boolean.TRUE.toString());
        }
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException)

Example 4 with HeaderProcessor

use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.

the class ConsumerMultiRecordEntryPointInterceptor method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    if (args == null || args.length == 0) {
        return null;
    }
    if (!(args[0] instanceof List)) {
        return null;
    }
    List<ConsumerRecord> list = (List<ConsumerRecord>) args[0];
    if (list.isEmpty()) {
        return null;
    }
    Object consumer = advice.getParameterArray()[2];
    if (consumer instanceof Consumer && consumer.getClass().getName().equals("brave.kafka.clients.TracingConsumer")) {
        consumer = Reflect.on(consumer).get("delegate");
    }
    ConsumerRecord consumerRecord = list.get(0);
    String group = null;
    String remoteAddress = null;
    if (args.length >= 3) {
        group = manager.getDynamicField(consumer, KafkaConstants.DYNAMIC_FIELD_GROUP);
        remoteAddress = KafkaUtils.getRemoteAddress(consumer, manager);
    }
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setRemoteIp(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 ? "" : group);
    spanRecord.setRemoteIp(remoteAddress);
    spanRecord.setCallbackMsg((System.currentTimeMillis() - consumerRecord.timestamp()) + "");
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) Consumer(org.apache.kafka.clients.consumer.Consumer) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 5 with HeaderProcessor

use of com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor in project LinkAgent by shulieTech.

the class ConsumerMultiRecordEntryPointInterceptor2 method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    if (args == null || args.length == 0) {
        return null;
    }
    if (!(args[0] instanceof List)) {
        return null;
    }
    List<ConsumerRecord> list = (List<ConsumerRecord>) args[0];
    if (list.isEmpty()) {
        return null;
    }
    ConsumerRecord consumerRecord = list.get(0);
    String group = null;
    String remoteAddress = null;
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setRemoteIp(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 ? "" : group);
    spanRecord.setRemoteIp(remoteAddress);
    spanRecord.setCallbackMsg((System.currentTimeMillis() - consumerRecord.timestamp()) + "");
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) HeaderProcessor(com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

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