[GitHub] rmannibucau commented on issue #2: GERONIMO-6658 - WIP Fix bug with gauge json

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] rmannibucau commented on issue #2: GERONIMO-6658 - WIP Fix bug with gauge json

GitBox
rmannibucau commented on issue #2: GERONIMO-6658 - WIP Fix bug with gauge json
URL: https://github.com/apache/geronimo-metrics/pull/2#issuecomment-442837720
 
 
   To be concrete, here is the proposal:
   
   
       diff --git a/geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java b/geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java
       index 4293fd5..405cd45 100644
       --- a/geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java
       +++ b/geronimo-metrics-common/src/main/java/org/apache/geronimo/microprofile/metrics/common/jaxrs/MetricsEndpoints.java
       @@ -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,7 +135,7 @@ public class MetricsEndpoints {
                                  @Context final SecurityContext securityContext,
                                  @Context final UriInfo uriInfo) {
                securityValidator.checkSecurity(securityContext, uriInfo);
       -        return singleEntry(name, findRegistry(registry));
       +        return singleEntry(name, findRegistry(registry), this::map);
            }
       
            @GET
       @@ -147,7 +149,7 @@ public class MetricsEndpoints {
                final MetricRegistry metricRegistry = findRegistry(registry);
                return prometheus.toText(
                        metricRegistry, registry,
       -                singleEntry(name, metricRegistry))
       +                singleEntry(name, metricRegistry, identity()))
                        .toString();
            }
       
       @@ -175,9 +177,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);
            }
   
   And for the test:
   
       package org.apache.geronimo.microprofile.metrics.common;
       
       import static java.util.Collections.singletonMap;
       import static org.junit.Assert.assertEquals;
       
       import javax.ws.rs.core.SecurityContext;
       import javax.ws.rs.core.UriInfo;
       
       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;
       
       public class MetricsEndpointTest {
           @Test
           public void run() {
               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);
           }
       }
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services