diff --git a/examples/micrometer/README.MD b/examples/micrometer/README.MD index a1baad93c0..ea0e16c92b 100644 --- a/examples/micrometer/README.MD +++ b/examples/micrometer/README.MD @@ -18,17 +18,17 @@ The mapping of the URI path space is presented in the following table: URI path | Resource class | HTTP methods ------------------------------------------ | ------------------------- | -------------- -**_/micro/measure/timed_** | MeasuredTimedResource | GET -**_/micro/init_** | MetricsResource | GET +**_/micro/timed_** | MeasuredTimedResource | GET +**_/micro/metrics_** | MetricsResource | GET **_/micro/summary_** | SummaryResource | GET Sample Response --------------- ```html ---- (micro/measure/timed) +--- (micro/timed) Requests to this method are measured. Use /init to see more ----- (micro/init) +---- (micro/metrics) Static meters are initialized, try summary. If you want more measurements just refresh this page several times. ---- (micro/summary) Listing available meters @@ -47,9 +47,9 @@ Run the example using [Grizzly](https://javaee.github.io/grizzly/) container as > mvn clean compile exec:java -- +- - after few request to the main page go to the url -- +- - and see the responses from available resource pages - then go to the - diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java index f7c0c13a21..3e4f465112 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java @@ -21,7 +21,6 @@ public class App { private static final URI BASE_URI = URI.create("http://localhost:8080/micro/"); - public static final String ROOT_PATH = "measure"; public static final String WEB_PATH = "/micro/"; public static void main(String[] args) { @@ -42,7 +41,7 @@ public void run() { System.out.println(String.format("Application started.\nTry out %s%s\n" + "And after that go to the %s%s\n" + "Stop the application using CTRL+C", - BASE_URI, ROOT_PATH + "/timed", BASE_URI, "init")); + BASE_URI, "timed", BASE_URI, "metrics")); Thread.currentThread().join(); } catch (IOException | InterruptedException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java index d574b841b2..28f1f1185c 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java @@ -10,23 +10,19 @@ package org.glassfish.jersey.examples.micrometer; -import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.Timer; - import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import java.util.concurrent.TimeUnit; import static org.glassfish.jersey.examples.micrometer.App.WEB_PATH; -@Path("init") +@Path("metrics") public class MetricsResource { @GET @Produces("text/html") public String getMeters() { - return "Static meters are initialized, try Gaining measurements for the summary page, try summary. If you want more measurements just refresh this page several times." + ""; } diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java index 8fdf1e0b50..6ce457c18d 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java @@ -10,20 +10,27 @@ package org.glassfish.jersey.examples.micrometer; +import org.glassfish.jersey.internal.inject.AbstractBinder; import org.glassfish.jersey.server.ResourceConfig; import javax.ws.rs.ApplicationPath; -@ApplicationPath("/*") +@ApplicationPath("/") public class MetricsResourceConfig extends ResourceConfig { private final MetricsStore store = new MetricsStore(); public MetricsResourceConfig() { + register(new AbstractBinder() { + @Override + protected void configure() { + bind(store).to(MetricsStore.class); + } + }); register(store.getMetricsApplicationEventListener()); - register(MeasuredTimedResource.class); + register(TimedResource.class); register(MetricsResource.class); - register(new SummaryResource(store)); + register(SummaryResource.class); } public MetricsStore getStore() { diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java index 42e7030bc5..01eaed897a 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java @@ -11,9 +11,9 @@ package org.glassfish.jersey.examples.micrometer; import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.binder.jersey.server.DefaultJerseyTagsProvider; -import io.micrometer.core.instrument.binder.jersey.server.MetricsApplicationEventListener; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProvider; +import org.glassfish.jersey.micrometer.server.MetricsApplicationEventListener; public class MetricsStore { diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java index bf2e684a8c..5f82d156de 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java @@ -16,16 +16,17 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; import java.util.concurrent.TimeUnit; +import static org.glassfish.jersey.examples.micrometer.App.WEB_PATH; +import static org.glassfish.jersey.examples.micrometer.MetricsStore.REGISTRY_NAME; + @Path("summary") public class SummaryResource { - private final MetricsStore store; - - public SummaryResource(MetricsStore store) { - this.store = store; - } + @Context + private MetricsStore store; @GET @Produces("text/html") @@ -33,41 +34,45 @@ public String getExtendedMeters() { final StringBuffer result = new StringBuffer(); try { result.append("" - + "Listing available meters
Many occurrences of the same name means that there are more meters" + + "Listing available meters

Many occurrences of the same name means that there are more meters" + " which could be used with different tags," - + " but this is actually a challenge to handle all available metrics :
"); + + " but this is actually a challenge to handle all available metrics :

"); for (final Meter meter : store.getRegistry().getMeters()) { result.append(meter.getId().getName()); result.append(";
\n\r "); } } catch (Exception ex) { - result.append("Looks like there are no proper metrics.
"); - result.append("\n\r"); - result.append("Please visit /measure/timed first
"); - result.append(ex); + result.append("Try clicking links below to gain more metrics.
"); } - if (store.getRegistry().getMeters().size() > 0) { - try { - final Timer timer = store.getRegistry().get("http.shared.metrics") - .tags("method", "GET", "status", "200", "exception", "None", "outcome", "SUCCESS", "uri", "/micro/init") - .timer(); + result.append("
\n\r "); + result.append("
\n\r "); + try { + final Timer timer = store.getRegistry().get(REGISTRY_NAME) + .tags("method", "GET", "status", "200", "exception", "None", + "outcome", "SUCCESS", "uri", "/micro/metrics") + .timer(); - result.append(String.format("Counts to the init page: %d, time spent on requests to the init " - + "page (millis): %f
\n\r", - timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); + result.append( + String.format("Counts to the page with standard measurements: %d, time spent on requests to the init " + + "page (millis): %f
\n\r", + timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); - final Timer annotatedTimer = store.getRegistry().timer(MeasuredTimedResource.TIMER_NAME, - "method", "GET", "status", "200", "exception", "None", - "outcome", "SUCCESS", "uri", "/micro/measure/timed"); + final Timer annotatedTimer = store.getRegistry().timer(TimedResource.TIMER_NAME, + "method", "GET", "status", "200", "exception", "None", + "outcome", "SUCCESS", "uri", "/micro/timed"); - result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f
\n\r", - annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); + result.append( + String.format("Counts to the page with annotated measurements: %d, total time (millis): %f
\n\r", + annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); - } catch (Exception ex) { - result.append("Exception occurred, more info is in console...
"); - result.append(ex); - } + } catch (Exception ex) { + result.append(String.format("Counts to the init page: %d, total time (millis): %d
\n\r", + 0, 0)); + result.append("Try clicking links below to gain more metrics.
"); } + result.append("

Available pages for measurements: measure requests in the standard way  , measure requests in the annotated way"); return result.append("").toString(); } } diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/TimedResource.java similarity index 70% rename from examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java rename to examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/TimedResource.java index 25f70280b9..654cba49be 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/TimedResource.java @@ -18,18 +18,18 @@ import static org.glassfish.jersey.examples.micrometer.App.WEB_PATH; -@Path("measure") -public class MeasuredTimedResource { +@Path("timed") +public class TimedResource { - public static final String MESSAGE = "Requests to this method are measured. " - + "Use /init to see moreTake a look at the standard way of measurements
" + + "Or just go to summary to check what you've got"; public static final String TIMER_NAME = "http.timers"; public static final String TIMER_DESCRIPTION = "resource measurement timer"; @GET @Produces("text/html") @Timed(value = TIMER_NAME, description = TIMER_DESCRIPTION, histogram = true) - @Path("timed") public String getTimedMessage() { return MESSAGE; } diff --git a/examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java b/examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java index af752bb8e6..e8612c4c1c 100644 --- a/examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java +++ b/examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java @@ -17,7 +17,7 @@ import javax.ws.rs.core.Application; import java.util.concurrent.TimeUnit; -import static org.glassfish.jersey.examples.micrometer.MeasuredTimedResource.MESSAGE; +import static org.glassfish.jersey.examples.micrometer.TimedResource.MESSAGE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -36,16 +36,16 @@ protected Application configure() { @Test void meterResourceTest() throws InterruptedException { - final String response = target("/measure/timed").request().get(String.class); + final String response = target("/timed").request().get(String.class); assertEquals(response, MESSAGE); for (int i = 0; i < REQUESTS_COUNT; i++) { - target("/init").request().get(String.class); + target("/metrics").request().get(String.class); } // Jersey metrics are recorded asynchronously to the request completing Thread.sleep(10); Timer timer = resourceConfig.getStore().getRegistry() .get(MetricsStore.REGISTRY_NAME) - .tags("method", "GET", "uri", "/init", "status", "200", "exception", "None", "outcome", "SUCCESS") + .tags("method", "GET", "uri", "/metrics", "status", "200", "exception", "None", "outcome", "SUCCESS") .timer(); assertEquals(REQUESTS_COUNT, timer.count()); assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS));