Github user srdo commented on a diff in the pull request: https://github.com/apache/storm/pull/2771#discussion_r205044870 --- Diff: storm-server/src/main/java/org/apache/storm/metric/StormMetricsRegistry.java --- @@ -12,79 +12,122 @@ package org.apache.storm.metric; +import com.codahale.metrics.ExponentiallyDecayingReservoir; import com.codahale.metrics.Gauge; import com.codahale.metrics.Histogram; import com.codahale.metrics.Meter; import com.codahale.metrics.Metric; import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.MetricSet; import com.codahale.metrics.Reservoir; +import com.codahale.metrics.Timer; + import java.util.Map; import java.util.concurrent.Callable; + +import org.apache.commons.lang.StringUtils; import org.apache.storm.daemon.metrics.MetricsUtils; import org.apache.storm.daemon.metrics.reporters.PreparableReporter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@SuppressWarnings("unchecked") -public class StormMetricsRegistry { - public static final MetricRegistry DEFAULT_REGISTRY = new MetricRegistry(); +public class StormMetricsRegistry extends MetricRegistry { + private static final StormMetricsRegistry DEFAULT_REGISTRY = new StormMetricsRegistry(); private static final Logger LOG = LoggerFactory.getLogger(StormMetricsRegistry.class); - public static Meter registerMeter(String name) { - Meter meter = new Meter(); - return register(name, meter); - } + private StormMetricsRegistry() {/*Singleton pattern*/} - // TODO: should replace Callable to Gauge<Integer> when nimbus.clj is translated to java - public static Gauge<Integer> registerGauge(final String name, final Callable fn) { - Gauge<Integer> gauge = new Gauge<Integer>() { - @Override - public Integer getValue() { - try { - return (Integer) fn.call(); - } catch (Exception e) { - LOG.error("Error getting gauge value for {}", name, e); - } - return 0; + /** + * Register a gauge with provided callback. This swallows all exceptions + * thrown from the callback, consider using {@link #registerProvidedGauge(String, Gauge)} + * if no exceptions will be thrown by the callable. + * + * @param name name of the gauge + * @param fn callback that measures + * @param <V> type of measurement the callback returns, also the type of gauge + * @return registered gauge + */ + public static <V> Gauge<V> registerGauge(final String name, final Callable<V> fn) { + return DEFAULT_REGISTRY.register(name, () -> { + try { + return fn.call(); + } catch (Exception e) { + LOG.error("Error getting gauge value for {}", name, e); } - }; - return register(name, gauge); + return null; + }); + } + + /** + * Register a provided gauge. Use this method if a custom gauge is needed or + * exceptions have already been handled. + * + * @param name name of the gauge + * @param gauge gauge + * @param <V> type of value the gauge measures + */ + public static <V> void registerProvidedGauge(final String name, final Gauge<V> gauge) { + DEFAULT_REGISTRY.register(name, gauge); } - public static void registerProvidedGauge(final String name, Gauge gauge) { - register(name, gauge); + public static Histogram registerHistogram(String name) { + return registerHistogram(name, new ExponentiallyDecayingReservoir()); } public static Histogram registerHistogram(String name, Reservoir reservoir) { - Histogram histogram = new Histogram(reservoir); - return register(name, histogram); + return DEFAULT_REGISTRY.register(name, new Histogram(reservoir)); } + public static Meter registerMeter(String name) { + return DEFAULT_REGISTRY.register(name, new Meter()); + } + + //Change the name to avoid name conflict in future Metrics release --- End diff -- You are right, my bad.
---