GarbageCollectorMetricSet implements MetricSet { @Override public Map<String, Metric> getMetrics() { final Map<String, Metric> gauges = new HashMap<>(); for (final GarbageCollectorMXBean gc : garbageCollectors) { final String name = WHITESPACE.matcher(gc.getName()).replaceAll("-"); gauges.put(name(name, "count"), (Gauge<Long>) gc::getCollectionCount); gauges.put(name(name, "time"), (Gauge<Long>) gc::getCollectionTime); } return Collections.unmodifiableMap(gauges); } GarbageCollectorMetricSet();  GarbageCollectorMetricSet(Collection<GarbageCollectorMXBean> garbageCollectors); @Override Map<String, Metric> getMetrics();  }
@Test public void hasGaugesForGcCountsAndElapsedTimes() { assertThat(metrics.getMetrics().keySet()) .containsOnly("PS-OldGen.time", "PS-OldGen.count"); }
@Test public void hasAGaugeForGcCounts() { final Gauge<Long> gauge = (Gauge<Long>) metrics.getMetrics().get("PS-OldGen.count"); assertThat(gauge.getValue()) .isEqualTo(1L); }
@Test public void hasAGaugeForGcTimes() { final Gauge<Long> gauge = (Gauge<Long>) metrics.getMetrics().get("PS-OldGen.time"); assertThat(gauge.getValue()) .isEqualTo(2L); }
@Test public void autoDiscoversGCs() { assertThat(new GarbageCollectorMetricSet().getMetrics().keySet()) .isNotEmpty(); }