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