Skip to content

Commit 29464db

Browse files
Nima: MicroMeter now contains Nima and SE integration (#7090)
* Nima: MicroMeter now contains Nima and SE integration --------- Signed-off-by: Jorge Bescos Gascon <[email protected]> Co-authored-by: Romain Grecourt <[email protected]>
1 parent 067ee8f commit 29464db

File tree

17 files changed

+177
-560
lines changed

17 files changed

+177
-560
lines changed

examples/integrations/micrometer/se/pom.xml

+11-10
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,40 @@
5555

5656
<dependencies>
5757
<dependency>
58-
<groupId>io.helidon.reactive.webserver</groupId>
59-
<artifactId>helidon-reactive-webserver</artifactId>
58+
<groupId>io.helidon.nima.webserver</groupId>
59+
<artifactId>helidon-nima-webserver</artifactId>
6060
</dependency>
6161
<dependency>
6262
<groupId>io.helidon.config</groupId>
6363
<artifactId>helidon-config-yaml</artifactId>
6464
</dependency>
6565
<dependency>
66-
<groupId>io.helidon.reactive.webserver</groupId>
67-
<artifactId>helidon-reactive-webserver-cors</artifactId>
66+
<groupId>io.helidon.nima.http.media</groupId>
67+
<artifactId>helidon-nima-http-media-jsonp</artifactId>
6868
</dependency>
6969
<dependency>
7070
<groupId>io.helidon.integrations.micrometer</groupId>
7171
<artifactId>helidon-integrations-micrometer</artifactId>
7272
</dependency>
73-
<dependency>
74-
<groupId>io.helidon.reactive.media</groupId>
75-
<artifactId>helidon-reactive-media-jsonp</artifactId>
76-
</dependency>
7773
<dependency>
7874
<groupId>org.junit.jupiter</groupId>
7975
<artifactId>junit-jupiter-api</artifactId>
8076
<scope>test</scope>
8177
</dependency>
8278
<dependency>
83-
<groupId>io.helidon.reactive.webclient</groupId>
84-
<artifactId>helidon-reactive-webclient</artifactId>
79+
<groupId>io.helidon.nima.webclient</groupId>
80+
<artifactId>helidon-nima-webclient</artifactId>
8581
<scope>test</scope>
8682
</dependency>
8783
<dependency>
8884
<groupId>org.hamcrest</groupId>
8985
<artifactId>hamcrest-all</artifactId>
9086
<scope>test</scope>
9187
</dependency>
88+
<dependency>
89+
<groupId>io.helidon.nima.testing.junit5</groupId>
90+
<artifactId>helidon-nima-testing-junit5-webserver</artifactId>
91+
<scope>test</scope>
92+
</dependency>
9293
</dependencies>
9394
</project>

examples/integrations/micrometer/se/src/main/java/io/helidon/examples/micrometer/se/GreetService.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,10 +20,11 @@
2020

2121
import io.helidon.common.http.Http;
2222
import io.helidon.config.Config;
23-
import io.helidon.reactive.webserver.Routing;
24-
import io.helidon.reactive.webserver.ServerRequest;
25-
import io.helidon.reactive.webserver.ServerResponse;
26-
import io.helidon.reactive.webserver.Service;
23+
import io.helidon.nima.webserver.http.HttpRequest;
24+
import io.helidon.nima.webserver.http.HttpRules;
25+
import io.helidon.nima.webserver.http.HttpService;
26+
import io.helidon.nima.webserver.http.ServerRequest;
27+
import io.helidon.nima.webserver.http.ServerResponse;
2728

2829
import io.micrometer.core.instrument.Counter;
2930
import io.micrometer.core.instrument.Timer;
@@ -51,7 +52,7 @@
5152
* </p>
5253
*/
5354

54-
public class GreetService implements Service {
55+
public class GreetService implements HttpService {
5556

5657
/**
5758
* The config value for the key {@code greeting}.
@@ -74,14 +75,14 @@ public class GreetService implements Service {
7475
* @param rules the routing rules.
7576
*/
7677
@Override
77-
public void update(Routing.Rules rules) {
78+
public void routing(HttpRules rules) {
7879
rules
79-
.get((req, resp) -> getTimer.record((Runnable) req::next)) // Update the timer with every GET.
80+
.get((req, resp) -> getTimer.record(resp::next)) // Update the timer with every GET.
8081
.get("/", this::getDefaultMessageHandler)
8182
.get("/{name}",
8283
(req, resp) -> {
8384
personalizedGetCounter.increment();
84-
req.next();
85+
resp.next();
8586
}, // Count personalized GETs...
8687
this::getMessageHandler) // ...and process them.
8788
.put("/greeting", this::updateGreetingHandler);
@@ -92,7 +93,7 @@ public void update(Routing.Rules rules) {
9293
* @param request the server request
9394
* @param response the server response
9495
*/
95-
private void getDefaultMessageHandler(ServerRequest request,
96+
private void getDefaultMessageHandler(HttpRequest request,
9697
ServerResponse response) {
9798
sendResponse(response, "World");
9899
}
@@ -104,7 +105,7 @@ private void getDefaultMessageHandler(ServerRequest request,
104105
*/
105106
private void getMessageHandler(ServerRequest request,
106107
ServerResponse response) {
107-
String name = request.path().param("name");
108+
String name = request.path().pathParameters().first("name").get();
108109
sendResponse(response, name);
109110
}
110111

@@ -135,6 +136,7 @@ private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
135136
*/
136137
private void updateGreetingHandler(ServerRequest request,
137138
ServerResponse response) {
138-
request.content().as(JsonObject.class).thenAccept(jo -> updateGreetingFromJson(jo, response));
139+
JsonObject obj = request.content().as(JsonObject.class);
140+
updateGreetingFromJson(obj, response);
139141
}
140142
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,11 @@
1616

1717
package io.helidon.examples.micrometer.se;
1818

19-
import io.helidon.common.reactive.Single;
2019
import io.helidon.config.Config;
21-
import io.helidon.integrations.micrometer.MicrometerSupport;
20+
import io.helidon.integrations.micrometer.MicrometerFeature;
2221
import io.helidon.logging.common.LogConfig;
23-
import io.helidon.reactive.media.jsonp.JsonpSupport;
24-
import io.helidon.reactive.webserver.Routing;
25-
import io.helidon.reactive.webserver.WebServer;
22+
import io.helidon.nima.webserver.WebServer;
23+
import io.helidon.nima.webserver.http.HttpRouting;
2624

2725
import io.micrometer.core.instrument.Counter;
2826
import io.micrometer.core.instrument.Timer;
@@ -43,6 +41,7 @@ private Main() {
4341

4442
/**
4543
* Application main entry point.
44+
*
4645
* @param args command line arguments.
4746
*/
4847
public static void main(final String[] args) {
@@ -51,48 +50,33 @@ public static void main(final String[] args) {
5150

5251
/**
5352
* Start the server.
54-
* @return the created {@link WebServer} instance
5553
*/
56-
static Single<WebServer> startServer() {
54+
static WebServer startServer() {
5755

5856
// load logging configuration
5957
LogConfig.configureRuntime();
6058

61-
// By default this will pick up application.yaml from the classpath
59+
// By default, this will pick up application.yaml from the classpath
6260
Config config = Config.create();
6361

64-
// Get webserver config from the "server" section of application.yaml
65-
WebServer server = WebServer.builder(createRouting(config))
62+
WebServer server = WebServer.builder()
6663
.config(config.get("server"))
67-
.port(-1)
68-
.addMediaSupport(JsonpSupport.create())
69-
.build();
64+
.routing(r -> setupRouting(r, config))
65+
.build()
66+
.start();
7067

71-
// Try to start the server. If successful, print some info and arrange to
72-
// print a message at shutdown. If unsuccessful, print the exception.
73-
// Server threads are not daemon. No need to block. Just react.
74-
return server.start()
75-
.peek(ws -> {
76-
System.out.println(
77-
"WEB server is up! http://localhost:" + ws.port() + "/greet");
78-
ws.whenShutdown().thenRun(()
79-
-> System.out.println("WEB server is DOWN. Good bye!"));
80-
})
81-
.onError(t -> {
82-
System.err.println("Startup failed: " + t.getMessage());
83-
t.printStackTrace(System.err);
84-
});
68+
System.out.println("WEB server is up! http://localhost:" + server.port() + "/greet");
69+
return server;
8570
}
8671

8772
/**
88-
* Creates new {@link Routing}.
73+
* Setup routing.
8974
*
90-
* @return routing configured with JSON support, Micrometer metrics, and the greeting service
91-
* @param config configuration of this server
75+
* @param routing routing builder
76+
* @param config config
9277
*/
93-
private static Routing createRouting(Config config) {
94-
95-
MicrometerSupport micrometerSupport = MicrometerSupport.create();
78+
static void setupRouting(HttpRouting.Builder routing, Config config) {
79+
MicrometerFeature micrometerSupport = MicrometerFeature.create(config);
9680
Counter personalizedGetCounter = micrometerSupport.registry()
9781
.counter(PERSONALIZED_GETS_COUNTER_NAME);
9882
Timer getTimer = Timer.builder(ALL_GETS_TIMER_NAME)
@@ -101,9 +85,7 @@ private static Routing createRouting(Config config) {
10185

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

104-
return Routing.builder()
105-
.register(micrometerSupport) // Micrometer support at "/micrometer"
106-
.register("/greet", greetService)
107-
.build();
88+
routing.register("/greet", greetService)
89+
.addFeature(micrometerSupport);
10890
}
10991
}

examples/integrations/micrometer/se/src/test/java/io/helidon/examples/micrometer/se/MainTest.java

+40-55
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,54 @@
1616
package io.helidon.examples.micrometer.se;
1717

1818
import java.util.Collections;
19-
import java.util.concurrent.TimeUnit;
2019

2120
import io.helidon.common.http.Http;
22-
import io.helidon.reactive.media.jsonp.JsonpSupport;
23-
import io.helidon.reactive.webclient.WebClient;
24-
import io.helidon.reactive.webclient.WebClientResponse;
25-
import io.helidon.reactive.webserver.WebServer;
21+
import io.helidon.config.Config;
22+
import io.helidon.nima.testing.junit5.webserver.ServerTest;
23+
import io.helidon.nima.testing.junit5.webserver.SetUpServer;
24+
import io.helidon.nima.webclient.http1.Http1Client;
25+
import io.helidon.nima.webclient.http1.Http1ClientResponse;
26+
import io.helidon.nima.webserver.ServerConfig;
27+
import io.helidon.nima.webserver.ServerConfig.Builder;
2628

2729
import jakarta.json.Json;
2830
import jakarta.json.JsonBuilderFactory;
2931
import jakarta.json.JsonObject;
30-
import org.junit.jupiter.api.AfterAll;
3132
import org.junit.jupiter.api.Assertions;
32-
import org.junit.jupiter.api.BeforeAll;
3333
import org.junit.jupiter.api.MethodOrderer;
3434
import org.junit.jupiter.api.Order;
3535
import org.junit.jupiter.api.Test;
3636
import org.junit.jupiter.api.TestMethodOrder;
3737

38+
import static org.hamcrest.CoreMatchers.containsString;
3839
import static org.hamcrest.CoreMatchers.is;
3940
import static org.hamcrest.MatcherAssert.assertThat;
40-
import static org.hamcrest.CoreMatchers.containsString;
4141

4242
// we need to first call the methods, before validating metrics
4343
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
44+
@ServerTest
4445
public class MainTest {
4546

4647
private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Collections.emptyMap());
4748
private static final JsonObject TEST_JSON_OBJECT;
48-
private static WebServer webServer;
49-
private static WebClient webClient;
5049

5150
private static double expectedPersonalizedGets;
5251
private static double expectedAllGets;
52+
private final Http1Client client;
5353

5454
static {
5555
TEST_JSON_OBJECT = JSON_BF.createObjectBuilder()
5656
.add("greeting", "Hola")
5757
.build();
5858
}
5959

60-
@BeforeAll
61-
public static void startTheServer() {
62-
webServer = Main.startServer()
63-
.await(10, TimeUnit.SECONDS);
64-
65-
webClient = WebClient.builder()
66-
.baseUri("http://localhost:" + webServer.port())
67-
.addMediaSupport(JsonpSupport.create())
68-
.build();
60+
public MainTest(Http1Client client) {
61+
this.client = client;
6962
}
7063

71-
@AfterAll
72-
public static void stopServer() {
73-
if (webServer != null) {
74-
webServer.shutdown()
75-
.await(10, TimeUnit.SECONDS);
76-
}
77-
}
78-
79-
private static JsonObject get() {
80-
return get("/greet");
81-
}
82-
83-
private static JsonObject get(String path) {
84-
JsonObject jsonObject = webClient.get()
85-
.path(path)
86-
.request(JsonObject.class)
87-
.await();
88-
expectedAllGets++;
89-
return jsonObject;
90-
}
91-
92-
private static JsonObject personalizedGet(String name) {
93-
JsonObject result = get("/greet/" + name);
94-
expectedPersonalizedGets++;
95-
return result;
64+
@SetUpServer
65+
public static void setup(Builder builder) {
66+
builder.routing(r -> Main.setupRouting(r, Config.create()));
9667
}
9768

9869
@Test
@@ -112,13 +83,12 @@ void testNamedGreeting() {
11283
@Test
11384
@Order(3)
11485
void testUpdateGreeting() {
115-
116-
WebClientResponse response = webClient.put()
86+
try (Http1ClientResponse response = client.put()
11787
.path("/greet/greeting")
118-
.submit(TEST_JSON_OBJECT)
119-
.await();
88+
.submit(TEST_JSON_OBJECT)) {
12089

121-
assertThat(response.status(), is(Http.Status.NO_CONTENT_204));
90+
assertThat(response.status(), is(Http.Status.NO_CONTENT_204));
91+
}
12292

12393
JsonObject jsonObject = personalizedGet("Joe");
12494
assertThat(jsonObject.getString("greeting"), is("Hola Joe!"));
@@ -127,16 +97,13 @@ void testUpdateGreeting() {
12797
@Test
12898
@Order(4)
12999
void testMicrometer() {
130-
WebClientResponse response = webClient.get()
100+
Http1ClientResponse response = client.get()
131101
.path("/micrometer")
132-
.request()
133-
.await();
102+
.request();
134103

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

137-
String output = response.content()
138-
.as(String.class)
139-
.await();
106+
String output = response.as(String.class);
140107
String expected = Main.ALL_GETS_TIMER_NAME + "_seconds_count " + expectedAllGets;
141108
assertThat("Unable to find expected all-gets timer count " + expected + "; output is " + output,
142109
output, containsString(expected)); // all gets; the put
@@ -148,4 +115,22 @@ void testMicrometer() {
148115
output, containsString(expected));
149116
response.close();
150117
}
118+
119+
private JsonObject get() {
120+
return get("/greet");
121+
}
122+
123+
private JsonObject get(String path) {
124+
JsonObject jsonObject = client.get()
125+
.path(path)
126+
.request(JsonObject.class);
127+
expectedAllGets++;
128+
return jsonObject;
129+
}
130+
131+
private JsonObject personalizedGet(String name) {
132+
JsonObject result = get("/greet/" + name);
133+
expectedPersonalizedGets++;
134+
return result;
135+
}
151136
}

0 commit comments

Comments
 (0)