Search in sources :

Example 1 with MockKafkaConsumer

use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer in project easeagent by megaease.

the class KafkaConsumerTracingInterceptorTest method afterPoll.

@Test
public void afterPoll() {
    KafkaConsumerTracingInterceptor interceptor = new KafkaConsumerTracingInterceptor();
    MockKafkaConsumer kafkaConsumer = MockKafkaConsumer.buildOne();
    String topic = "testTopic1";
    String uri = (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data();
    ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topic, 1), Collections.singletonList(record(topic, 0))));
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    checkBaseInfo(Objects.requireNonNull(MockEaseAgent.getLastSpan()), topic, uri);
    List<ReportSpan> mockSpans = new ArrayList<>();
    MockEaseAgent.setMockSpanReport(mockSpans::add);
    consumerRecords = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topic, 1), tenRecords(topic)));
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(1, mockSpans.size());
    checkBaseInfo(mockSpans.get(0), topic, uri);
    mockSpans.clear();
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(10, mockSpans.size());
    for (ReportSpan mockSpan : mockSpans) {
        checkBaseInfo(mockSpan, topic, uri);
    }
    mockSpans.clear();
    MockEaseAgent.setMockSpanReport(mockSpans::add);
    consumerRecords = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topic, 1), tenRecords(topic)));
    interceptor.singleRootSpanOnReceiveBatch = false;
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(10, mockSpans.size());
    for (ReportSpan mockSpan : mockSpans) {
        checkBaseInfo(mockSpan, topic, uri);
    }
    mockSpans.clear();
}
Also used : ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) TopicPartition(org.apache.kafka.common.TopicPartition) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) Test(org.junit.Test)

Example 2 with MockKafkaConsumer

use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer in project easeagent by megaease.

the class KafkaMessageListenerTracingInterceptorTest method doBefore.

@Test
public void doBefore() {
    KafkaMessageListenerTracingInterceptor interceptor = new KafkaMessageListenerTracingInterceptor();
    Context context = EaseAgent.getContext();
    MockKafkaConsumer kafkaConsumer = MockKafkaConsumer.buildOne();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { createConsumerRecord(kafkaConsumer) }).build();
    interceptor.doBefore(methodInfo, context);
    context.<Span>remove(KafkaMessageListenerTracingInterceptor.SPAN).finish();
    check(MockEaseAgent.getLastSpan(), (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) Test(org.junit.Test)

Example 3 with MockKafkaConsumer

use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer in project easeagent by megaease.

the class KafkaMessageListenerTracingInterceptorTest method doBeforeRedirected.

@Test
public void doBeforeRedirected() {
    KafkaAbstractConfigConstructInterceptor kafkaAbstractConfigConstructInterceptor = new KafkaAbstractConfigConstructInterceptor();
    Context context = EaseAgent.getContext();
    KafkaTestUtils.mockRedirect(() -> {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        MethodInfo constructMethodInfo = MethodInfo.builder().args(new Object[] { props }).build();
        kafkaAbstractConfigConstructInterceptor.doBefore(constructMethodInfo, EaseAgent.getContext());
        MockKafkaConsumer kafkaConsumer = new MockKafkaConsumer(props);
        kafkaConsumer.setEaseAgent$$DynamicField$$Data(props.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
        KafkaMessageListenerTracingInterceptor interceptor = new KafkaMessageListenerTracingInterceptor();
        MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { createConsumerRecord(kafkaConsumer) }).build();
        interceptor.doBefore(methodInfo, context);
        context.<Span>remove(KafkaMessageListenerTracingInterceptor.SPAN).finish();
        check(MockEaseAgent.getLastSpan(), (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data());
        assertEquals(TestConst.REDIRECT_URIS, MockEaseAgent.getLastSpan().tag("label.remote"));
    });
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) Properties(java.util.Properties) KafkaAbstractConfigConstructInterceptor(com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor) Test(org.junit.Test)

Example 4 with MockKafkaConsumer

use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer in project easeagent by megaease.

the class KafkaConsumerPollInterceptorTest method doAfter.

@Test
public void doAfter() {
    KafkaConsumerPollInterceptor interceptor = new KafkaConsumerPollInterceptor();
    MockKafkaConsumer kafkaConsumer = MockKafkaConsumer.buildOne();
    String topic = "testTopic";
    MockConsumerRecord mockConsumerRecord = MockConsumerRecord.buldOne(topic, 0);
    ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topic, 1), Collections.singletonList(mockConsumerRecord)));
    assertNull(mockConsumerRecord.getEaseAgent$$DynamicField$$Data());
    MethodInfo methodInfo = MethodInfo.builder().invoker(kafkaConsumer).retValue(consumerRecords).build();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNotNull(mockConsumerRecord.getEaseAgent$$DynamicField$$Data());
    assertEquals(kafkaConsumer.getEaseAgent$$DynamicField$$Data(), mockConsumerRecord.getEaseAgent$$DynamicField$$Data());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) MockConsumerRecord(com.megaease.easeagent.plugin.kafka.interceptor.MockConsumerRecord) Test(org.junit.Test)

Example 5 with MockKafkaConsumer

use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer in project easeagent by megaease.

the class KafkaConsumerTracingInterceptorTest method doAfter.

@Test
public void doAfter() {
    KafkaConsumerTracingInterceptor interceptor = new KafkaConsumerTracingInterceptor();
    MockKafkaConsumer kafkaConsumer = MockKafkaConsumer.buildOne();
    String topic = "testTopic1";
    MethodInfo methodInfo = MethodInfo.builder().invoker(kafkaConsumer).throwable(new RuntimeException("testError")).build();
    MockEaseAgent.cleanLastSpan();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(MockEaseAgent.getLastSpan());
    methodInfo = MethodInfo.builder().invoker(kafkaConsumer).build();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(MockEaseAgent.getLastSpan());
    methodInfo = MethodInfo.builder().invoker(kafkaConsumer).retValue(new ConsumerRecords<>(Collections.emptyMap())).build();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(MockEaseAgent.getLastSpan());
    ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topic, 1), Collections.singletonList(record(topic, 0))));
    methodInfo = MethodInfo.builder().invoker(kafkaConsumer).retValue(consumerRecords).build();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    checkBaseInfo(mockSpan, topic, (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data());
}
Also used : ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) TopicPartition(org.apache.kafka.common.TopicPartition) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) Test(org.junit.Test)

Aggregations

MockKafkaConsumer (com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer)7 Test (org.junit.Test)7 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)5 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)4 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 Context (com.megaease.easeagent.plugin.api.Context)3 KafkaAbstractConfigConstructInterceptor (com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor)2 Span (com.megaease.easeagent.plugin.api.trace.Span)1 MockConsumerRecord (com.megaease.easeagent.plugin.kafka.interceptor.MockConsumerRecord)1 Properties (java.util.Properties)1