Search in sources :

Example 91 with Context

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

the class WebClientFilterTracingInterceptorTest method doAfter.

@Test
public void doAfter() 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 }).retValue(new MockMono()).build();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    MockEaseAgent.cleanLastSpan();
    interceptor.doAfter(methodInfo, context);
    assertFalse(context.currentTracing().hasCurrentSpan());
    assertNull(MockEaseAgent.getLastSpan());
    assertTrue(methodInfo.getRetValue() instanceof AgentMono);
    String errorInfo = "test error";
    clientRequest = MockClientRequest.build(uri);
    methodInfo = MethodInfo.builder().args(new Object[] { clientRequest }).retValue(new MockMono()).throwable(new RuntimeException(errorInfo)).build();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    interceptor.doAfter(methodInfo, context);
    assertFalse(context.currentTracing().hasCurrentSpan());
    assertTrue(methodInfo.getRetValue() instanceof AgentMono);
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertTrue(mockSpan.hasError());
    assertEquals(errorInfo, mockSpan.errorInfo());
}
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) AgentMono(com.megaease.easeagent.plugin.springweb.reactor.AgentMono) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockMono(com.megaease.easeagent.plugin.springweb.reactor.MockMono) URI(java.net.URI) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientRequest(org.springframework.web.reactive.function.client.MockClientRequest) Test(org.junit.Test)

Example 92 with Context

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

the class GatewayMetricsInterceptorTest method before.

@Test
public void before() {
    GatewayMetricsInterceptor interceptor = new GatewayMetricsInterceptor();
    Context context = EaseAgent.getContext();
    interceptor.before(null, context);
    assertNotNull(context.get(startTime));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Test(org.junit.Test)

Example 93 with Context

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

the class GatewayServerTracingInterceptorTest method finishCallback.

@Test
public void finishCallback() throws InterruptedException {
    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);
    RequestContext requestContext = context.get(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY);
    interceptor.after(methodInfo, context);
    assertNotNull(methodInfo.getRetValue());
    assertTrue(methodInfo.getRetValue() instanceof AgentMono);
    assertNull(MockEaseAgent.getLastSpan());
    AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    BiConsumer<ServerWebExchange, MethodInfo> consumer = (methodInfo1, exchange) -> atomicBoolean.set(true);
    mockServerWebExchange.getAttributes().put(GatewayCons.CLIENT_RECEIVE_CALLBACK_KEY, consumer);
    AgentMono agentMono = (AgentMono) methodInfo.getRetValue();
    Thread thread = new Thread(() -> agentMono.getFinish().accept(agentMono.getMethodInfo(), agentMono.getAsyncContext()));
    thread.start();
    thread.join();
    assertTrue(atomicBoolean.get());
    ReportSpan reportSpan = MockEaseAgent.getLastSpan();
    assertTrue(reportSpan.name().contains("/test"));
    SpanTestUtils.sameId(requestContext.span(), reportSpan);
}
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) Context(com.megaease.easeagent.plugin.api.Context) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) TestServerWebExchangeUtils(easeagent.plugin.spring.gateway.TestServerWebExchangeUtils) RunWith(org.junit.runner.RunWith) SpanTestUtils(com.megaease.easeagent.mock.plugin.api.utils.SpanTestUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Order(com.megaease.easeagent.plugin.enums.Order) Test(org.junit.Test) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockEaseAgent(com.megaease.easeagent.mock.plugin.api.MockEaseAgent) EaseAgent(com.megaease.easeagent.plugin.bridge.EaseAgent) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) GatewayCons(easeagent.plugin.spring.gateway.interceptor.GatewayCons) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) EaseAgentJunit4ClassRunner(com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner) ConfigConst(com.megaease.easeagent.plugin.api.config.ConfigConst) Assert(org.junit.Assert) AgentMono(easeagent.plugin.spring.gateway.reactor.AgentMono) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) AgentMono(easeagent.plugin.spring.gateway.reactor.AgentMono) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Test(org.junit.Test)

Example 94 with Context

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

the class GatewayServerForwardedInterceptorTest method doBefore.

@Test
public void doBefore() {
    GatewayServerForwardedInterceptor interceptor = new GatewayServerForwardedInterceptor();
    MockServerWebExchange mockServerWebExchange = TestServerWebExchangeUtils.build(builder().header(TestConst.FORWARDED_NAME, TestConst.FORWARDED_VALUE));
    Context context = EaseAgent.getContext();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.doBefore(methodInfo, context);
    assertEquals(TestConst.FORWARDED_VALUE, context.get(TestConst.FORWARDED_NAME));
    interceptor.doAfter(methodInfo, context);
    assertNull(context.get(TestConst.FORWARDED_NAME));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 95 with Context

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

the class DoFilterTraceInterceptorTest method testHasPassHeader.

@Test
public void testHasPassHeader() {
    MockHttpServletRequest httpServletRequest = TestServletUtils.buildMockRequest();
    Context context = EaseAgent.getContext();
    RequestContext requestContext = context.clientRequest(new MockClientRequest(httpServletRequest::addHeader));
    requestContext.scope().close();
    assertFalse(context.currentTracing().hasCurrentSpan());
    HttpServletResponse response = TestServletUtils.buildMockResponse();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { httpServletRequest, response }).build();
    DoFilterTraceInterceptor doFilterTraceInterceptor = new DoFilterTraceInterceptor();
    doFilterTraceInterceptor.doBefore(methodInfo, EaseAgent.getContext());
    doFilterTraceInterceptor.doAfter(methodInfo, EaseAgent.getContext());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertNotNull(mockSpan);
    assertEquals(requestContext.span().traceIdString(), mockSpan.traceId());
    assertEquals(requestContext.span().spanIdString(), mockSpan.id());
    assertEquals(requestContext.span().parentIdString(), mockSpan.parentId());
    checkServerSpan(mockSpan);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) MockAsyncContext(org.springframework.mock.web.MockAsyncContext) AsyncContext(javax.servlet.AsyncContext) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) 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