From 959c43f3e3541af6f8eb58805b66cdc6f1c31ff4 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 27 Sep 2023 14:07:55 +0200 Subject: [PATCH 01/11] UserGuide and example extended for Micrometer integration Signed-off-by: Maxim Nesen --- bundles/apidocs/pom.xml | 10 ++ docs/pom.xml | 2 +- docs/src/main/docbook/client.xml | 5 + docs/src/main/docbook/jersey.ent | 3 + docs/src/main/docbook/micrometer.xml | 92 +++++++++++++++++++ docs/src/main/docbook/modules.xml | 64 +++++++++++++ docs/src/main/docbook/user-guide.xml | 3 +- examples/micrometer/README.MD | 39 ++++---- examples/micrometer/pom.xml | 17 ++-- .../jersey/examples/micrometer/App.java | 38 +++----- .../examples/micrometer/MeasuredResource.java | 51 ++++++++++ .../examples/micrometer/MetricsResource.java | 73 +++++++-------- .../micrometer/MetricsResourceConfig.java | 32 +++++++ .../examples/micrometer/MetricsStore.java | 43 +++++++++ .../micrometer/MicrometerResource.java | 33 ------- .../examples/micrometer/MicrometerTest.java | 45 ++++----- ext/microprofile/pom.xml | 3 +- pom.xml | 4 +- 18 files changed, 397 insertions(+), 160 deletions(-) create mode 100644 docs/src/main/docbook/micrometer.xml create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java delete mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java diff --git a/bundles/apidocs/pom.xml b/bundles/apidocs/pom.xml index 50fb94d8a5..3fd55ea3cd 100644 --- a/bundles/apidocs/pom.xml +++ b/bundles/apidocs/pom.xml @@ -101,6 +101,11 @@ jersey-jetty-connector ${project.version} + + org.glassfish.jersey.connectors + jersey-jetty-http2-connector + ${project.version} + org.glassfish.jersey.connectors jersey-netty-connector @@ -213,6 +218,11 @@ jersey-declarative-linking ${project.version} + + org.glassfish.jersey.ext.micrometer + jersey-micrometer + ${project.version} + org.glassfish.jersey.ext.microprofile jersey-mp-config diff --git a/docs/pom.xml b/docs/pom.xml index 4f7621a9c7..5886dfe794 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -77,7 +77,7 @@ com.agilejava.docbkx docbkx-maven-plugin - 2.0.15 + 2.0.17 net.sf.docbook diff --git a/docs/src/main/docbook/client.xml b/docs/src/main/docbook/client.xml index 85e9fd8af6..6f2be1c841 100644 --- a/docs/src/main/docbook/client.xml +++ b/docs/src/main/docbook/client.xml @@ -660,6 +660,11 @@ webTarget.request().post(Entity.entity(f, MediaType.TEXT_PLAIN_TYPE)); &jersey.jetty.JettyConnectorProvider; org.glassfish.jersey.connectors:jersey-jetty-connector + + Jetty HTTP/2 client + &jersey.jetty.JettyHttp2ConnectorProvider; + org.glassfish.jersey.connectors:jersey-jetty-http2-connector + Netty NIO framework &jersey.netty.NettyConnectorProvider; diff --git a/docs/src/main/docbook/jersey.ent b/docs/src/main/docbook/jersey.ent index 25e6b9e260..5e7511a74b 100644 --- a/docs/src/main/docbook/jersey.ent +++ b/docs/src/main/docbook/jersey.ent @@ -109,6 +109,8 @@ Helidon"> SmallRye"> Yasson"> +Micrometer project"> +Micrometer Jersey/Jetty support"> Configuration"> @@ -476,6 +478,7 @@ JettyClientProperties.SYNC_LISTENER_RESPONSE_MAX_SIZE" > JettyClientProperties.TOTAL_TIMEOUT" > JettyConnectorProvider"> +JettyHttp2ConnectorProvider"> JettyHttpContainer"> JettyHttpContainerFactory"> JettyHttpContainerProvider"> diff --git a/docs/src/main/docbook/micrometer.xml b/docs/src/main/docbook/micrometer.xml new file mode 100644 index 0000000000..cdcfca19f7 --- /dev/null +++ b/docs/src/main/docbook/micrometer.xml @@ -0,0 +1,92 @@ + + + + %ents; ]> + + Micrometer - application observability facade + + The chapter is about Micrometer integration into Jersey which comes since the version 2.41 as an extension module. + Before Jersey 2.41 it was possible to integrate Micrometer with Jersey using directly µmeter.jersey.link; + There is also support for Jakarta EE 10 integration. Detailed documentation regarding metrics fine-tuning + can be found at the µmeter.link; + +
+ Integration into Jersey + + Since Jersey 2.41 it's possibly to use an extension module in order to use Micrometer instrumentation + inside your projects. The module shall be added as a dependency: + + <dependency> + <groupId>org.glassfish.jersey.ext.micrometer</groupId> + <artifactId>jersey-micrometer</artifactId> + <version>&version;</scope> + </dependency> + + After the dependency is added, the Micrometer can be configured as follows: + + final ResourceConfig resourceConfig = new ResourceConfig(); + resourceConfig.register(new MetricsApplicationEventListener( + registry, + new DefaultJerseyTagsProvider(), + "http.shared.metrics", + true)); + final ServletContainer servletContainer = new ServletContainer(resourceConfig); + + the registry instance is of type MeterRegistry which could be + new SimpleMeterRegistry();. Then all metrics can be accessed like + registry.get("http.shared.metrics"). The "http.shared.metrics" string + is the name of a particular registry which was registered within the + MetricsApplicationEventListener. + + Micrometer supports a set of Meter primitives, including Timer, + Counter, Gauge, DistributionSummary, + LongTaskTimer, FunctionCounter, FunctionTimer, + and TimeGauge. + Different meter types result in a different number of time series metrics. For example, while there is + a single metric that represents a Gauge, a Timer measures both the + count of timed events and the total time of all timed events. + + + Implementing resource methods, which should be measured, several annotations can be used. The basic example + demonstrates the @Counted annotation. + + Annotated Micrometer resource methods + + @GET + @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) + @Produces(MediaType.TEXT_PLAIN) + @Path("counted") + public String getCounterMessage() { + return "Requests to this method are counted. Use /metrics to see more"; + } + + + Metrics however can be introduced using another annotations @Timed, or + @TimedSet which is set of @Timed. + +
+
\ No newline at end of file diff --git a/docs/src/main/docbook/modules.xml b/docs/src/main/docbook/modules.xml index 7fa60b42c1..6f029df9a5 100644 --- a/docs/src/main/docbook/modules.xml +++ b/docs/src/main/docbook/modules.xml @@ -116,6 +116,14 @@
+ + jersey-container-jetty-http2 + + +Jetty HTTP/2 Container + + + jersey-container-jetty-servlet @@ -178,6 +186,14 @@ + + jersey-apache5-connector + + +Jersey Client Transport via Apache 5 + + + jersey-apache-connector @@ -194,6 +210,14 @@ + + jersey-helidon-connector + + +Jersey Client Transport via Helidon + + + jersey-jdk-connector @@ -210,6 +234,14 @@ + + jersey-jetty-http2-connector + + +Jersey Client Transport via Jetty with HTTP/2 support + + + jersey-netty-connector @@ -398,6 +430,30 @@ + + jersey-micrometer + + +Jersey extension module providing support for Micrometer. + + + + + jersey-mp-config + + +Jersey extension module providing support for MicroProfile Configuration. + + + + + jersey-mp-rest-client + + +Jersey extension module providing support for MicroProfile REST Client. + + + jersey-mvc @@ -486,6 +542,14 @@ + + jersey-spring5 + + +Jersey extension module providing support for Spring 5 integration. + + + jersey-wadl-doclet diff --git a/docs/src/main/docbook/user-guide.xml b/docs/src/main/docbook/user-guide.xml index 799478d677..2973292c38 100644 --- a/docs/src/main/docbook/user-guide.xml +++ b/docs/src/main/docbook/user-guide.xml @@ -1,7 +1,7 @@ @@ -63,6 +57,11 @@ junit-jupiter-api test
+ + org.aspectj + aspectjweaver + ${aspectj.weaver.version} +
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 92dcf22e85..2ccb769169 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 @@ -2,50 +2,34 @@ * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + * SPDX-License-Identifier: BSD-3-Clause */ package org.glassfish.jersey.examples.micrometer; +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; + import java.io.IOException; import java.net.URI; import java.util.logging.Level; import java.util.logging.Logger; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProvider; -import org.glassfish.jersey.micrometer.server.MetricsApplicationEventListener; -import org.glassfish.jersey.server.ResourceConfig; - -import org.glassfish.grizzly.http.server.HttpServer; - public class App { private static final URI BASE_URI = URI.create("http://localhost:8080/micro/"); - public static final String ROOT_PATH = "meter"; + public static final String ROOT_PATH = "measure"; public static void main(String[] args) { try { System.out.println("Micrometer/ Jersey Basic Example App"); - final MeterRegistry registry = new SimpleMeterRegistry(); - - final ResourceConfig resourceConfig = new ResourceConfig(MicrometerResource.class) - .register(new MetricsApplicationEventListener(registry, new DefaultJerseyTagsProvider(), - "http.shared.metrics", true)) - .register(new MetricsResource(registry)); - final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false); + final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, + new MetricsResourceConfig(), + false); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { @@ -57,7 +41,7 @@ public void run() { System.out.println(String.format("Application started.\nTry out %s%s\n" + "After several requests go to %s%s\nAnd after that go to the %s%s\n" + "Stop the application using CTRL+C", - BASE_URI, ROOT_PATH, BASE_URI, "metrics", BASE_URI, "metrics/metrics")); + BASE_URI, ROOT_PATH + "/timed", BASE_URI, ROOT_PATH + "/counted", 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/MeasuredResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java new file mode 100644 index 0000000000..f3221b4374 --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.micrometer; + +import io.micrometer.core.annotation.Counted; +import io.micrometer.core.annotation.Timed; + +import javax.inject.Singleton; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("measure") +public class MeasuredResource { + + public static final String CLICHED_MESSAGE = "Requests to this method are measured. Use /metrics to see more"; + + public static final String TIMER_NAME = "http.timers"; + public static final String COUNTER_NAME = "http.counters"; + public static final String TIMER_DESCRIPTION = "resource measurement timer"; + public static final String COUNTER_DESCRIPTION = "resource measurement counter"; + + @Singleton + MetricsStore store; + + @GET + @Produces("text/plain") + @Timed(value = TIMER_NAME, description = TIMER_DESCRIPTION, histogram = true) + @Path("timed") + public String getTimedMessage() { + return CLICHED_MESSAGE; + } + + @GET + @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) + @Produces(MediaType.TEXT_PLAIN) + @Path("counted") + public String getCounterMessage() { + return "Requests to this method are counted. Use /metrics to see more"; + } + +} 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 60e9194d8e..49d097e846 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 @@ -2,22 +2,16 @@ * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + * SPDX-License-Identifier: BSD-3-Clause */ package org.glassfish.jersey.examples.micrometer; +import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import javax.ws.rs.GET; @@ -28,10 +22,11 @@ @Path("metrics") public class MetricsResource { - private final MeterRegistry registry; + private MetricsStore store; + - public MetricsResource(MeterRegistry registry) { - this.registry = registry; + public MetricsResource(MetricsStore store) { + this.store = store; } @GET @@ -40,33 +35,39 @@ public String getMeters() { final StringBuffer result = new StringBuffer(); try { result.append("Listing available meters: "); - for (final Meter meter : registry.getMeters()) { + for (final Meter meter : store.getRegistry().getMeters()) { result.append(meter.getId().getName()); - result.append("; "); + result.append(";\n\r "); } } catch (Exception ex) { - System.out.println(ex); - result.append("Exception occured, see log for details..."); - result.append(ex.toString()); + result.append("Looks like there are no proper metrics."); + result.append("\n\r"); + result.append("Please visit /measure/timed and /measure/counted first "); + result.append(ex); } - return result.toString(); - } - @GET - @Path("metrics") - @Produces("text/plain") - public String getMetrics() { - final StringBuffer result = new StringBuffer(); - try { - final Timer timer = registry.get("http.shared.metrics") - .tags("method", "GET", "uri", "/micro/meter", "status", "200", "exception", "None", "outcome", "SUCCESS") - .timer(); - result.append(String.format("Overall requests counts: %d, total time (millis): %f", - timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); - } catch (Exception ex) { - System.out.println(ex); - result.append("Exception occured, see log for details..."); - result.append(ex.toString()); + 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") + .timer(); + + result.append(String.format("Overall requests counts: %d, total time (millis): %f \n\r", + timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); + + final Timer annotatedTimer = store.getRegistry().timer(MeasuredResource.TIMER_NAME); + + result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r", + annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); + + final Counter counter = store.getRegistry().counter(MeasuredResource.COUNTER_NAME); + + result.append(String.format("Requests to 'measure/counted' counts: %f \n\r", + counter.count())); + } catch (Exception ex) { + result.append("Exception occurred, see log for details..."); + result.append(ex); + } } return result.toString(); } -} +} \ No newline at end of file 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 new file mode 100644 index 0000000000..04484ebb64 --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResourceConfig.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.micrometer; + +import org.glassfish.jersey.server.ResourceConfig; + +import javax.ws.rs.ApplicationPath; + +@ApplicationPath("/*") +public class MetricsResourceConfig extends ResourceConfig { + + private final MetricsStore store = new MetricsStore(); + + public MetricsResourceConfig() { + store.timedAspect(); + register(MeasuredResource.class); + register(new MetricsResource(store)); + register(store.getMetricsApplicationEventListener()); + } + + public MetricsStore getStore() { + return store; + } +} 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 new file mode 100644 index 0000000000..f819c0b7d7 --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsStore.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.micrometer; + +import io.micrometer.core.aop.TimedAspect; +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; + +public class MetricsStore { + + public static final String REGISTRY_NAME = "http.shared.metrics"; + private final MetricsApplicationEventListener metricsApplicationEventListener; + private final MeterRegistry registry = new SimpleMeterRegistry(); + + public MetricsStore() { + metricsApplicationEventListener = new MetricsApplicationEventListener(registry, + new DefaultJerseyTagsProvider(), + REGISTRY_NAME, true); + } + + public MetricsApplicationEventListener getMetricsApplicationEventListener() { + return metricsApplicationEventListener; + } + + public MeterRegistry getRegistry() { + return registry; + } + + + public TimedAspect timedAspect() { + return new TimedAspect(registry); + } +} diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java deleted file mode 100644 index 7ff108337a..0000000000 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 - */ - -package org.glassfish.jersey.examples.micrometer; - -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; - -@Path("meter") -public class MicrometerResource { - public static final String CLICHED_MESSAGE = "Hello World!"; - - @GET - @Produces("text/plain") - public String getHello() { - return CLICHED_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 4a5f4eea6e..46a81f5644 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 @@ -2,63 +2,52 @@ * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + * SPDX-License-Identifier: BSD-3-Clause */ package org.glassfish.jersey.examples.micrometer; -import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProvider; -import org.glassfish.jersey.micrometer.server.MetricsApplicationEventListener; import org.glassfish.jersey.test.JerseyTest; -import org.glassfish.jersey.server.ResourceConfig; import org.junit.jupiter.api.Test; import javax.ws.rs.core.Application; - import java.util.concurrent.TimeUnit; -import static org.glassfish.jersey.examples.micrometer.MicrometerResource.CLICHED_MESSAGE; +import static org.glassfish.jersey.examples.micrometer.MeasuredResource.CLICHED_MESSAGE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; public class MicrometerTest extends JerseyTest { - static final String TIMER_METRIC_NAME = "http.server.requests"; + static final int REQUESTS_COUNT = 10; - MeterRegistry registry; + private MetricsResourceConfig resourceConfig; @Override protected Application configure() { - registry = new SimpleMeterRegistry(); - MetricsApplicationEventListener metricsListener = new MetricsApplicationEventListener(registry, - new DefaultJerseyTagsProvider(), TIMER_METRIC_NAME, true); - return new ResourceConfig(MicrometerResource.class) - .register(metricsListener) - .register(new MetricsResource(registry)); + resourceConfig = new MetricsResourceConfig(); + assertNotNull(this.resourceConfig); + return this.resourceConfig; } @Test void meterResourceTest() throws InterruptedException { - String response = target("/meter").request().get(String.class); + final String response = target("/measure/timed").request().get(String.class); assertEquals(response, CLICHED_MESSAGE); + for (int i = 0; i < REQUESTS_COUNT; i++) { + target("/metrics").request().get(String.class); + } // Jersey metrics are recorded asynchronously to the request completing Thread.sleep(10); - Timer timer = registry.get(TIMER_METRIC_NAME) - .tags("method", "GET", "uri", "/meter", "status", "200", "exception", "None", "outcome", "SUCCESS") + Timer timer = resourceConfig.getStore().getRegistry() + .get(MetricsStore.REGISTRY_NAME) + .tags("method", "GET", "uri", "/metrics", "status", "200", "exception", "None", "outcome", "SUCCESS") .timer(); - assertEquals(timer.count(), 1); + assertEquals(REQUESTS_COUNT, timer.count()); assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS)); } diff --git a/ext/microprofile/pom.xml b/ext/microprofile/pom.xml index cccbb1779c..d887cb8884 100644 --- a/ext/microprofile/pom.xml +++ b/ext/microprofile/pom.xml @@ -33,8 +33,7 @@ mp-rest-client - mp-config + mp-config - diff --git a/pom.xml b/pom.xml index e0c676ae62..58abf82f3e 100644 --- a/pom.xml +++ b/pom.xml @@ -1171,7 +1171,7 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 2.7 + 3.4.5 @@ -2323,7 +2323,7 @@ 2.3.6 1.2.7 1.3.3 - 1.10.10 + 1.10.11 1.0.9 2.0.1 2.0 From ecd71d15d6a8f04e45a6472ff826564de33545cd Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Mon, 9 Oct 2023 16:19:06 +0200 Subject: [PATCH 02/11] example extended for Micrometer integration Signed-off-by: Maxim Nesen --- .../jersey/examples/micrometer/MetricsResource.java | 4 +++- .../jersey/examples/micrometer/MetricsResourceConfig.java | 3 +-- .../jersey/examples/micrometer/MetricsStore.java | 8 +------- 3 files changed, 5 insertions(+), 10 deletions(-) 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 49d097e846..d9ad96586e 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 @@ -54,7 +54,9 @@ public String getMeters() { result.append(String.format("Overall requests counts: %d, total time (millis): %f \n\r", timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); - final Timer annotatedTimer = store.getRegistry().timer(MeasuredResource.TIMER_NAME); + final Timer annotatedTimer = store.getRegistry().timer(MeasuredResource.TIMER_NAME, + "method", "GET", "status", "200", "exception", "None", + "outcome", "SUCCESS", "uri", "/micro/measure/timed"); result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r", annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); 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 04484ebb64..67a30cd1c6 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 @@ -20,10 +20,9 @@ public class MetricsResourceConfig extends ResourceConfig { private final MetricsStore store = new MetricsStore(); public MetricsResourceConfig() { - store.timedAspect(); + register(store.getMetricsApplicationEventListener()); register(MeasuredResource.class); register(new MetricsResource(store)); - register(store.getMetricsApplicationEventListener()); } 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 f819c0b7d7..42e7030bc5 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 @@ -10,7 +10,6 @@ package org.glassfish.jersey.examples.micrometer; -import io.micrometer.core.aop.TimedAspect; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.jersey.server.DefaultJerseyTagsProvider; import io.micrometer.core.instrument.binder.jersey.server.MetricsApplicationEventListener; @@ -35,9 +34,4 @@ public MetricsApplicationEventListener getMetricsApplicationEventListener() { public MeterRegistry getRegistry() { return registry; } - - - public TimedAspect timedAspect() { - return new TimedAspect(registry); - } -} +} \ No newline at end of file From b1d2ae1c8306c534b85955ccfeab86195dd82ddc Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Tue, 10 Oct 2023 10:25:53 +0200 Subject: [PATCH 03/11] example extended for Micrometer integration Signed-off-by: Maxim Nesen --- examples/micrometer/pom.xml | 5 --- .../micrometer/MeasuredCountedResource.java | 36 +++++++++++++++++++ ...source.java => MeasuredTimedResource.java} | 20 +---------- .../examples/micrometer/MetricsResource.java | 8 +++-- .../micrometer/MetricsResourceConfig.java | 3 +- .../examples/micrometer/MicrometerTest.java | 2 +- pom.xml | 2 +- 7 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java rename examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/{MeasuredResource.java => MeasuredTimedResource.java} (62%) diff --git a/examples/micrometer/pom.xml b/examples/micrometer/pom.xml index edc5e91022..c2329fbca3 100644 --- a/examples/micrometer/pom.xml +++ b/examples/micrometer/pom.xml @@ -57,11 +57,6 @@ junit-jupiter-api test
- - org.aspectj - aspectjweaver - ${aspectj.weaver.version} - diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java new file mode 100644 index 0000000000..a39d719eb2 --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.micrometer; + +import io.micrometer.core.annotation.Counted; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("measure") +public class MeasuredCountedResource { + + public static final String CLICHED_MESSAGE = "Requests to this method are counted. Use /metrics to see more"; + public static final String COUNTER_NAME = "http.counters"; + public static final String COUNTER_DESCRIPTION = "resource measurement counter"; + + @GET + //unfortunately the @counted annotation is not supported for the Jersey Integration. + @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) + @Produces(MediaType.TEXT_PLAIN) + @Path("counted") + public String getCounterMessage() { + return CLICHED_MESSAGE; + } + +} diff --git a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java similarity index 62% rename from examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java rename to examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java index f3221b4374..b771622add 100644 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredResource.java +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java @@ -10,27 +10,18 @@ package org.glassfish.jersey.examples.micrometer; -import io.micrometer.core.annotation.Counted; import io.micrometer.core.annotation.Timed; -import javax.inject.Singleton; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; @Path("measure") -public class MeasuredResource { +public class MeasuredTimedResource { public static final String CLICHED_MESSAGE = "Requests to this method are measured. Use /metrics to see more"; - public static final String TIMER_NAME = "http.timers"; - public static final String COUNTER_NAME = "http.counters"; public static final String TIMER_DESCRIPTION = "resource measurement timer"; - public static final String COUNTER_DESCRIPTION = "resource measurement counter"; - - @Singleton - MetricsStore store; @GET @Produces("text/plain") @@ -39,13 +30,4 @@ public class MeasuredResource { public String getTimedMessage() { return CLICHED_MESSAGE; } - - @GET - @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) - @Produces(MediaType.TEXT_PLAIN) - @Path("counted") - public String getCounterMessage() { - return "Requests to this method are counted. Use /metrics to see more"; - } - } 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 d9ad96586e..f98275e485 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 @@ -54,14 +54,18 @@ public String getMeters() { result.append(String.format("Overall requests counts: %d, total time (millis): %f \n\r", timer.count(), timer.totalTime(TimeUnit.MILLISECONDS))); - final Timer annotatedTimer = store.getRegistry().timer(MeasuredResource.TIMER_NAME, + final Timer annotatedTimer = store.getRegistry().timer(MeasuredTimedResource.TIMER_NAME, "method", "GET", "status", "200", "exception", "None", "outcome", "SUCCESS", "uri", "/micro/measure/timed"); result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r", annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); - final Counter counter = store.getRegistry().counter(MeasuredResource.COUNTER_NAME); + //unfortunately the @counted annotation is not supported for the Jersey Integration. + //so, it always shows 0.0 as count() result. + final Counter counter = store.getRegistry().counter(MeasuredCountedResource.COUNTER_NAME, + "method", "GET", "status", "200", "exception", "None", + "outcome", "SUCCESS", "uri", "/micro/measure/counted"); result.append(String.format("Requests to 'measure/counted' counts: %f \n\r", counter.count())); 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 67a30cd1c6..d3968575b4 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 @@ -21,7 +21,8 @@ public class MetricsResourceConfig extends ResourceConfig { public MetricsResourceConfig() { register(store.getMetricsApplicationEventListener()); - register(MeasuredResource.class); + register(MeasuredTimedResource.class); + register(MeasuredCountedResource.class); register(new MetricsResource(store)); } 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 46a81f5644..04678454fb 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.MeasuredResource.CLICHED_MESSAGE; +import static org.glassfish.jersey.examples.micrometer.MeasuredTimedResource.CLICHED_MESSAGE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; diff --git a/pom.xml b/pom.xml index 58abf82f3e..c93b8ac23a 100644 --- a/pom.xml +++ b/pom.xml @@ -2323,7 +2323,7 @@ 2.3.6 1.2.7 1.3.3 - 1.10.11 + 1.10.12 1.0.9 2.0.1 2.0 From 429ef1a76f676b7ec1ce34359a7ccc965d118218 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 11 Oct 2023 14:35:04 +0200 Subject: [PATCH 04/11] example extended for Micrometer integration Signed-off-by: Maxim Nesen --- examples/micrometer/README.MD | 4 --- .../jersey/examples/micrometer/App.java | 4 +-- .../micrometer/MeasuredCountedResource.java | 36 ------------------- .../examples/micrometer/MetricsResource.java | 9 ----- .../micrometer/MetricsResourceConfig.java | 1 - 5 files changed, 2 insertions(+), 52 deletions(-) delete mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java diff --git a/examples/micrometer/README.MD b/examples/micrometer/README.MD index 4747f9e902..074f9dc05a 100644 --- a/examples/micrometer/README.MD +++ b/examples/micrometer/README.MD @@ -19,7 +19,6 @@ The mapping of the URI path space is presented in the following table: URI path | Resource class | HTTP methods ------------------------------------------ | ------------------------- | -------------- **_/micro/measure/timed_** | JerseyResource | GET -**_/micro/measure/counted_** | JerseyResource | GET **_/micro/metrics_** | JerseyResource | GET Sample Response @@ -28,8 +27,6 @@ Sample Response ```html --- (micro/measure/timed) Requests to this method are measured. Use /metrics to see more ---- (micro/measure/counted) -Requests to this method are counted. Use /metrics to see more ---- (micro/metrics) Listing available meters: http.shared.metrics; ----) @@ -47,7 +44,6 @@ Run the example using [Grizzly](https://javaee.github.io/grizzly/) container as - - 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 2ccb769169..3956612298 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 @@ -39,9 +39,9 @@ public void run() { server.start(); System.out.println(String.format("Application started.\nTry out %s%s\n" - + "After several requests go to %s%s\nAnd after that go to the %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, ROOT_PATH + "/counted", BASE_URI, "metrics")); + BASE_URI, ROOT_PATH + "/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/MeasuredCountedResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java deleted file mode 100644 index a39d719eb2..0000000000 --- a/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredCountedResource.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Distribution License v. 1.0, which is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -package org.glassfish.jersey.examples.micrometer; - -import io.micrometer.core.annotation.Counted; - -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -@Path("measure") -public class MeasuredCountedResource { - - public static final String CLICHED_MESSAGE = "Requests to this method are counted. Use /metrics to see more"; - public static final String COUNTER_NAME = "http.counters"; - public static final String COUNTER_DESCRIPTION = "resource measurement counter"; - - @GET - //unfortunately the @counted annotation is not supported for the Jersey Integration. - @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) - @Produces(MediaType.TEXT_PLAIN) - @Path("counted") - public String getCounterMessage() { - return CLICHED_MESSAGE; - } - -} 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 f98275e485..ac2a6e812d 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,7 +10,6 @@ package org.glassfish.jersey.examples.micrometer; -import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Timer; @@ -61,14 +60,6 @@ public String getMeters() { result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r", annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); - //unfortunately the @counted annotation is not supported for the Jersey Integration. - //so, it always shows 0.0 as count() result. - final Counter counter = store.getRegistry().counter(MeasuredCountedResource.COUNTER_NAME, - "method", "GET", "status", "200", "exception", "None", - "outcome", "SUCCESS", "uri", "/micro/measure/counted"); - - result.append(String.format("Requests to 'measure/counted' counts: %f \n\r", - counter.count())); } catch (Exception ex) { result.append("Exception occurred, see log for details..."); result.append(ex); 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 d3968575b4..1afc1caf76 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 @@ -22,7 +22,6 @@ public class MetricsResourceConfig extends ResourceConfig { public MetricsResourceConfig() { register(store.getMetricsApplicationEventListener()); register(MeasuredTimedResource.class); - register(MeasuredCountedResource.class); register(new MetricsResource(store)); } From 9f0435216721731d5c62eaf161c1cbd5b0ee7535 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 11 Oct 2023 15:17:33 +0200 Subject: [PATCH 05/11] alignment Signed-off-by: Maxim Nesen --- ext/microprofile/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/microprofile/pom.xml b/ext/microprofile/pom.xml index d887cb8884..647b7b3521 100644 --- a/ext/microprofile/pom.xml +++ b/ext/microprofile/pom.xml @@ -33,7 +33,7 @@ mp-rest-client - mp-config + mp-config - + \ No newline at end of file From fdcfea54c32932ee7bea062ac73d92ccd8beb167 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Tue, 17 Oct 2023 11:27:33 +0200 Subject: [PATCH 06/11] alignment Signed-off-by: Maxim Nesen --- docs/src/main/docbook/micrometer.xml | 49 ++++++++----------- docs/src/main/docbook/user-guide.xml | 2 +- .../micrometer/MeasuredTimedResource.java | 4 +- .../examples/micrometer/MicrometerTest.java | 4 +- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/docs/src/main/docbook/micrometer.xml b/docs/src/main/docbook/micrometer.xml index cdcfca19f7..a496f14339 100644 --- a/docs/src/main/docbook/micrometer.xml +++ b/docs/src/main/docbook/micrometer.xml @@ -30,32 +30,26 @@ Micrometer - application observability facade The chapter is about Micrometer integration into Jersey which comes since the version 2.41 as an extension module. - Before Jersey 2.41 it was possible to integrate Micrometer with Jersey using directly µmeter.jersey.link; - There is also support for Jakarta EE 10 integration. Detailed documentation regarding metrics fine-tuning - can be found at the µmeter.link; + Before Jersey 2.41, it was possible to integrate Micrometer with Jersey using directly µmeter.jersey.link;. + There is also support for Jakarta EE 10 integration. The detailed documentation regarding metrics fine-tuning + can be found at the µmeter.link;.
Integration into Jersey Since Jersey 2.41 it's possibly to use an extension module in order to use Micrometer instrumentation inside your projects. The module shall be added as a dependency: - - <dependency> - <groupId>org.glassfish.jersey.ext.micrometer</groupId> - <artifactId>jersey-micrometer</artifactId> - <version>&version;</scope> - </dependency> - + <dependency> +<groupId>org.glassfish.jersey.ext.micrometer</groupId> +<artifactId>jersey-micrometer</artifactId> +<version>&version;</scope> +</dependency> After the dependency is added, the Micrometer can be configured as follows: - - final ResourceConfig resourceConfig = new ResourceConfig(); - resourceConfig.register(new MetricsApplicationEventListener( + final ResourceConfig resourceConfig = new ResourceConfig(); +resourceConfig.register(new MetricsApplicationEventListener( registry, - new DefaultJerseyTagsProvider(), - "http.shared.metrics", - true)); - final ServletContainer servletContainer = new ServletContainer(resourceConfig); - +new DefaultJerseyTagsProvider(), "http.shared.metrics", true)); +final ServletContainer servletContainer = new ServletContainer(resourceConfig); the registry instance is of type MeterRegistry which could be new SimpleMeterRegistry();. Then all metrics can be accessed like registry.get("http.shared.metrics"). The "http.shared.metrics" string @@ -72,21 +66,20 @@ Implementing resource methods, which should be measured, several annotations can be used. The basic example - demonstrates the @Counted annotation. + demonstrates the @Counted annotation. Annotated Micrometer resource methods - - @GET - @Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) - @Produces(MediaType.TEXT_PLAIN) - @Path("counted") - public String getCounterMessage() { - return "Requests to this method are counted. Use /metrics to see more"; - } + @GET +@Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION) +@Produces(MediaType.TEXT_PLAIN) +@Path("counted") +public String getCounterMessage() { + return "Requests to this method are counted. Use /metrics to see more"; +} Metrics however can be introduced using another annotations @Timed, or - @TimedSet which is set of @Timed. + @TimedSet which is a set of @Timed.
\ No newline at end of file diff --git a/docs/src/main/docbook/user-guide.xml b/docs/src/main/docbook/user-guide.xml index 2973292c38..7d412441d4 100644 --- a/docs/src/main/docbook/user-guide.xml +++ b/docs/src/main/docbook/user-guide.xml @@ -104,8 +104,8 @@ This line fits the page width. - + 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/MeasuredTimedResource.java index b771622add..508e80a245 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/MeasuredTimedResource.java @@ -19,7 +19,7 @@ @Path("measure") public class MeasuredTimedResource { - public static final String CLICHED_MESSAGE = "Requests to this method are measured. Use /metrics to see more"; + public static final String MESSAGE = "Requests to this method are measured. Use /metrics to see more"; public static final String TIMER_NAME = "http.timers"; public static final String TIMER_DESCRIPTION = "resource measurement timer"; @@ -28,6 +28,6 @@ public class MeasuredTimedResource { @Timed(value = TIMER_NAME, description = TIMER_DESCRIPTION, histogram = true) @Path("timed") public String getTimedMessage() { - return CLICHED_MESSAGE; + 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 04678454fb..3a54c6fb9a 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.CLICHED_MESSAGE; +import static org.glassfish.jersey.examples.micrometer.MeasuredTimedResource.MESSAGE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -37,7 +37,7 @@ protected Application configure() { @Test void meterResourceTest() throws InterruptedException { final String response = target("/measure/timed").request().get(String.class); - assertEquals(response, CLICHED_MESSAGE); + assertEquals(response, MESSAGE); for (int i = 0; i < REQUESTS_COUNT; i++) { target("/metrics").request().get(String.class); } From 954668bf67f3ce7fee6b398b4dab9621131d5304 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Tue, 17 Oct 2023 11:33:36 +0200 Subject: [PATCH 07/11] alignment Signed-off-by: Maxim Nesen --- docs/src/main/docbook/micrometer.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/src/main/docbook/micrometer.xml b/docs/src/main/docbook/micrometer.xml index a496f14339..f09375eed7 100644 --- a/docs/src/main/docbook/micrometer.xml +++ b/docs/src/main/docbook/micrometer.xml @@ -40,15 +40,15 @@ Since Jersey 2.41 it's possibly to use an extension module in order to use Micrometer instrumentation inside your projects. The module shall be added as a dependency: <dependency> -<groupId>org.glassfish.jersey.ext.micrometer</groupId> -<artifactId>jersey-micrometer</artifactId> -<version>&version;</scope> + <groupId>org.glassfish.jersey.ext.micrometer</groupId> + <artifactId>jersey-micrometer</artifactId> + <version>&version;</scope> </dependency> After the dependency is added, the Micrometer can be configured as follows: final ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.register(new MetricsApplicationEventListener( registry, -new DefaultJerseyTagsProvider(), "http.shared.metrics", true)); + new DefaultJerseyTagsProvider(), "http.shared.metrics", true)); final ServletContainer servletContainer = new ServletContainer(resourceConfig); the registry instance is of type MeterRegistry which could be new SimpleMeterRegistry();. Then all metrics can be accessed like From bc912c064da9172310dc88013ed12002823d227f Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Tue, 17 Oct 2023 15:17:59 +0200 Subject: [PATCH 08/11] Micrometer example improvement Signed-off-by: Maxim Nesen --- .../glassfish/jersey/examples/micrometer/App.java | 1 + .../examples/micrometer/MeasuredTimedResource.java | 7 +++++-- .../jersey/examples/micrometer/MetricsResource.java | 13 ++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) 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 3956612298..8401e0dc34 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 @@ -22,6 +22,7 @@ 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/metrics/"; public static void main(String[] args) { try { 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/MeasuredTimedResource.java index 508e80a245..d49d782bf8 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/MeasuredTimedResource.java @@ -16,15 +16,18 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; +import static org.glassfish.jersey.examples.micrometer.App.WEB_PATH; + @Path("measure") public class MeasuredTimedResource { - public static final String MESSAGE = "Requests to this method are measured. Use /metrics to see more"; + public static final String MESSAGE = "Requests to this method are measured. " + + "Use /metrics to see moreStatic meters are initialized, try extendedMeters"; + } + + + @GET + @Produces("text/plain") + @Path("extendedMeters") + public String getExtendedMeters() { final StringBuffer result = new StringBuffer(); try { result.append("Listing available meters: "); From 02334641c3e5ff0e329aa7c56fa3966397b9f733 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 18 Oct 2023 07:48:28 +0200 Subject: [PATCH 09/11] Micrometer example improvement Signed-off-by: Maxim Nesen --- .../jersey/examples/micrometer/App.java | 4 +- .../micrometer/MeasuredTimedResource.java | 2 +- .../examples/micrometer/MetricsResource.java | 54 +------------- .../micrometer/MetricsResourceConfig.java | 3 +- .../examples/micrometer/SummaryResource.java | 73 +++++++++++++++++++ .../examples/micrometer/MicrometerTest.java | 4 +- 6 files changed, 83 insertions(+), 57 deletions(-) create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java 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 8401e0dc34..f7c0c13a21 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 @@ -22,7 +22,7 @@ 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/metrics/"; + public static final String WEB_PATH = "/micro/"; public static void main(String[] args) { try { @@ -42,7 +42,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, "metrics")); + BASE_URI, ROOT_PATH + "/timed", BASE_URI, "init")); 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/MeasuredTimedResource.java b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MeasuredTimedResource.java index d49d782bf8..25f70280b9 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/MeasuredTimedResource.java @@ -22,7 +22,7 @@ public class MeasuredTimedResource { public static final String MESSAGE = "Requests to this method are measured. " - + "Use /metrics to see more/init to see moreStatic meters are initialized, try extendedMeters"; - } - - - @GET - @Produces("text/plain") - @Path("extendedMeters") - public String getExtendedMeters() { - final StringBuffer result = new StringBuffer(); - try { - result.append("Listing available meters: "); - 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 and /measure/counted first "); - result.append(ex); - } - 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") - .timer(); - - result.append(String.format("Overall requests counts: %d, total time (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"); - - result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r", - annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS))); - - } catch (Exception ex) { - result.append("Exception occurred, see log for details..."); - result.append(ex); - } - } - return result.toString(); + + WEB_PATH + "summary\">summary. If you wan more measurements just refresh this page several times." + + ""; } } \ No newline at end of file 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 1afc1caf76..8fdf1e0b50 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 @@ -22,7 +22,8 @@ public class MetricsResourceConfig extends ResourceConfig { public MetricsResourceConfig() { register(store.getMetricsApplicationEventListener()); register(MeasuredTimedResource.class); - register(new MetricsResource(store)); + register(MetricsResource.class); + register(new SummaryResource(store)); } public MetricsStore getStore() { 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 new file mode 100644 index 0000000000..bf2e684a8c --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/SummaryResource.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +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; + +@Path("summary") +public class SummaryResource { + + private final MetricsStore store; + + public SummaryResource(MetricsStore store) { + this.store = store; + } + + @GET + @Produces("text/html") + 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" + + " which could be used with different tags," + + " 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); + } + 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(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))); + + final Timer annotatedTimer = store.getRegistry().timer(MeasuredTimedResource.TIMER_NAME, + "method", "GET", "status", "200", "exception", "None", + "outcome", "SUCCESS", "uri", "/micro/measure/timed"); + + result.append(String.format("Requests to 'measure/timed' counts: %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); + } + } + return result.append("").toString(); + } +} 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 3a54c6fb9a..af752bb8e6 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 @@ -39,13 +39,13 @@ void meterResourceTest() throws InterruptedException { final String response = target("/measure/timed").request().get(String.class); assertEquals(response, MESSAGE); for (int i = 0; i < REQUESTS_COUNT; i++) { - target("/metrics").request().get(String.class); + target("/init").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", "/metrics", "status", "200", "exception", "None", "outcome", "SUCCESS") + .tags("method", "GET", "uri", "/init", "status", "200", "exception", "None", "outcome", "SUCCESS") .timer(); assertEquals(REQUESTS_COUNT, timer.count()); assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS)); From 5af43c496066faae1eebf8a7b1b1dd1fd637ed62 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 18 Oct 2023 11:04:45 +0200 Subject: [PATCH 10/11] Typos fix Signed-off-by: Maxim Nesen --- examples/micrometer/README.MD | 26 ++++++++++++------- .../examples/micrometer/MetricsResource.java | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/examples/micrometer/README.MD b/examples/micrometer/README.MD index 074f9dc05a..a1baad93c0 100644 --- a/examples/micrometer/README.MD +++ b/examples/micrometer/README.MD @@ -18,19 +18,25 @@ The mapping of the URI path space is presented in the following table: URI path | Resource class | HTTP methods ------------------------------------------ | ------------------------- | -------------- -**_/micro/measure/timed_** | JerseyResource | GET -**_/micro/metrics_** | JerseyResource | GET +**_/micro/measure/timed_** | MeasuredTimedResource | GET +**_/micro/init_** | MetricsResource | GET +**_/micro/summary_** | SummaryResource | GET Sample Response --------------- ```html --- (micro/measure/timed) -Requests to this method are measured. Use /metrics to see more ----- (micro/metrics) -Listing available meters: http.shared.metrics; -----) -Overall requests counts: 9, total time (millis): 35.799483 +Requests to this method are measured. Use /init to see more +---- (micro/init) +Static meters are initialized, try summary. If you want more measurements just refresh this page several times. +---- (micro/summary) +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 : +http.timers; +http.shared.metrics; +Counts to the init page: 2, time spent on requests to the init page (millis): 2.759025 +Requests to 'measure/timed' counts: 2, total time (millis): 40.110161 ``` @@ -41,10 +47,10 @@ 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 -- - +- - and see statistics for the micro/meter page \ No newline at end of file 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 444a52816b..d574b841b2 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 @@ -27,7 +27,7 @@ public class MetricsResource { @Produces("text/html") public String getMeters() { return "Static meters are initialized, try summary. If you wan more measurements just refresh this page several times." + + WEB_PATH + "summary\">summary. If you want more measurements just refresh this page several times." + ""; } } \ No newline at end of file From 2e137206839475dfc11e243d9312f2280638f61d Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 18 Oct 2023 13:53:04 +0200 Subject: [PATCH 11/11] Typos fix Signed-off-by: Maxim Nesen --- examples/micrometer/README.MD | 12 ++-- .../jersey/examples/micrometer/App.java | 3 +- .../examples/micrometer/MetricsResource.java | 8 +-- .../micrometer/MetricsResourceConfig.java | 13 +++- .../examples/micrometer/MetricsStore.java | 4 +- .../examples/micrometer/SummaryResource.java | 61 ++++++++++--------- ...dTimedResource.java => TimedResource.java} | 10 +-- .../examples/micrometer/MicrometerTest.java | 8 +-- 8 files changed, 63 insertions(+), 56 deletions(-) rename examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/{MeasuredTimedResource.java => TimedResource.java} (70%) 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));