Search in sources :

Example 1 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class ContextIT method shouldObtainValueFromContextForThread.

@Test
public void shouldObtainValueFromContextForThread() {
    Context context = grpcServer.context();
    TestValue value = new TestValue("Foo");
    context.register(value);
    Echo.EchoResponse response = EchoServiceGrpc.newBlockingStub(channel).echo(Echo.EchoRequest.newBuilder().setMessage("thread").build());
    assertThat(response.getMessage(), is("Foo"));
    assertThat(context.get(TestValue.class).orElse(null), is(sameInstance(value)));
}
Also used : Context(io.helidon.common.context.Context) Echo(io.helidon.grpc.server.test.Echo) Test(org.junit.jupiter.api.Test)

Example 2 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class ContextIT method shouldObtainValueFromContextForRequest.

@Test
public void shouldObtainValueFromContextForRequest() {
    Context context = grpcServer.context();
    TestValue value = new TestValue("Bar");
    context.register(value);
    Echo.EchoResponse response = EchoServiceGrpc.newBlockingStub(channel).echo(Echo.EchoRequest.newBuilder().setMessage("request").build());
    assertThat(response.getMessage(), is("Bar"));
    assertThat(context.get(TestValue.class).orElse(null), is(sameInstance(value)));
}
Also used : Context(io.helidon.common.context.Context) Echo(io.helidon.grpc.server.test.Echo) Test(org.junit.jupiter.api.Test)

Example 3 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class DbClientTracing method apply.

@Override
protected Single<DbClientServiceContext> apply(DbClientServiceContext serviceContext) {
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig("dbclient", "statement");
    if (!spanConfig.enabled()) {
        return Single.just(serviceContext);
    }
    Context context = serviceContext.context();
    Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
    // now if span context is missing, we build a span without a parent
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(serviceContext.statementName());
    context.get(SpanContext.class).ifPresent(spanBuilder::asChildOf);
    Span span = spanBuilder.start();
    span.setTag("db.operation", serviceContext.statementType().toString());
    if (spanConfig.logEnabled("statement", true)) {
        Tags.DB_STATEMENT.set(span, serviceContext.statement());
    }
    Tags.COMPONENT.set(span, "dbclient");
    Tags.DB_TYPE.set(span, serviceContext.dbType());
    serviceContext.statementFuture().thenAccept(nothing -> {
        if (spanConfig.logEnabled("statement-finish", true)) {
            span.log(Map.of("type", "statement"));
        }
    });
    serviceContext.resultFuture().thenAccept(count -> {
        if (spanConfig.logEnabled("result-finish", true)) {
            span.log(Map.of("type", "result", "count", count));
        }
        span.finish();
    }).exceptionally(throwable -> {
        Tags.ERROR.set(span, Boolean.TRUE);
        span.log(Map.of("event", "error", "error.kind", "Exception", "error.object", throwable, "message", throwable.getMessage()));
        span.finish();
        return null;
    });
    return Single.just(serviceContext);
}
Also used : Context(io.helidon.common.context.Context) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) SpanContext(io.opentracing.SpanContext) DbClientServiceBase(io.helidon.dbclient.common.DbClientServiceBase) Tracer(io.opentracing.Tracer) Config(io.helidon.config.Config) TracingConfigUtil(io.helidon.tracing.config.TracingConfigUtil) Context(io.helidon.common.context.Context) GlobalTracer(io.opentracing.util.GlobalTracer) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) Tags(io.opentracing.tag.Tags) SpanContext(io.opentracing.SpanContext) Map(java.util.Map) Single(io.helidon.common.reactive.Single) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) GlobalTracer(io.opentracing.util.GlobalTracer) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 4 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class JulMdcTest method testThreadPropagation.

@Test
public void testThreadPropagation() {
    HelidonMdc.set(TEST_KEY, TEST_VALUE);
    Context context = Context.create();
    ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
    Contexts.runInContext(context, () -> {
        try {
            String value = executor.submit(new TestCallable()).get();
            assertThat(value, is(TEST_VALUE));
        } catch (Exception e) {
            throw new ExecutorException("failed to execute", e);
        }
    });
}
Also used : Context(io.helidon.common.context.Context) ExecutorException(io.helidon.common.context.ExecutorException) ExecutorService(java.util.concurrent.ExecutorService) ExecutorException(io.helidon.common.context.ExecutorException) Test(org.junit.jupiter.api.Test)

Example 5 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class Log4jMdcTest method testThreadPropagation.

@Test
public void testThreadPropagation() {
    HelidonMdc.set(TEST_KEY, TEST_VALUE);
    Context context = Context.create();
    ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
    Contexts.runInContext(context, () -> {
        try {
            String value = executor.submit(new TestCallable()).get();
            assertThat(value, is(TEST_VALUE));
        } catch (Exception e) {
            throw new ExecutorException("failed to execute", e);
        }
    });
}
Also used : ThreadContext(org.apache.logging.log4j.ThreadContext) Context(io.helidon.common.context.Context) ExecutorException(io.helidon.common.context.ExecutorException) ExecutorService(java.util.concurrent.ExecutorService) ExecutorException(io.helidon.common.context.ExecutorException) Test(org.junit.jupiter.api.Test)

Aggregations

Context (io.helidon.common.context.Context)32 Test (org.junit.jupiter.api.Test)14 Contexts (io.helidon.common.context.Contexts)8 WebClient (io.helidon.webclient.WebClient)8 Optional (java.util.Optional)7 Http (io.helidon.common.http.Http)6 Single (io.helidon.common.reactive.Single)6 Config (io.helidon.config.Config)6 ServerRequest (io.helidon.webserver.ServerRequest)6 SpanContext (io.opentracing.SpanContext)6 Logger (java.util.logging.Logger)6 HttpRequest (io.helidon.common.http.HttpRequest)5 SecurityContext (io.helidon.security.SecurityContext)5 ServerResponse (io.helidon.webserver.ServerResponse)5 List (java.util.List)5 Map (java.util.Map)5 WebClientResponse (io.helidon.webclient.WebClientResponse)4 Span (io.opentracing.Span)4 Tracer (io.opentracing.Tracer)4 Collections (java.util.Collections)4