Search in sources :

Example 16 with Context

use of com.megaease.easeagent.plugin.api.Context 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 17 with Context

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

the class MongoReactiveMetricCommandListener method commandStarted.

@Override
public void commandStarted(CommandStartedEvent event) {
    // LOGGER.warn("reactive commandStarted metric");
    Context context = EaseAgent.getContext();
    MongoCtx mongoCtx = MongoCtx.getOrCreate(context);
    MetricHelper.commandStarted(context, event);
    mongoCtx.put(MongoUtils.METRIC, this.mongoMetric);
    mongoCtx.put(MongoUtils.CONFIG, this.config);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) MongoCtx(com.megaease.easeagent.plugin.mongodb.interceptor.MongoCtx)

Example 18 with Context

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

the class MongoReactiveMetricCommandListener method commandFailed.

@Override
public void commandFailed(CommandFailedEvent event) {
    // LOGGER.warn("reactive commandFailed metric");
    Context context = EaseAgent.getContext();
    context.put(MongoUtils.EVENT_KEY, event);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context)

Example 19 with Context

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

the class TraceCallbackTest method onCompletion.

@Test
public void onCompletion() throws InterruptedException {
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan().start();
    TraceCallback traceCallback = new TraceCallback(span, null);
    traceCallback.onCompletion(null, null);
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    assertFalse(mockSpan.hasError());
    span = context.nextSpan().start();
    String errorInfo = "test error";
    traceCallback = new TraceCallback(span, null);
    traceCallback.onCompletion(null, new RuntimeException(errorInfo));
    mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    assertTrue(mockSpan.hasError());
    assertEquals(errorInfo, mockSpan.errorInfo());
    AtomicBoolean ran = new AtomicBoolean(false);
    span = context.nextSpan().start();
    final TraceCallback traceCallbackAsync = new TraceCallback(span, (metadata, exception) -> {
        assertTrue(EaseAgent.getContext().currentTracing().hasCurrentSpan());
        ran.set(true);
    });
    Thread thread = new Thread(() -> traceCallbackAsync.onCompletion(null, null));
    thread.start();
    thread.join();
    mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    assertFalse(mockSpan.hasError());
    assertTrue(ran.get());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Example 20 with Context

use of com.megaease.easeagent.plugin.api.Context 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)

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