GarbageCollectorMetricSet implements MetricSet { @Override public Map<MetricName, Metric> getMetrics() { final Map<MetricName, Metric> gauges = new HashMap<MetricName, Metric>(); int index = 0; for (final GarbageCollectorMXBean gc : garbageCollectors) { final String name = WHITESPACE.matcher(gc.getName()).replaceAll("_").toLowerCase(); final int i = index; gauges.put(MetricRegistry.name(name, "count"), new PersistentGauge<Long>() { @Override public Long getValue() { return gc.getCollectionCount(); } }); gauges.put(MetricRegistry.name(name, "time"), new PersistentGauge<Long>() { @Override public Long getValue() { return gc.getCollectionTime(); } }); gauges.put(MetricRegistry.name(name, "count.delta"), new PersistentGauge<Long>() { @Override public Long getValue() { long current = gc.getCollectionCount(); long result = current - lastGarbageCountCollectors.get(i); lastGarbageCountCollectors.set(i, current); return result; } }); gauges.put(MetricRegistry.name(name, "time.delta"), new PersistentGauge<Long>() { @Override public Long getValue() { long current = gc.getCollectionTime(); long result = current - lastGarbageTimeCollectors.get(i); lastGarbageTimeCollectors.set(i, current); return result; } }); index = i + 1; } return Collections.unmodifiableMap(gauges); } GarbageCollectorMetricSet();  GarbageCollectorMetricSet(Collection<GarbageCollectorMXBean> garbageCollectors); @Override Map<MetricName, Metric> getMetrics(); @Override long lastUpdateTime();  }
@Test public void hasGaugesForGcCountsAndElapsedTimes() throws Exception { assertThat(metrics.getMetrics().keySet()) .containsOnly(PS_OLDGEN_TIME, PS_OLDGEN_COUNT, PS_OLDGEN_TIME_DELTA, PS_OLDGEN_COUNT_DELTA); }
@Test public void hasAGaugeForGcCounts() throws Exception { final Gauge gauge = (Gauge) metrics.getMetrics().get(PS_OLDGEN_COUNT); assertThat(gauge.getValue()) .isEqualTo(1L); }
@Test public void hasAGaugeForGcTimes() throws Exception { final Gauge gauge = (Gauge) metrics.getMetrics().get(PS_OLDGEN_TIME); assertThat(gauge.getValue()) .isEqualTo(2L); }
@Test public void autoDiscoversGCs() throws Exception { assertThat(new GarbageCollectorMetricSet().getMetrics().keySet()) .isNotEmpty(); }