Search in sources :

Example 1 with RequestContext

use of com.megaease.easeagent.plugin.api.context.RequestContext in project easeagent by megaease.

the class TracingImplTest method clientRequest.

@Test
public void clientRequest() {
    RequestMock requestMock = new RequestMock().setKind(Span.Kind.CLIENT).setName(name);
    assertFalse(iTracing.hasCurrentSpan());
    RequestContext requestContext = iTracing.clientRequest(requestMock);
    assertTrue(iTracing.hasCurrentSpan());
    MutableSpan mutableSpan = TracingProviderImplMock.getMutableSpan(requestContext.span());
    assertEquals(brave.Span.Kind.CLIENT, mutableSpan.kind());
    assertEquals(name, mutableSpan.name());
    try (Scope scope = requestContext.scope()) {
        assertNull(requestContext.span().parentIdString());
        RequestContext requestContext2 = iTracing.clientRequest(requestMock);
        assertEquals(requestContext.span().traceIdString(), requestContext2.span().traceIdString());
        assertEquals(requestContext.span().spanIdString(), requestContext2.span().parentIdString());
        requestContext2.scope().close();
    }
    assertFalse(iTracing.hasCurrentSpan());
}
Also used : MutableSpan(brave.handler.MutableSpan) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Test(org.junit.Test)

Example 2 with RequestContext

use of com.megaease.easeagent.plugin.api.context.RequestContext 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();
}
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) Scope(com.megaease.easeagent.plugin.api.trace.Scope) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) URI(java.net.URI) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientRequest(org.springframework.web.reactive.function.client.MockClientRequest) Test(org.junit.Test)

Example 3 with RequestContext

use of com.megaease.easeagent.plugin.api.context.RequestContext in project easeagent by megaease.

the class GatewayAccessLogInterceptorTest method before.

@Test
public void before() {
    GatewayAccessLogInterceptor interceptor = new GatewayAccessLogInterceptor();
    Context context = EaseAgent.getContext();
    MockServerWebExchange mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.before(methodInfo, context);
    RequestInfo requestInfo = (RequestInfo) mockServerWebExchange.getAttributes().get(RequestInfo.class.getName());
    assertNotNull(requestInfo);
    verify(requestInfo, TimeUtils.startTime(context, startTime));
    assertNull(requestInfo.getTraceId());
    assertNull(requestInfo.getSpanId());
    assertNull(requestInfo.getParentSpanId());
    mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.before(methodInfo, context);
    RequestContext requestContext = GatewayServerTracingInterceptorTest.beforeGatewayServerTracing(mockServerWebExchange);
    Span span = requestContext.span();
    try (Scope ignored = requestContext.scope()) {
        interceptor.before(methodInfo, context);
        requestInfo = (RequestInfo) mockServerWebExchange.getAttributes().get(RequestInfo.class.getName());
        assertNotNull(requestInfo);
        verify(requestInfo, TimeUtils.startTime(context, startTime));
        assertEquals(span.traceIdString(), requestInfo.getTraceId());
        assertEquals(span.spanIdString(), requestInfo.getSpanId());
        assertEquals(span.parentIdString(), requestInfo.getParentSpanId());
    }
}
Also used : RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Context(com.megaease.easeagent.plugin.api.Context) Scope(com.megaease.easeagent.plugin.api.trace.Scope) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) RequestInfo(com.megaease.easeagent.plugin.tools.metrics.RequestInfo) Span(com.megaease.easeagent.plugin.api.trace.Span) GatewayServerTracingInterceptorTest(easeagent.plugin.spring.gateway.interceptor.tracing.GatewayServerTracingInterceptorTest) Test(org.junit.Test)

Example 4 with RequestContext

use of com.megaease.easeagent.plugin.api.context.RequestContext in project easeagent by megaease.

the class HttpHeadersFilterTracingInterceptorTest method doAfter.

@Test
public void doAfter() {
    HttpHeadersFilterTracingInterceptor interceptor = new HttpHeadersFilterTracingInterceptor();
    MockServerWebExchange mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { null, mockServerWebExchange }).retValue(new HttpHeaders()).build();
    interceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(mockServerWebExchange.getAttribute(GatewayCons.CHILD_SPAN_KEY));
    mockServerWebExchange = TestServerWebExchangeUtils.mockServerWebExchange();
    methodInfo = MethodInfo.builder().args(new Object[] { null, mockServerWebExchange }).retValue(new HttpHeaders()).build();
    RequestContext requestContext = GatewayServerTracingInterceptorTest.beforeGatewayServerTracing(mockServerWebExchange);
    Span span = requestContext.span();
    try (Scope ignored = requestContext.scope()) {
        interceptor.doAfter(methodInfo, EaseAgent.getContext());
        RequestContext clientContext = mockServerWebExchange.getAttribute(GatewayCons.CHILD_SPAN_KEY);
        HttpHeaders ret = (HttpHeaders) methodInfo.getRetValue();
        Collection<String> headers = ret.toSingleValueMap().values();
        assertTrue(headers.contains(clientContext.span().traceIdString()));
        assertTrue(headers.contains(clientContext.span().spanIdString()));
        assertTrue(headers.contains(clientContext.span().parentIdString()));
        assertEquals(span.traceIdString(), clientContext.span().traceIdString());
        assertEquals(span.spanIdString(), clientContext.span().parentIdString());
        clientContext.scope().close();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Scope(com.megaease.easeagent.plugin.api.trace.Scope) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 5 with RequestContext

use of com.megaease.easeagent.plugin.api.context.RequestContext in project easeagent by megaease.

the class WebClientFilterTracingInterceptor method doBefore.

@Override
public void doBefore(MethodInfo methodInfo, Context context) {
    HttpRequest request = getRequest(methodInfo);
    RequestContext requestContext = context.clientRequest(request);
    Span span = requestContext.span();
    HttpUtils.handleReceive(span.start(), request);
    context.put(getProgressKey(), requestContext);
}
Also used : HttpRequest(com.megaease.easeagent.plugin.tools.trace.HttpRequest) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Span(com.megaease.easeagent.plugin.api.trace.Span)

Aggregations

RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)25 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)13 Context (com.megaease.easeagent.plugin.api.Context)10 Test (org.junit.Test)10 Span (com.megaease.easeagent.plugin.api.trace.Span)7 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)7 Scope (com.megaease.easeagent.plugin.api.trace.Scope)5 URI (java.net.URI)4 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)4 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)4 MockClientRequest (org.springframework.web.reactive.function.client.MockClientRequest)4 ServerWebExchange (org.springframework.web.server.ServerWebExchange)4 WebClientFilterTracingInterceptor (com.megaease.easeagent.plugin.springweb.interceptor.tracing.WebClientFilterTracingInterceptor)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ClientResponse (org.springframework.web.reactive.function.client.ClientResponse)3 MutableSpan (brave.handler.MutableSpan)2 HttpRequest (com.megaease.easeagent.plugin.tools.trace.HttpRequest)2 HttpResponse (com.megaease.easeagent.plugin.tools.trace.HttpResponse)2