From fc4a05af652c97f614db9c8fe3d37758512ace23 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 13 Sep 2023 15:50:13 +0200 Subject: [PATCH] Micrometer Example Signed-off-by: Maxim Nesen --- bom/pom.xml | 5 + examples/micrometer/README.MD | 57 +++++++++++ examples/micrometer/pom.xml | 98 +++++++++++++++++++ .../jersey/examples/micrometer/App.java | 67 +++++++++++++ .../examples/micrometer/MetricsResource.java | 72 ++++++++++++++ .../micrometer/MicrometerResource.java | 33 +++++++ .../examples/micrometer/MicrometerTest.java | 65 ++++++++++++ examples/pom.xml | 1 + ext/micrometer/pom.xml | 5 +- .../micrometer/server/AnnotationFinder.java | 2 +- .../DefaultJerseyObservationConvention.java | 2 +- .../server/DefaultJerseyTagsProvider.java | 2 +- .../micrometer/server/JerseyContext.java | 2 +- .../micrometer/server/JerseyKeyValues.java | 2 +- .../server/JerseyObservationConvention.java | 2 +- .../JerseyObservationDocumentation.java | 2 +- .../jersey/micrometer/server/JerseyTags.java | 2 +- .../micrometer/server/JerseyTagsProvider.java | 2 +- .../MetricsApplicationEventListener.java | 2 +- .../server/MetricsRequestEventListener.java | 2 +- .../ObservationApplicationEventListener.java | 2 +- .../ObservationRequestEventListener.java | 2 +- .../jersey/micrometer/server/TimedFinder.java | 2 +- .../micrometer/server/package-info.java | 2 +- .../server/DefaultJerseyTagsProviderTest.java | 2 +- .../MetricsRequestEventListenerTest.java | 2 +- .../MetricsRequestEventListenerTimedTest.java | 2 +- .../exception/ResourceGoneException.java | 2 +- .../mapper/ResourceGoneExceptionMapper.java | 2 +- ...ctObservationRequestEventListenerTest.java | 2 +- ...servationApplicationEventListenerTest.java | 2 +- .../server/resources/TestResource.java | 2 +- .../resources/TimedOnClassResource.java | 2 +- .../server/resources/TimedResource.java | 2 +- ext/spring4/pom.xml | 2 +- ext/spring5/pom.xml | 2 +- pom.xml | 2 + 37 files changed, 430 insertions(+), 29 deletions(-) create mode 100644 examples/micrometer/README.MD create mode 100644 examples/micrometer/pom.xml create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java create mode 100644 examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java create mode 100644 examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java diff --git a/bom/pom.xml b/bom/pom.xml index 2525415c36..7bc29fa2e8 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -153,6 +153,11 @@ jersey-entity-filtering ${project.version} + + org.glassfish.jersey.ext + jersey-micrometer + ${project.version} + org.glassfish.jersey.ext jersey-metainf-services diff --git a/examples/micrometer/README.MD b/examples/micrometer/README.MD new file mode 100644 index 0000000000..609a6ea151 --- /dev/null +++ b/examples/micrometer/README.MD @@ -0,0 +1,57 @@ +[//]: # " 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 " + +jersey-micrometer-webapp +========================================================== + +This example demonstrates basics of Micrometer Jersey integration + +Contents +-------- + +The mapping of the URI path space is presented in the following table: + +URI path | Resource class | HTTP methods +------------------------------------------ | ------------------------- | -------------- +**_/micro/meter_** | JerseyResource | GET +**_/micro/metrics_** | JerseyResource | GET +**_/micro/metrics/metrics_** | JerseyResource | GET + +Sample Response +--------------- + +```javascript +--- (micro/meter) +Hello World! +---- (micro/metrics) +Listing available meters: http.shared.metrics; +---- (micro/metric/metrics) +Overall requests counts: 9, total time (millis): 35.799483 +``` + + +Running the Example +------------------- + +Run the example using [Grizzly](https://javaee.github.io/grizzly/) container as follows: + +> mvn clean compile exec:java + +- +- after few request to the main page go to the url +- - +- and see the list of available meters +- then go to the +- - +- and see statistics for the micro/meter page \ No newline at end of file diff --git a/examples/micrometer/pom.xml b/examples/micrometer/pom.xml new file mode 100644 index 0000000000..df423e1631 --- /dev/null +++ b/examples/micrometer/pom.xml @@ -0,0 +1,98 @@ + + + + 4.0.0 + + + org.glassfish.jersey.examples + project + 2.41-SNAPSHOT + + + jersey-micrometer-webapp + jar + jersey-micrometer-example-webapp + + Micrometer/Jersey metrics basic example + + + + org.glassfish.jersey.containers + jersey-container-grizzly2-http + + + org.glassfish.jersey.containers + jersey-container-servlet + + + org.glassfish.jersey.ext + jersey-micrometer + + + org.glassfish.jersey.inject + jersey-hk2 + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + test + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + test + + + org.junit.jupiter + junit-jupiter-api + test + + + + + + + org.codehaus.mojo + exec-maven-plugin + + org.glassfish.jersey.examples.micrometer.App + + + + + + + + pre-release + + + + org.codehaus.mojo + xml-maven-plugin + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + + 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 new file mode 100644 index 0000000000..92dcf22e85 --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/App.java @@ -0,0 +1,67 @@ +/* + * 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 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 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); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + server.shutdownNow(); + } + })); + 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" + + "Stop the application using CTRL+C", + BASE_URI, ROOT_PATH, BASE_URI, "metrics", BASE_URI, "metrics/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 new file mode 100644 index 0000000000..60e9194d8e --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MetricsResource.java @@ -0,0 +1,72 @@ +/* + * 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 io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.MeterRegistry; +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("metrics") +public class MetricsResource { + + private final MeterRegistry registry; + + public MetricsResource(MeterRegistry registry) { + this.registry = registry; + } + + @GET + @Produces("text/plain") + public String getMeters() { + final StringBuffer result = new StringBuffer(); + try { + result.append("Listing available meters: "); + for (final Meter meter : registry.getMeters()) { + result.append(meter.getId().getName()); + result.append("; "); + } + } catch (Exception ex) { + System.out.println(ex); + result.append("Exception occured, see log for details..."); + result.append(ex.toString()); + } + 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()); + } + return result.toString(); + } +} 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 new file mode 100644 index 0000000000..7ff108337a --- /dev/null +++ b/examples/micrometer/src/main/java/org/glassfish/jersey/examples/micrometer/MicrometerResource.java @@ -0,0 +1,33 @@ +/* + * 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 new file mode 100644 index 0000000000..4a5f4eea6e --- /dev/null +++ b/examples/micrometer/src/test/java/org/glassfish/jersey/examples/micrometer/MicrometerTest.java @@ -0,0 +1,65 @@ +/* + * 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 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.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"; + + MeterRegistry registry; + + @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)); + } + + @Test + void meterResourceTest() throws InterruptedException { + String response = target("/meter").request().get(String.class); + assertEquals(response, CLICHED_MESSAGE); + // 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(); + assertEquals(timer.count(), 1); + assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS)); + } + +} \ No newline at end of file diff --git a/examples/pom.xml b/examples/pom.xml index ba5480b91d..3cf0079764 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -102,6 +102,7 @@ managed-client-simple-webapp multipart-webapp + micrometer open-tracing osgi-helloworld-webapp osgi-http-service diff --git a/ext/micrometer/pom.xml b/ext/micrometer/pom.xml index 5e19969a97..800083ec1d 100644 --- a/ext/micrometer/pom.xml +++ b/ext/micrometer/pom.xml @@ -1,7 +1,7 @@ 9.5 + + 1.6.11 2.13.0