MetricRegistry implements MetricSet { @SuppressWarnings("rawtypes") public Gauge gauge(String name, final MetricSupplier<Gauge> supplier) { return getOrAdd(name, new MetricBuilder<Gauge>() { @Override public Gauge newMetric() { return supplier.newMetric(); } @Override public boolean isInstance(Metric metric) { return Gauge.class.isInstance(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 @SuppressWarnings("rawtypes") public void accessingACustomGaugeRegistersAndReusesIt() { final MetricRegistry.MetricSupplier<Gauge> supplier = () -> gauge; final Gauge gauge1 = registry.gauge("thing", supplier); final Gauge gauge2 = registry.gauge("thing", supplier); assertThat(gauge1) .isSameAs(gauge2); verify(listener).onGaugeAdded("thing", gauge1); }