MetricRegistry implements MetricSet { @SuppressWarnings("unchecked") public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException { if (metric == null) { throw new NullPointerException("metric == null"); } if (metric instanceof MetricRegistry) { final MetricRegistry childRegistry = (MetricRegistry)metric; final String childName = name; childRegistry.addListener(new MetricRegistryListener() { @Override public void onGaugeAdded(String name, Gauge<?> gauge) { register(name(childName, name), gauge); } @Override public void onGaugeRemoved(String name) { remove(name(childName, name)); } @Override public void onCounterAdded(String name, Counter counter) { register(name(childName, name), counter); } @Override public void onCounterRemoved(String name) { remove(name(childName, name)); } @Override public void onHistogramAdded(String name, Histogram histogram) { register(name(childName, name), histogram); } @Override public void onHistogramRemoved(String name) { remove(name(childName, name)); } @Override public void onMeterAdded(String name, Meter meter) { register(name(childName, name), meter); } @Override public void onMeterRemoved(String name) { remove(name(childName, name)); } @Override public void onTimerAdded(String name, Timer timer) { register(name(childName, name), timer); } @Override public void onTimerRemoved(String name) { remove(name(childName, name)); } }); } else if (metric instanceof MetricSet) { registerAll(name, (MetricSet) metric); } else { final Metric existing = metrics.putIfAbsent(name, metric); if (existing == null) { onMetricAdded(name, metric); } else { throw new IllegalArgumentException("A metric named " + name + " already exists"); } } return metric; } MetricRegistry(); static String name(String name, String... names); static String name(Class<?> klass, String... names); @SuppressWarnings("unchecked") T register(String name, T metric); void registerAll(MetricSet metrics); Counter counter(String name); Counter counter(String name, final MetricSupplier<Counter> supplier); Histogram histogram(String name); Histogram histogram(String name, final MetricSupplier<Histogram> supplier); Meter meter(String name); Meter meter(String name, final MetricSupplier<Meter> supplier); Timer timer(String name); Timer timer(String name, final MetricSupplier<Timer> supplier); @SuppressWarnings("rawtypes") Gauge gauge(String name, final MetricSupplier<Gauge> supplier); boolean remove(String name); void removeMatching(MetricFilter filter); void addListener(MetricRegistryListener listener); void removeListener(MetricRegistryListener listener); SortedSet<String> getNames(); @SuppressWarnings("rawtypes") SortedMap<String, Gauge> getGauges(); @SuppressWarnings("rawtypes") SortedMap<String, Gauge> getGauges(MetricFilter filter); SortedMap<String, Counter> getCounters(); SortedMap<String, Counter> getCounters(MetricFilter filter); SortedMap<String, Histogram> getHistograms(); SortedMap<String, Histogram> getHistograms(MetricFilter filter); SortedMap<String, Meter> getMeters(); SortedMap<String, Meter> getMeters(MetricFilter filter); SortedMap<String, Timer> getTimers(); SortedMap<String, Timer> getTimers(MetricFilter filter); void registerAll(String prefix, MetricSet metrics); @Override Map<String, Metric> getMetrics();  }
@Test public void registeringAGaugeTriggersANotification() { assertThat(registry.register("thing", gauge)) .isEqualTo(gauge); verify(listener).onGaugeAdded("thing", gauge); }
@Test public void registeringACounterTriggersANotification() { assertThat(registry.register("thing", counter)) .isEqualTo(counter); verify(listener).onCounterAdded("thing", counter); }
@Test public void registeringAHistogramTriggersANotification() { assertThat(registry.register("thing", histogram)) .isEqualTo(histogram); verify(listener).onHistogramAdded("thing", histogram); }
@Test public void registeringAMeterTriggersANotification() { assertThat(registry.register("thing", meter)) .isEqualTo(meter); verify(listener).onMeterAdded("thing", meter); }
@Test public void registeringATimerTriggersANotification() { assertThat(registry.register("thing", timer)) .isEqualTo(timer); verify(listener).onTimerAdded("thing", timer); }
@Test public void registerNullMetric() { MetricRegistry registry = new MetricRegistry(); try { registry.register("any_name", null); Assert.fail("NullPointerException must be thrown !!!"); } catch (NullPointerException e) { Assert.assertEquals("metric == null", e.getMessage()); } }