Search in sources :

Example 1 with MockKafkaProducer

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"));
    });
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Properties(java.util.Properties) KafkaAbstractConfigConstructInterceptor(com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor) Test(org.junit.Test)

Example 2 with MockKafkaProducer

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);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 3 with MockKafkaProducer

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());
}
Also used : MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) HashMap(java.util.HashMap) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MockKafkaProducer

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"));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Aggregations

MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)4 MockKafkaProducer (com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer)4 Test (org.junit.Test)4 Context (com.megaease.easeagent.plugin.api.Context)3 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)3 Properties (java.util.Properties)2 Span (com.megaease.easeagent.plugin.api.trace.Span)1 KafkaAbstractConfigConstructInterceptor (com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Callback (org.apache.kafka.clients.producer.Callback)1