Search in sources :

Example 86 with SpanRecord

use of com.pamirs.pradar.interceptor.SpanRecord 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 87 with SpanRecord

use of com.pamirs.pradar.interceptor.SpanRecord in project LinkAgent by shulieTech.

the class ConsumerTraceInterceptor method exceptionTrace.

@Override
public SpanRecord exceptionTrace(Advice advice) {
    if (ConsumerHolder.isWorkWithOtherFramework((Consumer) advice.getTarget())) {
        return null;
    }
    Throwable throwable = advice.getThrowable();
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setRequest("poll");
    spanRecord.setResponse(throwable);
    spanRecord.setResultCode(ResultCode.INVOKE_RESULT_FAILED);
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord)

Example 88 with SpanRecord

use of com.pamirs.pradar.interceptor.SpanRecord in project LinkAgent by shulieTech.

the class ConsumerTraceInterceptor method afterTrace.

@Override
public SpanRecord afterTrace(Advice advice) {
    if (ConsumerHolder.isWorkWithOtherFramework((Consumer) advice.getTarget())) {
        return null;
    }
    Boolean lastPollHasRecords = lastPollHasRecordsThreadLocal.get();
    if (lastPollHasRecords == null || !lastPollHasRecords) {
        return null;
    }
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setResponse("next poll");
    spanRecord.setResultCode(ResultCode.INVOKE_RESULT_SUCCESS);
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord)

Example 89 with SpanRecord

use of com.pamirs.pradar.interceptor.SpanRecord 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 90 with SpanRecord

use of com.pamirs.pradar.interceptor.SpanRecord in project LinkAgent by shulieTech.

the class ConsumerMultiRecordEntryPointInterceptor method afterTrace.

@Override
public SpanRecord afterTrace(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);
    SpanRecord spanRecord = new SpanRecord();
    spanRecord.setRequest(consumerRecord);
    spanRecord.setResponse(advice.getReturnObj());
    return spanRecord;
}
Also used : SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Aggregations

SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)199 JSONObject (com.alibaba.fastjson.JSONObject)19 SocketTimeoutException (java.net.SocketTimeoutException)15 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)13 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)11 HashMap (java.util.HashMap)10 List (java.util.List)8 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)6 Request (com.weibo.api.motan.rpc.Request)6 HeaderProcessor (com.pamirs.attach.plugin.apache.kafka.header.HeaderProcessor)5 WrapperRequest (com.pamirs.attach.plugin.hessian.common.WrapperRequest)5 URI (java.net.URI)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Metadata (io.grpc.Metadata)4 ServerAddress (com.mongodb.ServerAddress)3 Channel (com.rabbitmq.client.Channel)3 Connection (com.rabbitmq.client.Connection)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 URL (java.net.URL)3