Search in sources :

Example 86 with Context

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

the class CommonRedisTracingInterceptorTest method finishTracing.

@Test
public void finishTracing() {
    CommonRedisTracingInterceptor commonRedisTracingInterceptor = new MockCommonRedisTracingInterceptor();
    Context context = EaseAgent.getContext();
    commonRedisTracingInterceptor.finishTracing(null, context);
    Span span = context.nextSpan().start();
    String name = "test_redis_span";
    span.name(name);
    context.put(SPAN_KEY, span);
    commonRedisTracingInterceptor.finishTracing(null, context);
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    assertEquals(name, mockSpan.name());
    assertEquals(span.traceIdString(), mockSpan.traceId());
    assertNull(context.get(SPAN_KEY));
    span = context.nextSpan().start();
    span.name(name);
    String errorInfo = "test error";
    context.put(SPAN_KEY, span);
    commonRedisTracingInterceptor.finishTracing(new RuntimeException(errorInfo), context);
    mockSpan = MockEaseAgent.getLastSpan();
    assertEquals(name, mockSpan.name());
    assertEquals(span.traceIdString(), mockSpan.traceId());
    assertNull(context.get(SPAN_KEY));
    assertEquals(errorInfo, mockSpan.tag("error"));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 87 with Context

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

the class CommonRedisTracingInterceptorTest method doBefore.

@Test
public void doBefore() {
    MockCommonRedisTracingInterceptor commonRedisTracingInterceptor = new MockCommonRedisTracingInterceptor();
    Context context = EaseAgent.getContext();
    commonRedisTracingInterceptor.doBefore(null, context);
    assertFalse(commonRedisTracingInterceptor.ran.get());
    Span span = context.nextSpan().start();
    try (Scope ignored = span.maybeScope()) {
        commonRedisTracingInterceptor.doBefore(null, context);
        assertTrue(commonRedisTracingInterceptor.ran.get());
    } finally {
        span.finish();
    }
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Scope(com.megaease.easeagent.plugin.api.trace.Scope) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 88 with Context

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

the class HttpHeadersFilterTracingInterceptor method doAfter.

@Override
public void doAfter(MethodInfo methodInfo, Context context) {
    ServerWebExchange exchange = (ServerWebExchange) methodInfo.getArgs()[1];
    HttpHeaders retHttpHeaders = (HttpHeaders) methodInfo.getRetValue();
    RequestContext pCtx = exchange.getAttribute(GatewayCons.SPAN_KEY);
    if (pCtx == null) {
        return;
    }
    FluxHttpServerRequest request = new HeaderFilterRequest(exchange.getRequest());
    RequestContext pnCtx = context.clientRequest(request);
    try (Scope ignored = pnCtx.scope()) {
        pnCtx.span().start();
        exchange.getAttributes().put(GatewayCons.CHILD_SPAN_KEY, pnCtx);
        Map<String, String> map = getHeadersFromExchange(exchange);
        map.putAll(retHttpHeaders.toSingleValueMap());
        map.putAll(pnCtx.getHeaders());
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll(map);
        methodInfo.setRetValue(httpHeaders);
        BiConsumer<ServerWebExchange, MethodInfo> consumer = (serverWebExchange, info) -> {
            RequestContext p = serverWebExchange.getAttribute(GatewayCons.CHILD_SPAN_KEY);
            if (p == null) {
                return;
            }
            FluxHttpServerResponse response = new FluxHttpServerResponse(serverWebExchange, info.getThrowable());
            HttpUtils.save(p.span(), response);
            p.finish(response);
        };
        exchange.getAttributes().put(GatewayCons.CLIENT_RECEIVE_CALLBACK_KEY, consumer);
    }
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) Span(com.megaease.easeagent.plugin.api.trace.Span) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Scope(com.megaease.easeagent.plugin.api.trace.Scope) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Consumer(java.util.function.Consumer) NonReentrantInterceptor(com.megaease.easeagent.plugin.interceptor.NonReentrantInterceptor) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) HttpUtils(com.megaease.easeagent.plugin.tools.trace.HttpUtils) GatewayCons(easeagent.plugin.spring.gateway.interceptor.GatewayCons) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) AdviceTo(com.megaease.easeagent.plugin.annotation.AdviceTo) SpringGatewayPlugin(easeagent.plugin.spring.gateway.SpringGatewayPlugin) HttpHeadersFilterAdvice(easeagent.plugin.spring.gateway.advice.HttpHeadersFilterAdvice) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) Scope(com.megaease.easeagent.plugin.api.trace.Scope) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext)

