Search in sources :

Example 51 with ReportSpan

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

the class HttpClientDoExecuteInterceptorTest method before.

@Test
public void before() {
    Context context = EaseAgent.getContext();
    HttpGet httpGet = new HttpGet("http://127.0.0.1:8080");
    ProtocolVersion protocolVersion = new ProtocolVersion("testProtocol", 10, 1);
    BasicHttpResponse basicHttpResponse = new BasicHttpResponse(protocolVersion, 200, "");
    basicHttpResponse.setHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE);
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { httpGet }).retValue(basicHttpResponse).build();
    HttpClientDoExecuteInterceptor httpClientDoExecuteInterceptor = new HttpClientDoExecuteInterceptor();
    MockEaseAgent.cleanLastSpan();
    httpClientDoExecuteInterceptor.before(methodInfo, context);
    httpClientDoExecuteInterceptor.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());
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        httpClientDoExecuteInterceptor.doBefore(methodInfo, context);
        httpClientDoExecuteInterceptor.doAfter(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) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) Scope(com.megaease.easeagent.plugin.api.trace.Scope) HttpGet(org.apache.http.client.methods.HttpGet) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) ProtocolVersion(org.apache.http.ProtocolVersion) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 52 with ReportSpan

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

the class RunnableInterceptorTest method before.

@Test
public void before() throws InterruptedException {
    Context context = EaseAgent.getContext();
    final Span span = context.nextSpan();
    span.start();
    span.cacheScope();
    RunnableInterceptor runnableInterceptor = new RunnableInterceptor();
    AtomicInteger run = new AtomicInteger();
    Runnable runnable = () -> {
        Context runCont = EaseAgent.getContext();
        assertTrue(runCont.currentTracing().hasCurrentSpan());
        Span span1 = runCont.nextSpan();
        assertEquals(span.traceId(), span1.traceId());
        assertEquals(span.spanId(), span1.parentId());
        assertNotEquals(span.spanId(), span1.spanId());
        run.incrementAndGet();
    };
    MethodInfo methodInfo = MethodInfo.builder().invoker("").type("").method("").args(new Object[] { runnable }).build();
    runnableInterceptor.before(methodInfo, context);
    Thread thread = new Thread((Runnable) methodInfo.getArgs()[0]);
    thread.start();
    thread.join();
    assertEquals(run.get(), 1);
    span.finish();
    ReportSpan span1 = MockEaseAgent.getLastSpan();
    assertEquals(span.traceIdString(), span1.traceId());
    assertEquals(span.parentIdString(), span1.parentId());
    assertEquals(span.spanIdString(), span1.id());
    System.out.println("run count: " + run.get());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 53 with ReportSpan

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

the class ElasticsearchBaseTest method assertTrace.

protected void assertTrace(boolean success, String error) {
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals("index-1", mockSpan.tag("es.index"));
    assertEquals("GET /index-1/_search", mockSpan.tag("es.operation"));
    assertEquals(body, mockSpan.tag("es.body"));
    if (success) {
        assertNull(mockSpan.tag("error"));
    }
    if (!success) {
        assertNotNull(mockSpan.tag("error"));
        assertEquals(error, mockSpan.tag("error"));
    }
    assertNull(mockSpan.parentId());
}
Also used : ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan)

Example 54 with ReportSpan

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

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