Search in sources :

Example 1 with Scope

use of com.megaease.easeagent.plugin.api.trace.Scope in project easeagent by megaease.

the class AgentMDCScopeDecoratorTest method getV2.

@Test
public void getV2() {
    Span eSpan = SpanImpl.build(tracing, tracing.tracer().nextSpan(), injector);
    checkEmptySpanIds();
    Span eSpan2 = SpanImpl.build(tracing, tracing.tracer().nextSpan(), injector);
    checkEmptySpanIds();
    try (Scope scope = eSpan.maybeScope()) {
        checkSpanIds(eSpan);
        eSpan2 = SpanImpl.build(tracing, tracing.tracer().nextSpan(), injector);
        try (Scope s = eSpan2.maybeScope()) {
            checkSpanIds(eSpan2);
        }
        checkSpanIds(eSpan);
    }
    checkEmptySpanIds();
}
Also used : Scope(com.megaease.easeagent.plugin.api.trace.Scope) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 2 with Scope

use of com.megaease.easeagent.plugin.api.trace.Scope in project easeagent by megaease.

the class OkHttpTracingInterceptorTest method doBefore.

@Test
public void doBefore() {
    Call call = OkHttpTestUtils.buildCall();
    MethodInfo.MethodInfoBuilder methodInfoBuilder = MethodInfo.builder().invoker(call);
    MethodInfo methodInfo = methodInfoBuilder.build();
    Context context = EaseAgent.getContext();
    OkHttpTracingInterceptor okHttpTracingInterceptor = new OkHttpTracingInterceptor();
    MockEaseAgent.cleanLastSpan();
    okHttpTracingInterceptor.before(methodInfo, context);
    methodInfo = methodInfoBuilder.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build()).build();
    okHttpTracingInterceptor.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());
    call = OkHttpTestUtils.buildCall();
    methodInfo = methodInfoBuilder.invoker(call).retValue(null).build();
    okHttpTracingInterceptor.before(methodInfo, context);
    methodInfo.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build());
    RuntimeException runtimeException = new RuntimeException("test error");
    methodInfo.throwable(runtimeException);
    okHttpTracingInterceptor.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());
    call = OkHttpTestUtils.buildCall();
    methodInfo = methodInfoBuilder.invoker(call).retValue(null).build();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        okHttpTracingInterceptor.before(methodInfo, context);
        methodInfo.retValue(OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build());
        okHttpTracingInterceptor.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) Call(okhttp3.Call) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) 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 3 with Scope

use of com.megaease.easeagent.plugin.api.trace.Scope in project easeagent by megaease.

the class OkHttpAsyncTracingInterceptorTest method doBefore.