Example 89 with Context

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

the class GatewayServerTracingInterceptorTest method before.

@Test
public void before() {
    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);
    assertNotNull(context.get(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY));
    assertNotNull(context.get(FluxHttpServerRequest.class));
    RequestContext requestContext = context.remove(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY);
    assertTrue(context.currentTracing().hasCurrentSpan());
    requestContext.scope().close();
    assertFalse(context.currentTracing().hasCurrentSpan());
    context.remove(FluxHttpServerRequest.class);
    MockServerHttpRequest.BaseBuilder<?> baseBuilder = TestServerWebExchangeUtils.builder();
    for (Map.Entry<String, String> entry : requestContext.getHeaders().entrySet()) {
        baseBuilder.header(entry.getKey(), entry.getValue());
    }
    mockServerWebExchange = TestServerWebExchangeUtils.build(baseBuilder);
    methodInfo = MethodInfo.builder().args(new Object[] { mockServerWebExchange }).build();
    interceptor.before(methodInfo, context);
    RequestContext requestContext2 = context.remove(GatewayServerTracingInterceptor.SPAN_CONTEXT_KEY);
    assertTrue(context.currentTracing().hasCurrentSpan());
    requestContext2.scope().close();
    assertFalse(context.currentTracing().hasCurrentSpan());
    context.remove(FluxHttpServerRequest.class);
    requestContext2.span().finish();
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(requestContext.span(), mockSpan);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) Map(java.util.Map) Test(org.junit.Test)

Example 90 with Context

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

the class ClientHttpRequestInterceptorTest method testRestTemplate.

@Test
public void testRestTemplate() throws URISyntaxException, IOException {
    String url = "http://127.0.0.1:8080/test";
    ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = requestFactory.createRequest(new URI(url), HttpMethod.GET);
    ClientHttpResponse clientHttpResponse = SimpleClientHttpResponseFactory.createMockResponse(url);
    MethodInfo.MethodInfoBuilder methodInfoBuilder = MethodInfo.builder();
    MethodInfo methodInfo = methodInfoBuilder.invoker(request).build();
    Context context = EaseAgent.getContext();
    ClientHttpRequestInterceptor clientHttpRequestInterceptor = new ClientHttpRequestInterceptor();
    MockEaseAgent.cleanLastSpan();
    clientHttpRequestInterceptor.before(methodInfo, context);
    methodInfo = methodInfoBuilder.retValue(clientHttpResponse).build();
    clientHttpRequestInterceptor.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());
    request = requestFactory.createRequest(new URI(url), HttpMethod.GET);
    methodInfo = methodInfoBuilder.invoker(request).build();
    clientHttpRequestInterceptor.before(methodInfo, context);
    methodInfo = methodInfoBuilder.retValue(clientHttpResponse).build();
    RuntimeException runtimeException = new RuntimeException("test error");
    methodInfo.throwable(runtimeException);
    clientHttpRequestInterceptor.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());
    request = requestFactory.createRequest(new URI(url), HttpMethod.GET);
    methodInfo = methodInfoBuilder.invoker(request).build();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        clientHttpRequestInterceptor.before(methodInfo, context);
        methodInfo = methodInfoBuilder.retValue(clientHttpResponse).build();
        clientHttpRequestInterceptor.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) URI(java.net.URI) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) 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