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();
}
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());
}
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"));
});
}
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());
}
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());
}
Aggregations