Adding fix for gauge and adding tests
authorivanjunckes <ijunckes@tomitribe.com>
Wed, 28 Nov 2018 20:17:12 +0000 (18:17 -0200)
committerivanjunckes <ijunckes@tomitribe.com>
Wed, 28 Nov 2018 20:17:12 +0000 (18:17 -0200)
geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java
geronimo-metrics-tests/pom.xml [new file with mode: 0644]
geronimo-metrics-tests/src/test/java/GaugeMetricTest.java [new file with mode: 0644]
geronimo-metrics-tests/src/test/java/WeatherResource.java [new file with mode: 0644]
pom.xml

index 4293fd5..559addf 100644 (file)
@@ -133,7 +133,9 @@ public class MetricsEndpoints {
                           @Context final SecurityContext securityContext,
                           @Context final UriInfo uriInfo) {
         securityValidator.checkSecurity(securityContext, uriInfo);
-        return singleEntry(name, findRegistry(registry));
+        return ofNullable(findRegistry(registry).getMetrics().get(name))
+                .map(metric -> singletonMap(name, map(metric)))
+                .orElseGet(Collections::emptyMap);
     }
 
     @GET
diff --git a/geronimo-metrics-tests/pom.xml b/geronimo-metrics-tests/pom.xml
new file mode 100644 (file)
index 0000000..e04f76d
--- /dev/null
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>geronimo-metrics-parent</artifactId>
+        <groupId>org.apache.geronimo</groupId>
+        <version>1.0.1-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>geronimo-metrics-tests</artifactId>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>tomcat-catalina</artifactId>
+            <version>9.0.10</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-jcdi_2.0_spec</artifactId>
+            <version>1.0.1</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo</groupId>
+            <artifactId>geronimo-metrics-extension-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-interceptor_1.2_spec</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-atinject_1.0_spec</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-annotation_1.3_spec</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.meecrowave</groupId>
+            <artifactId>meecrowave-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo</groupId>
+            <artifactId>geronimo-metrics</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/geronimo-metrics-tests/src/test/java/GaugeMetricTest.java b/geronimo-metrics-tests/src/test/java/GaugeMetricTest.java
new file mode 100644 (file)
index 0000000..91fed51
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ */
+
+import org.apache.meecrowave.Meecrowave;
+import org.apache.meecrowave.junit.MeecrowaveRule;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import static org.junit.Assert.assertEquals;
+
+public class GaugeMetricTest {
+
+    private WebTarget target;
+    private Client client;
+
+    @ClassRule
+    public static final MeecrowaveRule MEECROWAVE = new MeecrowaveRule(new Meecrowave.Builder() {{
+        setTomcatNoJmx(false);
+    }}, "");
+
+
+    @Before
+    public void before() {
+        client = ClientBuilder.newBuilder().build();
+        target = client.target("http://" + MEECROWAVE.getConfiguration().getHost() + ":" + MEECROWAVE.getConfiguration().getHttpPort());
+    }
+
+    @After
+    public void after() {
+        client.close();
+    }
+
+    @Test
+    public void testMetricGaugeJson() {
+        Response response = target.path("/weather/temperature").request().get();
+        assertEquals(200, response.getStatus());
+
+        String gauge = target.path("/metrics/application/temperature")
+                .request()
+                .accept(MediaType.APPLICATION_JSON)
+                .get(String.class);
+
+        assertEquals("{\"temperature\":30}", gauge);
+    }
+
+    @Test
+    public void testMetricGaugePrometheus() {
+        Response response = target.path("/weather/temperature").request().get();
+        assertEquals(200, response.getStatus());
+
+        String gauge = target.path("/metrics/application/temperature")
+                .request()
+                .accept(MediaType.TEXT_PLAIN)
+                .get(String.class);
+
+        assertEquals("# TYPE application:temperature_celsius gauge\napplication:temperature_celsius{weather=\"temperature\"} 30.0\n", gauge);
+    }
+}
diff --git a/geronimo-metrics-tests/src/test/java/WeatherResource.java b/geronimo-metrics-tests/src/test/java/WeatherResource.java
new file mode 100644 (file)
index 0000000..3df610e
--- /dev/null
@@ -0,0 +1,20 @@
+import org.eclipse.microprofile.metrics.annotation.Gauge;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+@Path("weather")
+@ApplicationScoped
+public class WeatherResource {
+
+    @Gauge(name = "temperature", absolute = true, unit = "celsius",
+            displayName = "Weather Temperature",
+            description = "This metric shows the temperature.",
+            tags = {"weather=temperature"})
+    @GET
+    @Path("temperature")
+    public Integer temperature() {
+        return 30;
+    }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 832fc69..a881939 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -51,6 +51,7 @@
     <module>geronimo-metrics-common</module>
     <module>geronimo-metrics</module>
     <module>geronimo-metrics-extensions</module>
+    <module>geronimo-metrics-tests</module>
   </modules>
 
   <dependencies>