Skip to content

Commit

Permalink
GERONIMO-6647 ensure the metricsendpoint does not fail when the metri…
Browse files Browse the repository at this point in the history
…c does not exist
  • Loading branch information
rmannibucau committed Oct 15, 2018
1 parent c2eae44 commit 62f6023
Showing 1 changed file with 16 additions and 3 deletions.
Expand Up @@ -16,13 +16,15 @@
*/
package org.apache.geronimo.microprofile.metrics.jaxrs;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;
import static org.eclipse.microprofile.metrics.MetricRegistry.Type.BASE;
import static org.eclipse.microprofile.metrics.MetricRegistry.Type.VENDOR;

import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -102,7 +104,7 @@ public String getText(@PathParam("registry") final String registry) {
@Produces(MediaType.APPLICATION_JSON)
public Object getJson(@PathParam("registry") final String registry,
@PathParam("metric") final String name) {
return singletonMap(name, findRegistry(registry).getMetrics().get(name));
return singleEntry(name, findRegistry(registry));
}

@GET
Expand All @@ -111,15 +113,20 @@ public Object getJson(@PathParam("registry") final String registry,
public String getText(@PathParam("registry") final String registry,
@PathParam("metric") final String name) {
final MetricRegistry metricRegistry = findRegistry(registry);
return prometheus.toText(metricRegistry, registry, singletonMap(name, metricRegistry.getMetrics().get(name))).toString();
return prometheus.toText(
metricRegistry, registry,
singleEntry(name, metricRegistry))
.toString();
}

@OPTIONS
@Path("{registry}/{metric}")
@Produces(MediaType.APPLICATION_JSON)
public Object getMetadata(@PathParam("registry") final String registry,
@PathParam("metric") final String name) {
return singletonMap(name, mapMeta(findRegistry(registry).getMetadata().get(name)));
return ofNullable(findRegistry(registry).getMetadata().get(name))
.map(metric -> singletonMap(name, mapMeta(metric)))
.orElse(emptyMap());
}

@OPTIONS
Expand All @@ -130,6 +137,12 @@ public Object getMetadata(@PathParam("registry") final String registry) {
.collect(toMap(Map.Entry::getKey, e -> mapMeta(e.getValue())));
}

private Map<String, Metric> singleEntry(final String name, final MetricRegistry metricRegistry) {
return ofNullable(metricRegistry.getMetrics().get(name))
.map(metric -> singletonMap(name, metric))
.orElseGet(Collections::emptyMap);
}

private Meta mapMeta(final Metadata value) {
return ofNullable(value).map(Meta::new).orElse(null);
}
Expand Down

0 comments on commit 62f6023

Please sign in to comment.