MetricRegistry { public Counter counter(String name) { if (counters.containsKey(name)) { return counters.get(name); } else { Counter counter = new Counter(); return register(counters, name, counter); } }  void registerGauge(String name, Gauge gauge); Counter counter(String name); Histogram histogram(String name, Double... pcts); Timer timer(String name, Double... pcts); void clearAll(); Map<String, Gauge> getGauges(); Map<String, Counter> getCounters(); Map<String, Histogram> getHistograms(); Map<String, Timer> getTimers(); void setDefaultPcts(Double[] defaultPcts); void addListener(MetricRegistryListener listener); static String name(String name, String... subNames); static SortedMap<String, T> getSortedMetrics(Map<String, T> metrics); static final MetricRegistry INSTANCE; }
@Test public void counter() { MetricRegistry metricRegistry = new MetricRegistry(); Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter")); assertThat(counter).isNotNull(); Map<String, Counter> counters = metricRegistry.getCounters(); Counter counter2 = counters.get("UserService.getUser.counter"); assertThat(counter2).isNotNull().isSameAs(counter); Counter counter3 = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter")); assertThat(counter3).isNotNull().isSameAs(counter); }