@Test
public void doBefore() throws InterruptedException {
    ReportSpan mockSpan = runOne((call, callback) -> {
        Response response = OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build();
        try {
            callback.onResponse(call, response);
        } catch (IOException e) {
            throw new RuntimeException("onResponse fail.", e);
        }
    });
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME));
    assertNull(mockSpan.parentId());
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan();
    try (Scope ignored = span.maybeScope()) {
        mockSpan = runOne((call, callback) -> {
            Response response = OkHttpTestUtils.responseBuilder(call).addHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE).build();
            try {
                callback.onResponse(call, response);
            } catch (IOException e) {
                throw new RuntimeException("onResponse fail.", e);
            }
        });
        assertNotNull(mockSpan);
        assertEquals(span.traceIdString(), mockSpan.traceId());
        assertEquals(span.spanIdString(), mockSpan.parentId());
        assertNotNull(mockSpan.id());
    }
    mockSpan = runOne((call, callback) -> {
        callback.onFailure(call, new IOException("test error"));
    });
    assertNull(mockSpan);
}
Also used : Response(okhttp3.Response) Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Context(com.megaease.easeagent.plugin.api.Context) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) IOException(java.io.IOException) Span(com.megaease.easeagent.plugin.api.trace.Span) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) MockEaseAgent(com.megaease.easeagent.mock.plugin.api.MockEaseAgent) Scope(com.megaease.easeagent.plugin.api.trace.Scope) EaseAgent(com.megaease.easeagent.plugin.bridge.EaseAgent) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) EaseAgentJunit4ClassRunner(com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Scope(com.megaease.easeagent.plugin.api.trace.Scope) IOException(java.io.IOException) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 4 with Scope

use of com.megaease.easeagent.plugin.api.trace.Scope in project easeagent by megaease.

the class HttpClient5AsyncTracingInterceptorTest method doBefore.

@Test
public void doBefore() throws InterruptedException {
    BasicHttpResponse basicHttpResponse = new BasicHttpResponse(200);
    basicHttpResponse.setHeader(TestConst.RESPONSE_TAG_NAME, TestConst.RESPONSE_TAG_VALUE);
    ReportSpan mockSpan = runOne(httpResponseFutureCallback -> {
        httpResponseFutureCallback.completed(basicHttpResponse);
    });
    assertNotNull(mockSpan);
    assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
    assertEquals(TestConst.RESPONSE_TAG_VALUE, mockSpan.tag(TestConst.RESPONSE_TAG_NAME));
    assertNull(mockSpan.parentId());
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan();
    try (Scope scope = span.maybeScope()) {
        mockSpan = runOne(httpResponseFutureCallback -> {
            httpResponseFutureCallback.completed(basicHttpResponse);
        });
        assertNotNull(mockSpan);
        assertEquals(span.traceIdString(), mockSpan.traceId());
        assertEquals(span.spanIdString(), mockSpan.parentId());
        assertNotNull(mockSpan.id());
    }
    mockSpan = runOne(httpResponseFutureCallback -> {
        httpResponseFutureCallback.failed(new RuntimeException("test error"));
    });
    assertNull(mockSpan);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Context(com.megaease.easeagent.plugin.api.Context) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Span(com.megaease.easeagent.plugin.api.trace.Span) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) MockEaseAgent(com.megaease.easeagent.mock.plugin.api.MockEaseAgent) Scope(com.megaease.easeagent.plugin.api.trace.Scope) EaseAgent(com.megaease.easeagent.plugin.bridge.EaseAgent) Consumer(java.util.function.Consumer) SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpResponse(org.apache.hc.core5.http.HttpResponse) EaseAgentJunit4ClassRunner(com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner) Assert(org.junit.Assert) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) 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 5 with Scope

use of com.megaease.easeagent.plugin.api.trace.Scope in project easeagent by megaease.

the class CommonRedisTracingInterceptorTest method startTracing.

@Test
public void startTracing() {
    CommonRedisTracingInterceptor commonRedisTracingInterceptor = new MockCommonRedisTracingInterceptor();
    Context context = EaseAgent.getContext();
    commonRedisTracingInterceptor.startTracing(context, name, null, null);
    Span span = context.get(SPAN_KEY);
    span.finish();
    checkMockSpanInfo(Objects.requireNonNull(MockEaseAgent.getLastSpan()));
    String cmd = "testCmd";
    commonRedisTracingInterceptor.startTracing(context, name, null, cmd);
    span = context.get(SPAN_KEY);
    span.finish();
    ReportSpan mockSpan = Objects.requireNonNull(MockEaseAgent.getLastSpan());
    checkMockSpanInfo(mockSpan);
    assertEquals(cmd, mockSpan.tag("redis.method"));
    Span pSpan = context.nextSpan().start();
    ReportSpan child;
    try (Scope ignored = pSpan.maybeScope()) {
        commonRedisTracingInterceptor.startTracing(context, name, null, null);
        span = context.get(SPAN_KEY);
        span.finish();
        child = Objects.requireNonNull(MockEaseAgent.getLastSpan());
    } finally {
        pSpan.finish();
    }
    ReportSpan parent = Objects.requireNonNull(MockEaseAgent.getLastSpan());
    assertEquals(parent.traceId(), child.traceId());
    assertEquals(parent.id(), child.parentId());
    assertNull(parent.parentId());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) 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)

Aggregations

Scope (com.megaease.easeagent.plugin.api.trace.Scope)22 Span (com.megaease.easeagent.plugin.api.trace.Span)22 Test (org.junit.Test)18 Context (com.megaease.easeagent.plugin.api.Context)13 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)13 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)11 RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)5 MutableSpan (brave.handler.MutableSpan)4 MockEaseAgent (com.megaease.easeagent.mock.plugin.api.MockEaseAgent)2 EaseAgentJunit4ClassRunner (com.megaease.easeagent.mock.plugin.api.junit.EaseAgentJunit4ClassRunner)2 EaseAgent (com.megaease.easeagent.plugin.bridge.EaseAgent)2 URI (java.net.URI)2 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2 Call (okhttp3.Call)2 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)2 Assert (org.junit.Assert)2 RunWith (org.junit.runner.RunWith)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)2