From fffa371ffd0ac8c18163ab858ad604161273a250 Mon Sep 17 00:00:00 2001 From: ivanjunckes Date: Mon, 19 Nov 2018 10:00:29 -0200 Subject: [PATCH 1/5] metrics example creation --- examples/pom.xml | 1 + examples/rest-mp-metrics/pom.xml | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 examples/rest-mp-metrics/pom.xml diff --git a/examples/pom.xml b/examples/pom.xml index 18d49712d06..4cab9e29ea9 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -171,6 +171,7 @@ BROKEN, see TOMEE-2140 moviefun moviefun-rest resources-jmx-example + rest-mp-metrics diff --git a/examples/rest-mp-metrics/pom.xml b/examples/rest-mp-metrics/pom.xml new file mode 100644 index 00000000000..6a31d779a42 --- /dev/null +++ b/examples/rest-mp-metrics/pom.xml @@ -0,0 +1,15 @@ + + + + examples + org.apache.tomee + 8.0.0-SNAPSHOT + + 4.0.0 + + rest-mp-metrics + + + \ No newline at end of file From cd09707502f35f2b0154ec8b1062d35a2b516bed Mon Sep 17 00:00:00 2001 From: ivanjunckes Date: Mon, 19 Nov 2018 10:04:18 -0200 Subject: [PATCH 2/5] Adding rest service --- .../org/superbiz/rest/GreetingService.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java diff --git a/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java b/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java new file mode 100644 index 00000000000..fa84c6914f9 --- /dev/null +++ b/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java @@ -0,0 +1,21 @@ +package org.superbiz.rest; + +import org.eclipse.microprofile.metrics.annotation.Counted; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/greeting") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class GreetingService { + + @Counted(monotonic = true, name = "count_messages") + @GET + public String message() { + return "Hi Microprofile Metrics!"; + } +} From b85fe05851e208f4744e1b7a7a5b3ea3ca89ed7e Mon Sep 17 00:00:00 2001 From: ivanjunckes Date: Mon, 19 Nov 2018 10:50:30 -0200 Subject: [PATCH 3/5] adding tomee plugin and dependencies --- examples/rest-mp-metrics/pom.xml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/rest-mp-metrics/pom.xml b/examples/rest-mp-metrics/pom.xml index 6a31d779a42..97890db1768 100644 --- a/examples/rest-mp-metrics/pom.xml +++ b/examples/rest-mp-metrics/pom.xml @@ -10,6 +10,34 @@ 4.0.0 rest-mp-metrics + war + + + org.apache.tomee + javaee-api + 8.0 + provided + + + org.eclipse.microprofile.metrics + microprofile-metrics-api + 1.0 + provided + + + + + + org.apache.tomee.maven + tomee-maven-plugin + 8.0.0-SNAPSHOT + + microprofile + ${artifactId} + + + + \ No newline at end of file From 017b98e89ac68e4d5b695e06a16c7a7e43561d20 Mon Sep 17 00:00:00 2001 From: ivanjunckes Date: Mon, 19 Nov 2018 17:42:38 -0200 Subject: [PATCH 4/5] adding test to microprofile metrics --- examples/rest-mp-metrics/pom.xml | 27 ++++++++ .../org/superbiz/rest/GreetingService.java | 5 +- .../superbiz/rest/GreetingServiceTest.java | 61 +++++++++++++++++++ .../src/test/resources/arquillian.xml | 30 +++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 examples/rest-mp-metrics/src/test/java/org/superbiz/rest/GreetingServiceTest.java create mode 100644 examples/rest-mp-metrics/src/test/resources/arquillian.xml diff --git a/examples/rest-mp-metrics/pom.xml b/examples/rest-mp-metrics/pom.xml index 97890db1768..ded0689b7d7 100644 --- a/examples/rest-mp-metrics/pom.xml +++ b/examples/rest-mp-metrics/pom.xml @@ -12,6 +12,7 @@ rest-mp-metrics war + org.apache.tomee @@ -25,6 +26,32 @@ 1.0 provided + + org.apache.tomee + openejb-cxf-rs + ${tomee.version} + test + + + org.jboss.arquillian.junit + arquillian-junit-container + 1.0.3.Final + test + + + org.apache.tomee + arquillian-tomee-remote + ${tomee.version} + test + + + org.apache.tomee + apache-tomee + ${tomee.version} + zip + microprofile + test + diff --git a/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java b/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java index fa84c6914f9..a75087e625b 100644 --- a/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java +++ b/examples/rest-mp-metrics/src/main/java/org/superbiz/rest/GreetingService.java @@ -2,6 +2,7 @@ import org.eclipse.microprofile.metrics.annotation.Counted; +import javax.enterprise.context.ApplicationScoped; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -11,10 +12,12 @@ @Path("/greeting") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) +@ApplicationScoped public class GreetingService { - @Counted(monotonic = true, name = "count_messages") + @Counted(monotonic = true, name = "message_counter", absolute = true) @GET + @Produces(MediaType.TEXT_PLAIN) public String message() { return "Hi Microprofile Metrics!"; } diff --git a/examples/rest-mp-metrics/src/test/java/org/superbiz/rest/GreetingServiceTest.java b/examples/rest-mp-metrics/src/test/java/org/superbiz/rest/GreetingServiceTest.java new file mode 100644 index 00000000000..dff404418f4 --- /dev/null +++ b/examples/rest-mp-metrics/src/test/java/org/superbiz/rest/GreetingServiceTest.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.superbiz.rest; + +import org.apache.cxf.jaxrs.client.WebClient; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ws.rs.core.MediaType; +import java.net.URL; + +import static org.junit.Assert.assertEquals; + +@RunWith(Arquillian.class) +public class GreetingServiceTest { + + @Deployment(testable = false) + public static WebArchive createDeployment() { + final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war") + .addClass(GreetingService.class) + .addAsWebInfResource(new StringAsset(""), "beans.xml"); + return webArchive; + } + + @ArquillianResource + private URL base; + + @Test + public void testPrometheusApplicationMetric() { + final String message = WebClient.create(base.toExternalForm()) + .path("/greeting/") + .get(String.class); + assertEquals("Hi Microprofile Metrics!", message); + + final String metric = WebClient.create(base.toExternalForm()) + .path("/metrics/application/message_counter") + .accept(MediaType.TEXT_PLAIN) + .get(String.class); + assertEquals("# TYPE application:message_counter counter\napplication:message_counter 1.0\n", metric); + } +} diff --git a/examples/rest-mp-metrics/src/test/resources/arquillian.xml b/examples/rest-mp-metrics/src/test/resources/arquillian.xml new file mode 100644 index 00000000000..3029d48c033 --- /dev/null +++ b/examples/rest-mp-metrics/src/test/resources/arquillian.xml @@ -0,0 +1,30 @@ + + + + + + + -1 + -1 + microprofile + target/apache-tomee-remote + target/arquillian-test-working-dir + + + \ No newline at end of file From be6bf8f170361dc8d296ff0a64739341de09a8d2 Mon Sep 17 00:00:00 2001 From: ivanjunckes Date: Mon, 19 Nov 2018 18:11:06 -0200 Subject: [PATCH 5/5] Adding readme --- examples/rest-mp-metrics/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/rest-mp-metrics/README.md diff --git a/examples/rest-mp-metrics/README.md b/examples/rest-mp-metrics/README.md new file mode 100644 index 00000000000..9dafe75c92a --- /dev/null +++ b/examples/rest-mp-metrics/README.md @@ -0,0 +1,19 @@ +# Microprofile Metrics +This is an example on how to use microprofile metrics in TomEE. + +The command to run the application the application is: + + mvn clean install tomee:run + +Make a call to the greeting endpoint: + + http://localhost:8080/rest-mp-metrics/greeting + +Check the metrics in Prometheus format: + + http://localhost:8080/rest-mp-metrics/metrics + + + + +