Search in sources :

Example 1 with AsyncContext

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

the class ElasticsearchPerformRequestAsync4TraceInterceptor method before.

@Override
public void before(MethodInfo methodInfo, Context context) {
    ElasticsearchCtxUtils.initSpan(methodInfo, context);
    AsyncContext asyncContext = context.exportAsync();
    ResponseListener listener = (ResponseListener) methodInfo.getArgs()[1];
    ResponseListener asyncResponseListener = new AsyncResponse4TraceListener(listener, asyncContext);
    methodInfo.changeArg(1, asyncResponseListener);
}
Also used : AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) ResponseListener(org.elasticsearch.client.ResponseListener)

Example 2 with AsyncContext

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

the class AgentMonoTest method testImportToCurrent.

@Test
public void testImportToCurrent() throws InterruptedException {
    Context context = EaseAgent.getContext();
    Span span = context.nextSpan();
    Thread thread;
    try (Scope ignored4 = span.maybeScope()) {
        AsyncContext asyncContext1 = context.exportAsync();
        AsyncContext asyncContext2 = context.exportAsync();
        AsyncContext asyncContext3 = context.exportAsync();
        thread = new Thread(() -> {
            Context asyncContext = EaseAgent.getContext();
            assertFalse(asyncContext.currentTracing().hasCurrentSpan());
            try (Cleaner ignored = asyncContext1.importToCurrent()) {
                assertTrue(asyncContext.currentTracing().hasCurrentSpan());
                try (Cleaner ignored1 = asyncContext2.importToCurrent()) {
                    assertTrue(asyncContext.currentTracing().hasCurrentSpan());
                    try (Cleaner ignored2 = asyncContext3.importToCurrent()) {
                        assertTrue(asyncContext.currentTracing().hasCurrentSpan());
                    }
                    assertTrue(asyncContext.currentTracing().hasCurrentSpan());
                }
                assertTrue(asyncContext.currentTracing().hasCurrentSpan());
            }
            assertFalse(asyncContext.currentTracing().hasCurrentSpan());
        });
    }
    thread.start();
    thread.join();
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Scope(com.megaease.easeagent.plugin.api.trace.Scope) AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Span(com.megaease.easeagent.plugin.api.trace.Span) Cleaner(com.megaease.easeagent.plugin.api.Cleaner) Test(org.junit.Test)

Example 3 with AsyncContext

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

the class AgentCoreSubscriberTest method onError.

@Test
public void onError() {
    MockCoreSubscriber mockCoreSubscriber = new MockCoreSubscriber();
    MethodInfo errorMethodInfo = MethodInfo.builder().build();
    AsyncContext errorAsyncContext = EaseAgent.getContext().exportAsync();
    RuntimeException runtimeException = new RuntimeException();
    AgentCoreSubscriber agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, errorMethodInfo, errorAsyncContext, (methodInfo, asyncContext) -> {
        assertNotNull(methodInfo);
        assertNotNull(asyncContext);
        assertSame(errorMethodInfo, methodInfo);
        assertSame(errorAsyncContext, asyncContext);
        assertFalse(methodInfo.isSuccess());
        assertSame(runtimeException, methodInfo.getThrowable());
    });
    agentCoreSubscriber.onError(runtimeException);
    assertTrue(mockCoreSubscriber.onError.get());
}
Also used : MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Test(org.junit.Test)

Example 4 with AsyncContext

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

the class SessionContextTest method exportAsync.

@Test
public void exportAsync() {
    SessionContext sessionContext = new SessionContext();
    sessionContext.setSupplier(EaseAgent.initializeContextSupplier);
    MockITracing iTracing = new MockITracing();
    sessionContext.setCurrentTracing(iTracing);
    String name = "test_name";
    String value = "test_value";
    assertNull(sessionContext.get(name));
    sessionContext.put(name, value);
    assertEquals(value, sessionContext.get(name));
    AsyncContext asyncContext = sessionContext.exportAsync();
    assertTrue(asyncContext.getSpanContext().isNoop());
    assertEquals(1, iTracing.exportAsyncCount.get());
    assertEquals(value, asyncContext.get(name));
    assertFalse(asyncContext.isNoop());
    SessionContext sessionContext2 = new SessionContext();
    sessionContext2.setCurrentTracing(iTracing);
    assertNull(sessionContext2.get(name));
    try (Cleaner ignored = sessionContext2.importAsync(asyncContext)) {
        assertEquals(value, sessionContext2.get(name));
        assertEquals(1, iTracing.importAsyncCount.get());
        try (Cleaner ignored1 = sessionContext2.importAsync(asyncContext)) {
            assertEquals(2, iTracing.importAsyncCount.get());
        }
        assertEquals(value, sessionContext2.get(name));
    }
}
Also used : AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Cleaner(com.megaease.easeagent.plugin.api.Cleaner) Test(org.junit.Test)

Example 5 with AsyncContext

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

the class AgentCoreSubscriberTest method onComplete.

@Test
public void onComplete() {
    MockCoreSubscriber mockCoreSubscriber = new MockCoreSubscriber();
    MethodInfo completeMethodInfo = MethodInfo.builder().build();
    AsyncContext completeAsyncContext = EaseAgent.getContext().exportAsync();
    AgentCoreSubscriber agentCoreSubscriber = new AgentCoreSubscriber(mockCoreSubscriber, completeMethodInfo, completeAsyncContext, (methodInfo, asyncContext) -> {
        assertNotNull(methodInfo);
        assertNotNull(asyncContext);
        assertSame(completeMethodInfo, methodInfo);
        assertSame(completeAsyncContext, asyncContext);
        assertTrue(methodInfo.isSuccess());
    });
    agentCoreSubscriber.onComplete();
    assertTrue(mockCoreSubscriber.onComplete.get());
}
Also used : MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Test(org.junit.Test)

Aggregations

AsyncContext (com.megaease.easeagent.plugin.api.context.AsyncContext)6 Test (org.junit.Test)4 Cleaner (com.megaease.easeagent.plugin.api.Cleaner)2 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)2 ResponseListener (org.elasticsearch.client.ResponseListener)2 Context (com.megaease.easeagent.plugin.api.Context)1 Scope (com.megaease.easeagent.plugin.api.trace.Scope)1 Span (com.megaease.easeagent.plugin.api.trace.Span)1 Request (org.elasticsearch.client.Request)1