Search in sources :

Example 1 with KafkaAbstractConfigConstructInterceptor

use of com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor 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 KafkaAbstractConfigConstructInterceptor

use of com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor 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 3 with KafkaAbstractConfigConstructInterceptor

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

the class KafkaConsumerTracingInterceptorTest method setConsumerSpan.

@Test
public void setConsumerSpan() {
    KafkaConsumerTracingInterceptor interceptor = new KafkaConsumerTracingInterceptor();
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan().start();
    String topic = "testTopic";
    String uri = "testUri";
    interceptor.setConsumerSpan(topic, uri, span);
    span.finish();
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    checkBaseInfo(mockSpan, topic, uri);
    KafkaAbstractConfigConstructInterceptor kafkaAbstractConfigConstructInterceptor = new KafkaAbstractConfigConstructInterceptor();
    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 methodInfo = MethodInfo.builder().args(new Object[] { props }).build();
        kafkaAbstractConfigConstructInterceptor.doBefore(methodInfo, EaseAgent.getContext());
        MockKafkaConsumer kafkaConsumer = new MockKafkaConsumer(props);
        kafkaConsumer.setEaseAgent$$DynamicField$$Data(props.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
        Span span1 = context.nextSpan().start();
        interceptor.setConsumerSpan(topic, (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data(), span1);
        span1.finish();
        ReportSpan mockSpan1 = MockEaseAgent.getLastSpan();
        assertEquals(TestConst.REDIRECT_URIS, mockSpan1.tag("label.remote"));
    });
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) KafkaAbstractConfigConstructInterceptor(com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor) Test(org.junit.Test)

Example 4 with KafkaAbstractConfigConstructInterceptor

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

the class KafkaMetricTest method newTags.

@Test
public void newTags() {
    Tags tags = KafkaMetric.newTags();
    assertEquals("application", tags.getCategory());
    assertEquals("kafka", tags.getType());
    assertEquals("resource", tags.getKeyFieldName());
    KafkaAbstractConfigConstructInterceptor interceptor = new KafkaAbstractConfigConstructInterceptor();
    KafkaTestUtils.mockRedirect(() -> {
        Map config = new HashMap();
        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
        MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { config }).build();
        interceptor.doBefore(methodInfo, EaseAgent.getContext());
        String testTagKey = "tagKey";
        String testTagValue = "tagValue";
        Map<String, String> oldTags = RedirectProcessor.INSTANCE.getTags();
        Map<String, String> newTagsMap = new HashMap<>();
        newTagsMap.put(testTagKey, testTagValue);
        AgentFieldReflectAccessor.setFieldValue(RedirectProcessor.INSTANCE, "tags", newTagsMap);
        try {
            Tags newTags = KafkaMetric.newTags();
            assertEquals(testTagValue, newTags.getTags().get(testTagKey));
        } finally {
            AgentFieldReflectAccessor.setFieldValue(RedirectProcessor.INSTANCE, "tags", oldTags);
        }
    });
}
Also used : HashMap(java.util.HashMap) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) HashMap(java.util.HashMap) Map(java.util.Map) Tags(com.megaease.easeagent.plugin.api.metric.name.Tags) KafkaAbstractConfigConstructInterceptor(com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor) Test(org.junit.Test)

Aggregations

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