Search in sources :

Example 26 with Context

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

the class HelloBean method getHelloTimeout.

/**
 * Runs in FT thread.
 *
 * @return Hello string.
 */
@Timeout(1000)
public String getHelloTimeout() {
    Context context = Contexts.context().orElse(null);
    Objects.requireNonNull(context);
    // application scoped context
    Objects.requireNonNull(context.get(RegistryFactory.class).orElse(null));
    // request scoped context
    Objects.requireNonNull(context.get(MyMessage.class).orElse(null));
    return "Hello World";
}
Also used : Context(io.helidon.common.context.Context) Timeout(org.eclipse.microprofile.faulttolerance.Timeout)

Example 27 with Context

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

the class GreetService method contextCheck.

/**
 * Checks the existence of a {@code Context} object in a WebClient thread.
 *
 * @param request the request
 * @param response the response
 */
private void contextCheck(ServerRequest request, ServerResponse response) {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/").build();
    Optional<Context> context = Contexts.context();
    // Verify that context was propagated with auth enabled
    if (context.isEmpty()) {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send();
        return;
    }
    // Register instance in context
    context.get().register(this);
    // Ensure context is available in webclient threads
    webClient.get().request().thenAccept(clientResponse -> {
        Context singleContext = Contexts.context().orElseThrow();
        Objects.requireNonNull(singleContext.get(GreetService.class));
        response.status(Http.Status.OK_200);
        response.send();
    }).exceptionally(throwable -> {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        response.send();
        return null;
    });
}
Also used : Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) WebClient(io.helidon.webclient.WebClient) DataChunk(io.helidon.common.http.DataChunk) Context(io.helidon.common.context.Context) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Level(java.util.logging.Level) FormParams(io.helidon.common.http.FormParams) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) Service(io.helidon.webserver.Service) JsonException(jakarta.json.JsonException) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) Config(io.helidon.config.Config) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) Logger(java.util.logging.Logger) Contexts(io.helidon.common.context.Contexts) Executors(java.util.concurrent.Executors) ServerRequest(io.helidon.webserver.ServerRequest) Json(jakarta.json.Json) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Principal(java.security.Principal) Optional(java.util.Optional) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) WebClient(io.helidon.webclient.WebClient)

Example 28 with Context

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

the class TracingPropagationTest method testTracingSuccess.

@Test
void testTracingSuccess() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    WebServer webServer = Main.startServer(mockTracer).toCompletableFuture().get();
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    String uri = "http://localhost:" + webServer.port() + "/greet";
    WebClient client = WebClient.builder().baseUri(uri).context(context).config(Config.create().get("client")).build();
    client.get().queryParam("some", "value").fragment("fragment").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
    TimeUnit.MILLISECONDS.sleep(1);
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat("At least one client and one server span expected", mockSpans.size(), greaterThanOrEqualTo(2));
    // we need the first span - parentId 0
    MockSpan clientSpan = findSpanWithParentId(mockSpans, 0);
    assertThat(clientSpan.operationName(), is("GET-" + uri));
    List<MockSpan.LogEntry> logEntries = clientSpan.logEntries();
    assertThat(logEntries, empty());
    Map<String, Object> tags = clientSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    // now we want to test first child - first WebServer span
    MockSpan wsSpan = findSpanWithParentId(mockSpans, clientSpan.context().spanId());
    assertThat(wsSpan.operationName(), is("HTTP Request"));
    tags = wsSpan.tags();
    assertThat(tags.get(Tags.HTTP_METHOD.getKey()), is("GET"));
    assertThat(tags.get(Tags.HTTP_URL.getKey()), is("/greet?some=value#fragment"));
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    assertThat(tags.get(Tags.COMPONENT.getKey()), is("helidon-webserver"));
    webServer.shutdown().toCompletableFuture().get();
}
Also used : Context(io.helidon.common.context.Context) WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) MockTracer(io.opentracing.mock.MockTracer) WebClient(io.helidon.webclient.WebClient) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.jupiter.api.Test)

Example 29 with Context

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

the class TracingTest method testTracingNoServerSuccess.

@Test
void testTracingNoServerSuccess() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    String uri = "http://localhost:" + webServer.port() + "/greet";
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    WebClient client = WebClient.builder().baseUri(uri).context(context).addMediaSupport(JsonpSupport.create()).config(CONFIG.get("client")).build();
    WebClientResponse response = client.get().request().toCompletableFuture().get();
    // we must fully read entity for tracing to finish
    response.content().as(JsonObject.class).toCompletableFuture().get();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat(mockSpans, iterableWithSize(1));
    MockSpan theSpan = mockSpans.get(0);
    assertThat(theSpan.operationName(), is("GET-" + uri));
    List<MockSpan.LogEntry> logEntries = theSpan.logEntries();
    assertThat(logEntries, empty());
    Map<String, Object> tags = theSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    assertThat(tags.get(Tags.HTTP_METHOD.getKey()), is("GET"));
    assertThat(tags.get(Tags.HTTP_URL.getKey()), is(uri));
    assertThat(tags.get(Tags.COMPONENT.getKey()), is("helidon-webclient"));
}
Also used : Context(io.helidon.common.context.Context) WebClientResponse(io.helidon.webclient.WebClientResponse) MockTracer(io.opentracing.mock.MockTracer) JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.jupiter.api.Test)

Example 30 with Context

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

the class TracingTest method testTracingNoServerFailure.

@Test
void testTracingNoServerFailure() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").context(context).addMediaSupport(JsonpSupport.create()).config(CONFIG.get("client")).build();
    WebClientResponse response = client.get().path("/error").request().toCompletableFuture().get();
    // we must fully read entity, as otherwise tracing does not finish
    response.content().as(String.class).toCompletableFuture().get();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat(mockSpans, iterableWithSize(1));
    MockSpan theSpan = mockSpans.get(0);
    assertThat(theSpan.operationName(), is("GET-http://localhost:" + webServer.port() + "/greet/error"));
    List<MockSpan.LogEntry> logEntries = theSpan.logEntries();
    assertThat(logEntries, iterableWithSize(1));
    MockSpan.LogEntry logEntry = logEntries.get(0);
    Map<String, ?> fields = logEntry.fields();
    assertThat(fields.get("event"), is("error"));
    assertThat(fields.get("message"), is("Response HTTP status: 404"));
    assertThat(fields.get("error.kind"), is("ClientError"));
    Map<String, Object> tags = theSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(404));
}
Also used : Context(io.helidon.common.context.Context) MockTracer(io.opentracing.mock.MockTracer) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonObject(jakarta.json.JsonObject) MockSpan(io.opentracing.mock.MockSpan) 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