fixing endpoint and test based on romain's feedback
authorivanjunckes <ijunckes@tomitribe.com>
Fri, 30 Nov 2018 12:05:55 +0000 (10:05 -0200)
committerivanjunckes <ijunckes@tomitribe.com>
Fri, 30 Nov 2018 12:05:55 +0000 (10:05 -0200)
geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java
geronimo-metrics-common/src/test/java/org/apache/geronimo/microprofile/metrics/common/json/JsonMetricTest.java [new file with mode: 0644]

index 559addf..ddda156 100644 (file)
@@ -19,11 +19,13 @@ package org.apache.geronimo.microprofile.metrics.common.jaxrs;
 import static java.util.Collections.emptyMap;
 import static java.util.Collections.singletonMap;
 import static java.util.Optional.ofNullable;
+import static java.util.function.Function.identity;
 import static java.util.stream.Collectors.joining;
 import static java.util.stream.Collectors.toMap;
 
 import java.util.Collections;
 import java.util.Map;
+import java.util.function.Function;
 import java.util.stream.Stream;
 
 import javax.ws.rs.GET;
@@ -133,9 +135,7 @@ public class MetricsEndpoints {
                           @Context final SecurityContext securityContext,
                           @Context final UriInfo uriInfo) {
         securityValidator.checkSecurity(securityContext, uriInfo);
-        return ofNullable(findRegistry(registry).getMetrics().get(name))
-                .map(metric -> singletonMap(name, map(metric)))
-                .orElseGet(Collections::emptyMap);
+        return singleEntry(name, findRegistry(registry), this::map);
     }
 
     @GET
@@ -149,8 +149,7 @@ public class MetricsEndpoints {
         final MetricRegistry metricRegistry = findRegistry(registry);
         return prometheus.toText(
                 metricRegistry, registry,
-                singleEntry(name, metricRegistry))
-                .toString();
+                singleEntry(name, metricRegistry, identity())).toString();
     }
 
     @OPTIONS
@@ -177,9 +176,10 @@ public class MetricsEndpoints {
                 .collect(toMap(Map.Entry::getKey, e -> mapMeta(e.getValue())));
     }
 
-    private Map<String, Metric> singleEntry(final String name, final MetricRegistry metricRegistry) {
+    private <T> Map<String, T> singleEntry(final String name, final MetricRegistry metricRegistry,
+                                           final Function<Metric, T> metricMapper) {
         return ofNullable(metricRegistry.getMetrics().get(name))
-                .map(metric -> singletonMap(name, metric))
+                .map(metric -> singletonMap(name, metricMapper.apply(metric)))
                 .orElseGet(Collections::emptyMap);
     }
 
diff --git a/geronimo-metrics-common/src/test/java/org/apache/geronimo/microprofile/metrics/common/json/JsonMetricTest.java b/geronimo-metrics-common/src/test/java/org/apache/geronimo/microprofile/metrics/common/json/JsonMetricTest.java
new file mode 100644 (file)
index 0000000..3cc552b
--- /dev/null
@@ -0,0 +1,50 @@
+package org.apache.geronimo.microprofile.metrics.common.json;/*
+ * 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.geronimo.microprofile.metrics.common.RegistryImpl;
+import org.apache.geronimo.microprofile.metrics.common.jaxrs.MetricsEndpoints;
+import org.apache.geronimo.microprofile.metrics.common.jaxrs.SecurityValidator;
+import org.apache.geronimo.microprofile.metrics.common.prometheus.PrometheusFormatter;
+import org.eclipse.microprofile.metrics.Gauge;
+import org.junit.Test;
+
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+
+import static java.util.Collections.singletonMap;
+import static org.junit.Assert.assertEquals;
+
+public class JsonMetricTest {
+
+    @Test
+    public void testJsonGaugeValue() {
+        final RegistryImpl registry = new RegistryImpl();
+        registry.register("foo", (Gauge<Long>) () -> 1L);
+
+        final MetricsEndpoints endpoints = new MetricsEndpoints();
+        endpoints.setApplicationRegistry(registry);
+        endpoints.setPrometheus(new PrometheusFormatter());
+        endpoints.setSecurityValidator(new SecurityValidator() {
+            @Override
+            public void checkSecurity(final SecurityContext securityContext, final UriInfo uriInfo) {
+                // no-op
+            }
+        });
+        final Object json = endpoints.getJson("application", "foo", null, null);
+        assertEquals(singletonMap("foo", 1L), json);
+    }
+}