Search in sources :

Example 26 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan in project easeagent by megaease.

the class KafkaConsumerTracingInterceptorTest method afterPoll2.

@Test
public void afterPoll2() {
    KafkaConsumerTracingInterceptor interceptor = new KafkaConsumerTracingInterceptor();
    MockKafkaConsumer kafkaConsumer = MockKafkaConsumer.buildOne();
    Map<TopicPartition, List<ConsumerRecord<String, String>>> records = new HashMap<>();
    records.put(new TopicPartition(topic1, 1), Collections.singletonList(record(topic1, 0)));
    records.put(new TopicPartition(topic2, 1), Collections.singletonList(record(topic2, 0)));
    List<ReportSpan> mockSpans = new ArrayList<>();
    MockEaseAgent.setMockSpanReport(mockSpans::add);
    String uri = (String) kafkaConsumer.getEaseAgent$$DynamicField$$Data();
    ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(records);
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(2, mockSpans.size());
    checkTowTopicMockSpans(mockSpans, uri);
    mockSpans.clear();
    consumerRecords = new ConsumerRecords<>(towTopicAndTenRecords());
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(2, mockSpans.size());
    checkTowTopicMockSpans(mockSpans, uri);
    mockSpans.clear();
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(10, mockSpans.size());
    checkTowTopicMockSpans(mockSpans, uri);
    mockSpans.clear();
    MockEaseAgent.setMockSpanReport(mockSpans::add);
    consumerRecords = new ConsumerRecords<>(towTopicAndTenRecords());
    interceptor.singleRootSpanOnReceiveBatch = false;
    interceptor.afterPoll(EaseAgent.getContext(), consumerRecords, uri);
    assertEquals(10, mockSpans.size());
    checkTowTopicMockSpans(mockSpans, uri);
    mockSpans.clear();
}
Also used : MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.Test)

Example 27 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan 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 28 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan in project easeagent by megaease.

the class JdbcStmTracingInterceptorTest method doBefore.

@Test
public void doBefore() throws SQLException {
    JdbcStmTracingInterceptor interceptor = new JdbcStmTracingInterceptor();
    Context context = EaseAgent.getContext();
    InterceptorTestUtils.init(interceptor, new JdbcTracingPlugin());
    String method = "test_method";
    MethodInfo methodInfo = MethodInfo.builder().method(method).build();
    interceptor.doBefore(methodInfo, context);
    assertNull(context.get(JdbcStmTracingInterceptor.SPAN_KEY));
    SqlInfo sqlInfo = new SqlInfo(TestUtils.mockConnection());
    String sql = "select * from data";
    sqlInfo.addSql(sql, false);
    context.put(SqlInfo.class, sqlInfo);
    interceptor.doBefore(methodInfo, context);
    Span span = context.remove(JdbcStmTracingInterceptor.SPAN_KEY);
    assertNotNull(span);
    span.finish();
    ReportSpan reportSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, reportSpan);
    assertEquals(method, reportSpan.name());
    assertEquals(Span.Kind.CLIENT.name(), reportSpan.kind());
    assertEquals(SQLCompressionFactory.getSqlCompression().compress(sql), reportSpan.tag(JdbcStmTracingInterceptor.SPAN_SQL_QUERY_TAG_NAME));
    assertEquals("database", reportSpan.tag(JdbcStmTracingInterceptor.SPAN_LOCAL_COMPONENT_TAG_NAME));
    assertEquals(TestUtils.URI, reportSpan.tag(JdbcStmTracingInterceptor.SPAN_URL));
    assertEquals(Type.DATABASE.getRemoteType(), reportSpan.tag(MiddlewareConstants.TYPE_TAG_NAME));
    assertEquals(TestUtils.DATABASE, reportSpan.remoteServiceName());
    assertEquals(TestUtils.HOST, reportSpan.remoteEndpoint().ipv4());
    assertEquals(TestUtils.PORT, reportSpan.remoteEndpoint().port());
    TestUtils.setRedirect();
    RedirectProcessor.redirected(Redirect.DATABASE, TestUtils.URI);
    interceptor.doBefore(methodInfo, context);
    span = context.remove(JdbcStmTracingInterceptor.SPAN_KEY);
    assertNotNull(span);
    span.finish();
    reportSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, reportSpan);
    assertEquals(TestUtils.URI, reportSpan.tag(MiddlewareConstants.REDIRECTED_LABEL_REMOTE_TAG_NAME));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) SqlInfo(com.megaease.easeagent.plugin.jdbc.common.SqlInfo) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) JdbcTracingPlugin(com.megaease.easeagent.plugin.jdbc.JdbcTracingPlugin) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 29 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan in project easeagent by megaease.

the class AgentCoreSubscriberTest method onError.

