Search in sources :

Example 21 with ReportSpan

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

the class MockEaseAgentTest method setMockSpanReport.

@Test
public void setMockSpanReport() {
    List<ReportSpan> spans = new ArrayList<>();
    MockEaseAgent.setMockSpanReport(spans::add);
    Span span1 = EaseAgent.getContext().nextSpan();
    span1.start().finish();
    Span span2 = EaseAgent.getContext().nextSpan();
    span2.start().finish();
    assertEquals(2, spans.size());
    SpanTestUtils.sameId(span1, spans.get(0));
    SpanTestUtils.sameId(span2, spans.get(1));
}
Also used : ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) ArrayList(java.util.ArrayList) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 22 with ReportSpan

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

the class WebClientFilterTracingInterceptorTest method doBefore.

@Test
public void doBefore() throws URISyntaxException {
    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 }).build();
    interceptor.doBefore(methodInfo, context);
    RequestContext requestContext = context.get(interceptor.getProgressKey());
    assertNotNull(requestContext);
    requestContext.scope().close();
    requestContext.span().finish();
    assertNull(requestContext.span().parentIdString());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(requestContext.span(), mockSpan);
    ClientRequest request = (ClientRequest) methodInfo.getArgs()[0];
    Collection<String> headers = request.headers().toSingleValueMap().values();
    assertTrue(headers.contains(mockSpan.traceId()));
    assertTrue(headers.contains(mockSpan.id()));
    Span span = context.nextSpan().start();
    try (Scope ignored = span.maybeScope()) {
        clientRequest = MockClientRequest.build(uri);
        methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).build();
        interceptor.doBefore(methodInfo, context);
        requestContext = context.get(interceptor.getProgressKey());
        requestContext.scope().close();
        requestContext.span().finish();
        mockSpan = MockEaseAgent.getLastSpan();
        assertEquals(span.traceIdString(), mockSpan.traceId());
        assertEquals(span.spanIdString(), mockSpan.parentId());
    }
    span.finish();
}
Also used : RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) URI(java.net.URI) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientRequest(org.springframework.web.reactive.function.client.MockClientRequest) Test(org.junit.Test)

Example 23 with ReportSpan

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

the class FeignClientTracingInterceptorTest method testFeignTracing.

@Test
public void testFeignTracing() {
    Request request = RequestUtils.buildFeignClient();
    MethodInfo.MethodInfoBuilder methodInfoBuilder = MethodInfo.builder();
    MethodInfo methodInfo = methodInfoBuilder.args(new Object[] { request }).build();
    Context context = EaseAgent.getContext();
    FeignClientTracingInterceptor feignClientTracingInterceptor = new FeignClientTracingInterceptor();
    MockEaseAgent.cleanLastSpan();
    feignClientTracingInterceptor.before(methodInfo, context);
    methodInfo = methodInfoBuilder.retValue(RequestUtils.responseBuilder(request).build()).build();
    feignClientTracingInterceptor.after(methodInfo, context);
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME));
    assertNull(mockSpan.parentId());
    request = RequestUtils.buildFeignClient();
    methodInfo = methodInfoBuilder.args(new Object[] { request }).build();
    feignClientTracingInterceptor.before(methodInfo, context);
    methodInfo.retValue(RequestUtils.responseBuilder(request).build());
    RuntimeException runtimeException = new RuntimeException("test error");
    methodInfo.throwable(runtimeException);
    feignClientTracingInterceptor.after(methodInfo, context);
    mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME));
    assertNull(mockSpan.parentId());
    request = RequestUtils.buildFeignClient();
    methodInfo = methodInfoBuilder.args(new Object[] { request }).build();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        feignClientTracingInterceptor.before(methodInfo, context);
        methodInfo.retValue(RequestUtils.responseBuilder(request).build());
        feignClientTracingInterceptor.after(methodInfo, context);
        mockSpan = MockEaseAgent.getLastSpan();
        assertEquals(span.traceIdString(), mockSpan.traceId());
        assertEquals(span.spanIdString(), mockSpan.parentId());
        assertNotNull(mockSpan.id());
    }
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) HttpRequest(com.megaease.easeagent.plugin.tools.trace.HttpRequest) Request(feign.Request) 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 24 with ReportSpan

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

the class GatewayServerTracingInterceptorTest method after.

@Test
public void after() {
    GatewayServerTracingInterceptor interceptor = new GatewayServerTracingInterceptor();
    Context context = EaseAgent.getContext();
    MockServerWebExchange mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.before(methodInfo, context);
    String errorInfo = "test error";
    methodInfo.throwable(new RuntimeException(errorInfo));
    interceptor.after(methodInfo, context);
    ReportSpan reportSpan = MockEaseAgent.getLastSpan();
    assertNotNull(reportSpan);
    assertTrue(reportSpan.hasError());
    assertEquals(errorInfo, reportSpan.errorInfo());
    assertNull(context.get(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY));
    assertNull(context.get(FluxHttpServerRequest.class));
    assertNull(methodInfo.getRetValue());
    mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.before(methodInfo, context);
    interceptor.after(methodInfo, context);
    assertNull(context.get(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY));
    assertNull(context.get(FluxHttpServerRequest.class));
    assertNotNull(methodInfo.getRetValue());
    assertTrue(methodInfo.getRetValue() instanceof AgentMono);
    assertFalse(context.currentTracing().hasCurrentSpan());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) AgentMono(easeagent.plugin.spring.gateway.reactor.AgentMono) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 25 with ReportSpan

use of com.megaease.easeagent.plugin.report.tracing.ReportSpan 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());
}
Also used : ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) TopicPartition(org.apache.kafka.common.TopicPartition) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockKafkaConsumer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaConsumer) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) 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