MetricRegistry implements MetricSet { public Timer timer(String name) { return getOrAdd(name, MetricBuilder.TIMERS); } 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 accessingATimerRegistersAndReusesIt() { final Timer timer1 = registry.timer("thing"); final Timer timer2 = registry.timer("thing"); assertThat(timer1) .isSameAs(timer2); verify(listener).onTimerAdded("thing", timer1); }
@Test public void accessingACustomTimerRegistersAndReusesIt() { final MetricRegistry.MetricSupplier<Timer> supplier = () -> timer; final Timer timer1 = registry.timer("thing", supplier); final Timer timer2 = registry.timer("thing", supplier); assertThat(timer1) .isSameAs(timer2); verify(listener).onTimerAdded("thing", timer1); }