Search in sources :

Example 71 with Context

use of com.megaease.easeagent.plugin.api.Context 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)

Example 72 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class InterceptorTest method testBeforeAfter.

@Test
public void testBeforeAfter() {
    assertNull(MockEaseAgent.getLastSpan());
    final Object key = new Object();
    Interceptor interceptor = new Interceptor() {

        @Override
        public void before(MethodInfo methodInfo, Context context) {
            Span span = context.nextSpan();
            span.start();
            context.put(key, span);
        }

        @Override
        public void after(MethodInfo methodInfo, Context context) {
            Span span = context.remove(key);
            span.finish();
        }
    };
    Context context = EaseAgent.getContext();
    interceptor.before(null, context);
    Span span = context.get(key);
    assertNotNull(span);
    assertNull(MockEaseAgent.getLastSpan());
    interceptor.after(null, context);
    ReportSpan reportSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, reportSpan);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Interceptor(com.megaease.easeagent.plugin.interceptor.Interceptor) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 73 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class MockEaseAgentTest method resetAll.

@Test
public void resetAll() {
    Context context = EaseAgent.getContext();
    String key = "testKey";
    context.put(key, "value");
    assertNotNull(context.get(key));
    MockEaseAgent.resetAll();
    assertNull(context.get(key));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Test(org.junit.Test)

Example 74 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class RabbitMqChannelPublishTracingInterceptorTest method before.

@Test
public void before() {
    RabbitMqChannelPublishTracingInterceptor interceptor = new RabbitMqChannelPublishTracingInterceptor();
    Context context = EaseAgent.getContext();
    String uri = "192.168.0.13:2222";
    context.put(ContextCons.MQ_URI, uri);
    String exchange = "testExchange";
    String routingKey = "testRoutingKey";
    AMQP.BasicProperties basicProperties = new AMQP.BasicProperties();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { exchange, routingKey, null, null, basicProperties }).build();
    interceptor.before(methodInfo, context);
    interceptor.after(methodInfo, context);
    ReportSpan reportSpan = MockEaseAgent.getLastSpan();
    assertNotNull(reportSpan);
    assertEquals(Span.Kind.PRODUCER.name(), reportSpan.kind());
    assertEquals(exchange, reportSpan.tag("rabbit.exchange"));
    assertEquals(routingKey, reportSpan.tag("rabbit.routing_key"));
    assertEquals(uri, reportSpan.tag("rabbit.broker"));
    assertEquals("rabbitmq", reportSpan.remoteServiceName());
    assertEquals(Type.RABBITMQ.getRemoteType(), reportSpan.tag(MiddlewareConstants.TYPE_TAG_NAME));
    assertNull(reportSpan.tag(MiddlewareConstants.REDIRECTED_LABEL_REMOTE_TAG_NAME));
    assertFalse(reportSpan.hasError());
    String errorInfo = "testError";
    methodInfo = MethodInfo.builder().args(new Object[] { exchange, routingKey, null, null, basicProperties }).throwable(new RuntimeException(errorInfo)).build();
    interceptor.before(methodInfo, context);
    interceptor.after(methodInfo, context);
    reportSpan = MockEaseAgent.getLastSpan();
    assertTrue(reportSpan.hasError());
    assertEquals(errorInfo, reportSpan.errorInfo());
    Span span = context.nextSpan();
    span.cacheScope();
    methodInfo = MethodInfo.builder().args(new Object[] { exchange, routingKey, null, null, basicProperties }).build();
    interceptor.before(methodInfo, context);
    interceptor.after(methodInfo, context);
    reportSpan = MockEaseAgent.getLastSpan();
    assertEquals(span.traceIdString(), reportSpan.traceId());
    assertEquals(span.spanIdString(), reportSpan.parentId());
    span.finish();
    TestUtils.setRedirect();
    RedirectProcessor.redirected(Redirect.RABBITMQ, TestUtils.getRedirectUri());
    methodInfo = MethodInfo.builder().args(new Object[] { exchange, routingKey, null, null, basicProperties }).build();
    interceptor.before(methodInfo, context);
    interceptor.after(methodInfo, context);
    reportSpan = MockEaseAgent.getLastSpan();
    assertEquals(TestUtils.getRedirectUri(), reportSpan.tag(MiddlewareConstants.REDIRECTED_LABEL_REMOTE_TAG_NAME));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) AMQP(com.rabbitmq.client.AMQP) 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 75 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class RabbitMqMessageListenerOnMessageInterceptorTest method before.

@Test
public void before() {
    RabbitMqMessageListenerOnMessageInterceptor interceptor = new RabbitMqMessageListenerOnMessageInterceptor();
    MessageProperties messageProperties = new MessageProperties();
    String testMqUri = "testMqUri";
    messageProperties.setHeader(ContextCons.MQ_URI, testMqUri);
    Message message = new Message("testBody".getBytes(), messageProperties);
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { message }).build();
    Context context = EaseAgent.getContext();
    interceptor.before(methodInfo, context);
    assertEquals(testMqUri, context.get(ContextCons.MQ_URI));
    context.remove(ContextCons.MQ_URI);
    methodInfo = MethodInfo.builder().args(new Object[] { Collections.singletonList(message) }).build();
    interceptor.before(methodInfo, context);
    assertEquals(testMqUri, context.get(ContextCons.MQ_URI));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Aggregations

Context (com.megaease.easeagent.plugin.api.Context)122 Test (org.junit.Test)101 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)87 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)39 Span (com.megaease.easeagent.plugin.api.trace.Span)32 RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)13 Scope (com.megaease.easeagent.plugin.api.trace.Scope)13 Message (org.springframework.amqp.core.Message)10 URI (java.net.URI)9 LastJsonReporter (com.megaease.easeagent.mock.report.impl.LastJsonReporter)8 TagVerifier (com.megaease.easeagent.mock.plugin.api.utils.TagVerifier)7 BsonDocument (org.bson.BsonDocument)7 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)7 HttpRequest (com.megaease.easeagent.plugin.tools.trace.HttpRequest)6 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)6 ArrayList (java.util.ArrayList)6 BsonString (org.bson.BsonString)6 Call (okhttp3.Call)5 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)4 MockClientRequest (org.springframework.web.reactive.function.client.MockClientRequest)4