Search in sources :

Example 1 with Cleaner

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

the class AsyncContextImplTest method importToCurrent.

@Test
public void importToCurrent() {
    String name = "test_name";
    String value = "test_value";
    SessionContext sessionContext = new SessionContext();
    AsyncContextImpl asyncContext = AsyncContextImpl.build(NoOpTracer.NO_OP_SPAN_CONTEXT, () -> sessionContext, null);
    asyncContext.put(name, value);
    assertNull(sessionContext.get(name));
    try (Cleaner cleaner = asyncContext.importToCurrent()) {
        assertEquals(value, sessionContext.get(name));
    }
    assertNull(sessionContext.get(name));
    AsyncContextImpl asyncContext2 = AsyncContextImpl.build(NoOpTracer.NO_OP_SPAN_CONTEXT, () -> sessionContext, Collections.singletonMap(name, value));
    assertNull(sessionContext.get(name));
    try (Cleaner cleaner = asyncContext2.importToCurrent()) {
        assertEquals(value, sessionContext.get(name));
    }
    assertNull(sessionContext.get(name));
}
Also used : Cleaner(com.megaease.easeagent.plugin.api.Cleaner) Test(org.junit.Test)

Example 2 with Cleaner

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

the class DoFilterForwardedInterceptor method doBefore.

@Override
public void doBefore(MethodInfo methodInfo, Context context) {
    HttpServletRequest httpServletRequest = (HttpServletRequest) methodInfo.getArgs()[0];
    HttpRequest httpRequest = new HttpServerRequest(httpServletRequest);
    Cleaner cleaner = context.importForwardedHeaders(httpRequest);
    context.put(FORWARDED_KEY, cleaner);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(com.megaease.easeagent.plugin.tools.trace.HttpRequest) Cleaner(com.megaease.easeagent.plugin.api.Cleaner)

Example 3 with Cleaner

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

the class AsyncResponse4MetricsListener method process.

private void process(Response response, Exception exception) {
    try (Cleaner ignored = asyncContext.importToCurrent()) {
        Context context = EaseAgent.getContext();
        Request request = context.get(REQUEST);
        long duration = ContextUtils.getDuration(context);
        boolean success = ElasticsearchCtxUtils.checkSuccess(response, exception);
        this.elasticsearchMetric.collectMetric(ElasticsearchCtxUtils.getIndex(request.getEndpoint()), duration, success);
    }
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) AsyncContext(com.megaease.easeagent.plugin.api.context.AsyncContext) Request(org.elasticsearch.client.Request) Cleaner(com.megaease.easeagent.plugin.api.Cleaner)

Example 4 with Cleaner

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

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

the class GatewayServerForwardedInterceptor method doBefore.

@Override
public void doBefore(MethodInfo methodInfo, Context context) {
    ServerWebExchange exchange = (ServerWebExchange) methodInfo.getArgs()[0];
    FluxHttpServerRequest httpServerRequest = new FluxHttpServerRequest(exchange.getRequest());
    Cleaner cleaner = context.importForwardedHeaders(httpServerRequest);
    context.put(FORWARDED_KEY, cleaner);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) FluxHttpServerRequest(easeagent.plugin.spring.gateway.interceptor.tracing.FluxHttpServerRequest) Cleaner(com.megaease.easeagent.plugin.api.Cleaner)

Aggregations

Cleaner (com.megaease.easeagent.plugin.api.Cleaner)8 AsyncContext (com.megaease.easeagent.plugin.api.context.AsyncContext)4 Context (com.megaease.easeagent.plugin.api.Context)3 Test (org.junit.Test)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)1 Scope (com.megaease.easeagent.plugin.api.trace.Scope)1 Span (com.megaease.easeagent.plugin.api.trace.Span)1 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)1 HttpRequest (com.megaease.easeagent.plugin.tools.trace.HttpRequest)1 HttpResponse (com.megaease.easeagent.plugin.tools.trace.HttpResponse)1 FluxHttpServerRequest (easeagent.plugin.spring.gateway.interceptor.tracing.FluxHttpServerRequest)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Request (org.elasticsearch.client.Request)1 PathPattern (org.springframework.web.util.pattern.PathPattern)1