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)));
}
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)));
}
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);
}
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);
}
});
}
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);
}
});
}
Aggregations