Skip to content

Commit

Permalink
Nima: MicroMeter now contains Nima and SE integration
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jun 26, 2023
1 parent 823bfd9 commit 563f7eb
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 534 deletions.
16 changes: 6 additions & 10 deletions examples/integrations/micrometer/se/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,29 @@

<dependencies>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver</artifactId>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver-cors</artifactId>
<groupId>io.helidon.nima.http.media</groupId>
<artifactId>helidon-nima-http-media-jsonp</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.micrometer</groupId>
<artifactId>helidon-integrations-micrometer</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.reactive.media</groupId>
<artifactId>helidon-reactive-media-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webclient</groupId>
<artifactId>helidon-reactive-webclient</artifactId>
<groupId>io.helidon.nima.webclient</groupId>
<artifactId>helidon-nima-webclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,10 +20,11 @@

import io.helidon.common.http.Http;
import io.helidon.config.Config;
import io.helidon.reactive.webserver.Routing;
import io.helidon.reactive.webserver.ServerRequest;
import io.helidon.reactive.webserver.ServerResponse;
import io.helidon.reactive.webserver.Service;
import io.helidon.nima.webserver.http.HttpRequest;
import io.helidon.nima.webserver.http.HttpRules;
import io.helidon.nima.webserver.http.HttpService;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
Expand Down Expand Up @@ -51,7 +52,7 @@
* </p>
*/

