From 4b26f1a6442a118266a180d50b9107f9d3bdeb55 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 27 Sep 2023 14:07:55 +0200 Subject: [PATCH] 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 50fb94d8a5a..3fd55ea3cdd 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 4f7621a9c7b..5886dfe7940 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 85e9fd8af63..6f2be1c8418 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 93aabcfb87c..4c6d3b257a7 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 00000000000..cdcfca19f70 --- /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 7fa60b42c16..6f029df9a5d 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 799478d6775..2973292c389 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 92dcf22e85e..2ccb769169a 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 00000000000..f3221b4374f --- /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 60e9194d8e9..49d097e846f 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 00000000000..04484ebb640 --- /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 00000000000..f819c0b7d78 --- /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 7ff108337a2..00000000000 --- 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 4a5f4eea6ed..46a81f56448 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 cccbb1779c3..d887cb8884d 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 37579bb77b7..4b34561a00d 100644 --- a/pom.xml +++ b/pom.xml @@ -1166,7 +1166,7 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 2.7 + 3.4.5 @@ -2318,7 +2318,7 @@ 2.3.6 1.2.7 1.3.3 - 1.10.10 + 1.10.11 1.0.9 2.0.1 2.0