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"));
}
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();
}
}
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);
}
}
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);
}
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());
}
}
Aggregations