Search in sources :

Example 1 with ReportSpan

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

the class MongoBaseTest method assertTrace.

protected void assertTrace(boolean success, String error) {
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals("mongodb-" + this.dbName, mockSpan.remoteServiceName());
    assertEquals(this.cmdName, mockSpan.tag("mongodb.command"));
    assertEquals("mongodb", mockSpan.tag("component.type"));
    assertEquals(this.collection, mockSpan.tag("mongodb.collection"));
    assertEquals(this.clusterId.getValue(), mockSpan.tag("mongodb.cluster_id"));
    assertEquals(this.serverAddress.getHost(), mockSpan.remoteEndpoint().ipv4());
    assertEquals(this.serverAddress.getPort(), mockSpan.remoteEndpoint().port());
    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 2 with ReportSpan

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

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

the class KafkaProducerDoSendInterceptorTest method doBefore.

@Test
public void doBefore() {
    KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
    MockKafkaProducer kafkaProducer = MockKafkaProducer.buildOne();
    Context context = EaseAgent.getContext();
    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());
    Span span = finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    checkBaseInfo(mockSpan);
    assertEquals(key, mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
    record = new ProducerRecord<>(topic, value);
    methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
    context = EaseAgent.getContext();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    span = finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    checkBaseInfo(mockSpan);
    assertNull(mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
    record = new ProducerRecord<>(topic, value);
    methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
    context = EaseAgent.getContext();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    assertNotNull(methodInfo.getArgs()[1]);
    assertTrue(methodInfo.getArgs()[1] instanceof TraceCallback);
}
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) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 4 with ReportSpan

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

the class OkHttpTracingInterceptorTest method doBefore.

@Test
public void doBefore() {
    Call call = OkHttpTestUtils.buildCall();
    MethodInfo.MethodInfoBuilder methodInfoBuilder = MethodInfo.builder().invoker(call);
    MethodInfo methodInfo = methodInfoBuilder.build();
    Context context = EaseAgent.getContext();
    OkHttpTracingInterceptor okHttpTracingInterceptor = new OkHttpTracingInterceptor();
    MockEaseAgent.cleanLastSpan();
    okHttpTracingInterceptor.before(methodInfo, context);
    methodInfo = methodInfoBuilder.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build()).build();
    okHttpTracingInterceptor.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());
    call = OkHttpTestUtils.buildCall();
    methodInfo = methodInfoBuilder.invoker(call).retValue(null).build();
    okHttpTracingInterceptor.before(methodInfo, context);
    methodInfo.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build());
    RuntimeException runtimeException = new RuntimeException("test error");
    methodInfo.throwable(runtimeException);
    okHttpTracingInterceptor.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());
    call = OkHttpTestUtils.buildCall();
    methodInfo = methodInfoBuilder.invoker(call).retValue(null).build();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        okHttpTracingInterceptor.before(methodInfo, context);
        methodInfo.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build());
        okHttpTracingInterceptor.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) Call(okhttp3.Call) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) 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 5 with ReportSpan

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

the class OkHttpAsyncTracingInterceptorTest method doBefore.

@Test
public void doBefore() throws InterruptedException {
    ReportSpan mockSpan = runOne((call, callback) -> {
        Response response = OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build();
        try {
            callback.onResponse(call, response);
        } catch (IOException e) {
            throw new RuntimeException("onResponse fail.", e);
        }
    });
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME));
    assertNull(mockSpan.parentId());
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        mockSpan = runOne((call, callback) -> {
            Response response = OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build();
            try {
                callback.onResponse(call, response);
            } catch (IOException e) {
                throw new RuntimeException("onResponse fail.", e);
            }
        });
        assertNotNull(mockSpan);
        assertEquals(span.traceIdString(), mockSpan.traceId());
        assertEquals(span.spanIdString(), mockSpan.parentId());
        assertNotNull(mockSpan.id());
    }
    mockSpan = runOne((call, callback) -> {
        callback.onFailure(call, new IOException("test error"));
    });
    assertNull(mockSpan);
}
Also used : Response(okhttp3.Response) Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Context(com.megaease.easeagent.plugin.api.Context) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) IOException(java.io.IOException) Span(com.megaease.easeagent.plugin.api.trace.Span) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockEaseAgent(com.megaease.easeagent.mock.plugin.api.MockEaseAgent) Scope(com.megaease.easeagent.plugin.api.trace.Scope) EaseAgent(com.megaease.easeagent.plugin.bridge.EaseAgent) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) EaseAgentJunit4ClassRunner(com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) IOException(java.io.IOException) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) 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