use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer in project easeagent by megaease.
the class KafkaProducerDoSendInterceptorTest method testRedirectedTag.
@Test
public void testRedirectedTag() {
KafkaAbstractConfigConstructInterceptor kafkaAbstractConfigConstructInterceptor = new KafkaAbstractConfigConstructInterceptor();
KafkaTestUtils.mockRedirect(() -> {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { props }).build();
kafkaAbstractConfigConstructInterceptor.doBefore(methodInfo, EaseAgent.getContext());
MockKafkaProducer kafkaProducer = new MockKafkaProducer(props);
kafkaProducer.setEaseAgent$$DynamicField$$Data(props.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
Context context = EaseAgent.getContext();
ProducerRecord record = new ProducerRecord<>(topic, key, value);
methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
finishSpan();
assertFalse(context.currentTracing().hasCurrentSpan());
ReportSpan mockSpan = MockEaseAgent.getLastSpan();
assertEquals(TestConst.REDIRECT_URIS, mockSpan.tag("label.remote"));
});
}
use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer in project easeagent by megaease.
the class KafkaProducerDoSendInterceptorTest method doBefore.
@Test
public void doBefore() {
KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
MockKafkaProducer kafkaProducer = MockKafkaProducer.buildOne();
Context context = EaseAgent.getContext();
ProducerRecord record = new ProducerRecord<>(topic, key, value);
MethodInfo methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
Span span = finishSpan();
assertFalse(context.currentTracing().hasCurrentSpan());
ReportSpan mockSpan = MockEaseAgent.getLastSpan();
SpanTestUtils.sameId(span, mockSpan);
checkBaseInfo(mockSpan);
assertEquals(key, mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
record = new ProducerRecord<>(topic, value);
methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
context = EaseAgent.getContext();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
span = finishSpan();
assertFalse(context.currentTracing().hasCurrentSpan());
mockSpan = MockEaseAgent.getLastSpan();
SpanTestUtils.sameId(span, mockSpan);
checkBaseInfo(mockSpan);
assertNull(mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
record = new ProducerRecord<>(topic, value);
methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
context = EaseAgent.getContext();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
finishSpan();
assertFalse(context.currentTracing().hasCurrentSpan());
assertNotNull(methodInfo.getArgs()[1]);
assertTrue(methodInfo.getArgs()[1] instanceof TraceCallback);
}
use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer in project easeagent by megaease.
the class KafkaProducerConstructInterceptorTest method doAfter.
@Test
public void doAfter() {
KafkaProducerConstructInterceptor interceptor = new KafkaProducerConstructInterceptor();
Map config = new HashMap();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
config.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
config.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
MockKafkaProducer mockKafkaProducer = new MockKafkaProducer(config);
MethodInfo methodInfo = MethodInfo.builder().invoker(mockKafkaProducer).args(new Object[] { config }).build();
interceptor.doAfter(methodInfo, EaseAgent.getContext());
assertEquals(TestConst.URIS, mockKafkaProducer.getEaseAgent$$DynamicField$$Data());
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
mockKafkaProducer = new MockKafkaProducer(props);
methodInfo = MethodInfo.builder().invoker(mockKafkaProducer).args(new Object[] { props }).build();
interceptor.doAfter(methodInfo, EaseAgent.getContext());
assertEquals(TestConst.URIS, mockKafkaProducer.getEaseAgent$$DynamicField$$Data());
}
use of com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer in project easeagent by megaease.
the class KafkaProducerDoSendInterceptorTest method doAfter.
@Test
public void doAfter() {
KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
Context context = EaseAgent.getContext();
MockKafkaProducer kafkaProducer = MockKafkaProducer.buildOne();
interceptor.doAfter(null, context);
ProducerRecord record = new ProducerRecord<>(topic, key, value);
MethodInfo methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
interceptor.doAfter(methodInfo, context);
assertFalse(context.currentTracing().hasCurrentSpan());
Callback callback = (Callback) methodInfo.getArgs()[1];
callback.onCompletion(null, null);
ReportSpan mockSpan = MockEaseAgent.getLastSpan();
assertFalse(mockSpan.tags().containsKey("error"));
record = new ProducerRecord<>(topic, key, value);
String errorInfo = "test error";
methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).throwable(new RuntimeException(errorInfo)).build();
interceptor.doBefore(methodInfo, context);
assertTrue(context.currentTracing().hasCurrentSpan());
interceptor.doAfter(methodInfo, context);
assertFalse(context.currentTracing().hasCurrentSpan());
callback = (Callback) methodInfo.getArgs()[1];
callback.onCompletion(null, null);
mockSpan = MockEaseAgent.getLastSpan();
assertTrue(mockSpan.tags().containsKey("error"));
assertEquals(errorInfo, mockSpan.tags().get("error"));
}
Aggregations