Github user zd-project commented on a diff in the pull request: https://github.com/apache/storm/pull/2763#discussion_r203770940 --- Diff: storm-server/src/main/java/org/apache/storm/metric/StormMetricsRegistry.java --- @@ -27,52 +28,63 @@ @SuppressWarnings("unchecked") public class StormMetricsRegistry { - public static final MetricRegistry DEFAULT_REGISTRY = new MetricRegistry(); + private static final MetricRegistry DEFAULT_REGISTRY = new MetricRegistry(); private static final Logger LOG = LoggerFactory.getLogger(StormMetricsRegistry.class); - public static Meter registerMeter(String name) { - Meter meter = new Meter(); - return register(name, meter); + public static Meter registerMeter(final String name) { + return register(name, new Meter()); } - // 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. + * @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 register(name, () -> { + try { + return fn.call(); + } catch (Exception e) { + LOG.error("Error getting gauge value for {}", name, e); } - }; - return register(name, gauge); + return null; + }); } - public static void registerProvidedGauge(final String name, Gauge gauge) { + /** + * Register a provided gauge. Use this method if custom gauges is needed or + * no checked exceptions should be 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) { register(name, gauge); } public static Histogram registerHistogram(String name, Reservoir reservoir) { - Histogram histogram = new Histogram(reservoir); - return register(name, histogram); + return register(name, new Histogram(reservoir)); + } + + public static void registerAll(final String prefix, MetricSet metrics) { --- End diff -- These two methods support registering and unregistering a whole metric set. For usage you can check out #2764. But basically the idea was since only leader nimbus have valid ClusterSummary, we should dynamically register/unregister the portedSummaryMetricSet depending on which nimbus just become the leader.
---