public class GreetService implements Service {
public class GreetService implements HttpService {

/**
* The config value for the key {@code greeting}.
Expand All @@ -74,14 +75,13 @@ public class GreetService implements Service {
* @param rules the routing rules.
*/
@Override
public void update(Routing.Rules rules) {
public void routing(HttpRules rules) {
rules
.get((req, resp) -> getTimer.record((Runnable) req::next)) // Update the timer with every GET.
.get("/", (req, resp) -> getTimer.record(() -> {})) // Update the timer with every GET.
.get("/", this::getDefaultMessageHandler)
.get("/{name}",
(req, resp) -> {
personalizedGetCounter.increment();
req.next();
}, // Count personalized GETs...
this::getMessageHandler) // ...and process them.
.put("/greeting", this::updateGreetingHandler);
Expand All @@ -92,7 +92,7 @@ public void update(Routing.Rules rules) {
* @param request the server request
* @param response the server response
*/
private void getDefaultMessageHandler(ServerRequest request,
private void getDefaultMessageHandler(HttpRequest request,
ServerResponse response) {
sendResponse(response, "World");
}
Expand All @@ -104,7 +104,7 @@ private void getDefaultMessageHandler(ServerRequest request,
*/
private void getMessageHandler(ServerRequest request,
ServerResponse response) {
String name = request.path().param("name");
String name = request.path().pathParameters().first("name").get();
sendResponse(response, name);
}

Expand Down Expand Up @@ -135,6 +135,7 @@ private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
*/
private void updateGreetingHandler(ServerRequest request,
ServerResponse response) {
request.content().as(JsonObject.class).thenAccept(jo -> updateGreetingFromJson(jo, response));
JsonObject obj = request.content().as(JsonObject.class);
updateGreetingFromJson(obj, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,11 @@

package io.helidon.examples.micrometer.se;

import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.integrations.micrometer.MicrometerSupport;
import io.helidon.integrations.micrometer.MicrometerFeature;
import io.helidon.logging.common.LogConfig;
import io.helidon.reactive.media.jsonp.JsonpSupport;
import io.helidon.reactive.webserver.Routing;
import io.helidon.reactive.webserver.WebServer;
import io.helidon.nima.http.media.jsonp.JsonpSupport;
import io.helidon.nima.webserver.WebServer;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
Expand Down Expand Up @@ -51,48 +49,16 @@ public static void main(final String[] args) {

/**
* Start the server.
* @return the created {@link WebServer} instance
*/
static Single<WebServer> startServer() {
static WebServer startServer() {

// load logging configuration
LogConfig.configureRuntime();

// By default this will pick up application.yaml from the classpath
Config config = Config.create();

// Get webserver config from the "server" section of application.yaml
WebServer server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.port(-1)
.addMediaSupport(JsonpSupport.create())
.build();

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
// Server threads are not daemon. No need to block. Just react.
return server.start()
.peek(ws -> {
System.out.println(
"WEB server is up! http://localhost:" + ws.port() + "/greet");
ws.whenShutdown().thenRun(()
-> System.out.println("WEB server is DOWN. Good bye!"));
})
.onError(t -> {
System.err.println("Startup failed: " + t.getMessage());
t.printStackTrace(System.err);
});
}

/**
* Creates new {@link Routing}.
*
* @return routing configured with JSON support, Micrometer metrics, and the greeting service
* @param config configuration of this server
*/
private static Routing createRouting(Config config) {

MicrometerSupport micrometerSupport = MicrometerSupport.create();
MicrometerFeature micrometerSupport = MicrometerFeature.create();
Counter personalizedGetCounter = micrometerSupport.registry()
.counter(PERSONALIZED_GETS_COUNTER_NAME);
Timer getTimer = Timer.builder(ALL_GETS_TIMER_NAME)
Expand All @@ -101,9 +67,18 @@ private static Routing createRouting(Config config) {

GreetService greetService = new GreetService(config, getTimer, personalizedGetCounter);

return Routing.builder()
.register(micrometerSupport) // Micrometer support at "/micrometer"
.register("/greet", greetService)
.build();
// Get webserver config from the "server" section of application.yaml
WebServer webServer = WebServer.builder()
.host("localhost")
.config(config.get("server"))
.port(-1)
.addMediaSupport(JsonpSupport.create(config))
.routing(router -> router
.register("/greet", () -> greetService)
.addFeature(() -> MicrometerFeature.builder().config(config).build()))
.build()
.start();
System.out.println("WEB server is up! http://localhost:" + webServer.port() + "/greet");
return webServer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
*/
package io.helidon.examples.micrometer.se;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import io.helidon.common.config.Config;
import io.helidon.common.http.Http;
import io.helidon.reactive.media.jsonp.JsonpSupport;
import io.helidon.reactive.webclient.WebClient;
import io.helidon.reactive.webclient.WebClientResponse;
import io.helidon.reactive.webserver.WebServer;
import io.helidon.nima.http.media.jsonp.JsonpSupport;
import io.helidon.nima.webclient.WebClient;
import io.helidon.nima.webclient.http1.Http1Client;
import io.helidon.nima.webclient.http1.Http1ClientResponse;
import io.helidon.nima.webserver.WebServer;

import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
Expand All @@ -35,18 +40,14 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;

// we need to first call the methods, before validating metrics
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MainTest {

private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Collections.emptyMap());
private static final JsonObject TEST_JSON_OBJECT;
private static WebServer webServer;
private static WebClient webClient;
private static Http1Client webClient;

private static double expectedPersonalizedGets;
private static double expectedAllGets;
Expand All @@ -59,21 +60,17 @@ public class MainTest {

@BeforeAll
public static void startTheServer() {
webServer = Main.startServer()
.await(10, TimeUnit.SECONDS);
webServer = Main.startServer();

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
.addMediaSupport(JsonpSupport.create())
.addMediaSupport(JsonpSupport.create(Config.empty()))
.build();
}

@AfterAll
public static void stopServer() {
if (webServer != null) {
webServer.shutdown()
.await(10, TimeUnit.SECONDS);
}
webServer.stop();
}

private static JsonObject get() {
Expand All @@ -83,8 +80,7 @@ private static JsonObject get() {
private static JsonObject get(String path) {
JsonObject jsonObject = webClient.get()
.path(path)
.request(JsonObject.class)
.await();
.request(JsonObject.class);
expectedAllGets++;
return jsonObject;
}
Expand Down Expand Up @@ -113,10 +109,9 @@ void testNamedGreeting() {
@Order(3)
void testUpdateGreeting() {

WebClientResponse response = webClient.put()
Http1ClientResponse response = webClient.put()
.path("/greet/greeting")
.submit(TEST_JSON_OBJECT)
.await();
.submit(TEST_JSON_OBJECT);

assertThat(response.status(), is(Http.Status.NO_CONTENT_204));

Expand All @@ -127,16 +122,13 @@ void testUpdateGreeting() {
@Test
@Order(4)
void testMicrometer() {
WebClientResponse response = webClient.get()
Http1ClientResponse response = webClient.get()
.path("/micrometer")
.request()
.await();
.request();

assertThat(response.status().code(), is(200));

String output = response.content()
.as(String.class)
.await();
String output = response.as(String.class);
String expected = Main.ALL_GETS_TIMER_NAME + "_seconds_count " + expectedAllGets;
assertThat("Unable to find expected all-gets timer count " + expected + "; output is " + output,
output, containsString(expected)); // all gets; the put
Expand Down
22 changes: 4 additions & 18 deletions integrations/micrometer/micrometer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,10 @@
<artifactId>helidon-common-features-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
<artifactId>helidon-reactive-webserver-cors</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.reactive.service-common</groupId>
<artifactId>helidon-reactive-service-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
Expand All @@ -84,6 +71,10 @@
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver-cors</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webclient</groupId>
<artifactId>helidon-nima-webclient</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.service-common</groupId>
<artifactId>helidon-nima-service-common</artifactId>
Expand Down Expand Up @@ -112,11 +103,6 @@
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.reactive.webclient</groupId>
<artifactId>helidon-reactive-webclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-hocon</artifactId>
Expand Down
Loading

0 comments on commit 563f7eb

Please sign in to comment.