@Test
public void onError() throws URISyntaxException {
    MockCoreSubscriber mockCoreSubscriber = new MockCoreSubscriber();
    String errorInfo = "test error";
    WebClientFilterTracingInterceptor interceptor = new WebClientFilterTracingInterceptor();
    Context context = EaseAgent.getContext();
    URI uri = new URI(url);
    ClientRequest clientRequest = MockClientRequest.build(uri);
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).retValue(new MockMono()).build();
    interceptor.doBefore(methodInfo, context);
    RequestContext requestContext = context.get(interceptor.getProgressKey());
    AgentCoreSubscriber agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, methodInfo, requestContext);
    agentCoreSubscriber.onError(new RuntimeException(errorInfo));
    assertFalse(methodInfo.isSuccess());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertTrue(mockSpan.hasError());
    assertEquals(errorInfo, mockSpan.errorInfo());
    assertTrue(mockCoreSubscriber.onError.get());
    MockEaseAgent.cleanLastSpan();
    methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).retValue(new MockMono()).build();
    interceptor.doBefore(methodInfo, context);
    requestContext = context.get(interceptor.getProgressKey());
    agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, methodInfo, requestContext);
    ClientResponse clientResponse = MockDefaultClientResponse.builder(500).build();
    agentCoreSubscriber.onNext(clientResponse);
    agentCoreSubscriber.onError(new RuntimeException(errorInfo));
    assertFalse(methodInfo.isSuccess());
    mockSpan = MockEaseAgent.getLastSpan();
    assertTrue(mockSpan.hasError());
    assertEquals(errorInfo, mockSpan.errorInfo());
    assertEquals("500", mockSpan.tag("http.status_code"));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) MockDefaultClientResponse(org.springframework.web.reactive.function.client.MockDefaultClientResponse) URI(java.net.URI) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) WebClientFilterTracingInterceptor(com.megaease.easeagent.plugin.springweb.interceptor.tracing.WebClientFilterTracingInterceptor) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) MockClientRequest(org.springframework.web.reactive.function.client.MockClientRequest) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.Test)

Example 30 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan in project easeagent by megaease.

the class AgentCoreSubscriberTest method onComplete.

@Test
public void onComplete() throws URISyntaxException {
    MockCoreSubscriber mockCoreSubscriber = new MockCoreSubscriber();
    WebClientFilterTracingInterceptor interceptor = new WebClientFilterTracingInterceptor();
    Context context = EaseAgent.getContext();
    URI uri = new URI(url);
    ClientRequest clientRequest = MockClientRequest.build(uri);
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).retValue(new MockMono()).build();
    interceptor.doBefore(methodInfo, context);
    RequestContext requestContext = context.get(interceptor.getProgressKey());
    AgentCoreSubscriber agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, methodInfo, requestContext);
    agentCoreSubscriber.onComplete();
    assertTrue(mockCoreSubscriber.onComplete.get());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertFalse(mockSpan.hasError());
    MockEaseAgent.cleanLastSpan();
    methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).retValue(new MockMono()).build();
    interceptor.doBefore(methodInfo, context);
    requestContext = context.get(interceptor.getProgressKey());
    agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, methodInfo, requestContext);
    ClientResponse clientResponse = MockDefaultClientResponse.builder(203).build();
    agentCoreSubscriber.onNext(clientResponse);
    agentCoreSubscriber.onComplete();
    assertTrue(mockCoreSubscriber.onComplete.get());
    mockSpan = MockEaseAgent.getLastSpan();
    assertFalse(mockSpan.hasError());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) MockDefaultClientResponse(org.springframework.web.reactive.function.client.MockDefaultClientResponse) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) WebClientFilterTracingInterceptor(com.megaease.easeagent.plugin.springweb.interceptor.tracing.WebClientFilterTracingInterceptor) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) URI(java.net.URI) MockClientRequest(org.springframework.web.reactive.function.client.MockClientRequest) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.Test)

Aggregations

ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)54 Test (org.junit.Test)46 Context (com.megaease.easeagent.plugin.api.Context)38 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)36 Span (com.megaease.easeagent.plugin.api.trace.Span)28 RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)10 Scope (com.megaease.easeagent.plugin.api.trace.Scope)9 Message (org.springframework.amqp.core.Message)6 URI (java.net.URI)5 MockEaseAgent (com.megaease.easeagent.mock.plugin.api.MockEaseAgent)4 EaseAgentJunit4ClassRunner (com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner)4 EaseAgent (com.megaease.easeagent.plugin.bridge.EaseAgent)4 MockKafkaConsumer (com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer)4 ArrayList (java.util.ArrayList)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Assert (org.junit.Assert)4 RunWith (org.junit.runner.RunWith)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)3