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));
}
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();
}
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());
}
}
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());
}
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());
}
Aggregations