MemoryUsageGaugeSet implements MetricSet { @Override public Map<MetricName, Metric> getMetrics() { final Map<MetricName, Metric> gauges = new HashMap<MetricName, Metric>(); gauges.put(MetricName.build("total.init"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getInit() + mxBean.getNonHeapMemoryUsage().getInit(); } }); gauges.put(MetricName.build("total.used"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getUsed() + mxBean.getNonHeapMemoryUsage().getUsed(); } }); gauges.put(MetricName.build("total.max"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax(); } }); gauges.put(MetricName.build("total.committed"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getCommitted() + mxBean.getNonHeapMemoryUsage().getCommitted(); } }); gauges.put(MetricName.build("heap.init"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getInit(); } }); gauges.put(MetricName.build("heap.used"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getUsed(); } }); gauges.put(MetricName.build("heap.max"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getMax(); } }); gauges.put(MetricName.build("heap.committed"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getHeapMemoryUsage().getCommitted(); } }); gauges.put(MetricName.build("heap.usage"), new RatioGauge() { @Override protected Ratio getRatio() { final MemoryUsage usage = mxBean.getHeapMemoryUsage(); return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax()); } }); gauges.put(MetricName.build("non_heap.init"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getNonHeapMemoryUsage().getInit(); } }); gauges.put(MetricName.build("non_heap.used"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getNonHeapMemoryUsage().getUsed(); } }); gauges.put(MetricName.build("non_heap.max"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getNonHeapMemoryUsage().getMax(); } }); gauges.put(MetricName.build("non_heap.committed"), new PersistentGauge<Long>() { @Override public Long getValue() { return mxBean.getNonHeapMemoryUsage().getCommitted(); } }); gauges.put(MetricName.build("non_heap.usage"), new RatioGauge() { @Override protected Ratio getRatio() { final MemoryUsage usage = mxBean.getNonHeapMemoryUsage(); return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax()); } }); for (final MemoryPoolMXBean pool : memoryPools) { final MetricName poolName = MetricRegistry.name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("_").toLowerCase()); gauges.put(poolName.resolve("usage"), new RatioGauge() { @Override protected Ratio getRatio() { MemoryUsage usage = pool.getUsage(); return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax()); } }); gauges.put(poolName.resolve("max"),new PersistentGauge<Long>() { @Override public Long getValue() { return pool.getUsage().getMax(); } }); gauges.put(poolName.resolve("used"),new PersistentGauge<Long>() { @Override public Long getValue() { return pool.getUsage().getUsed(); } }); gauges.put(poolName.resolve("committed"),new PersistentGauge<Long>() { @Override public Long getValue() { return pool.getUsage().getCommitted(); } }); if (pool.getCollectionUsage() != null) { gauges.put(poolName.resolve("used_after_gc"),new PersistentGauge<Long>() { @Override public Long getValue() { return pool.getCollectionUsage().getUsed(); } }); } gauges.put(poolName.resolve("init"),new PersistentGauge<Long>() { @Override public Long getValue() { return pool.getUsage().getInit(); } }); } return Collections.unmodifiableMap(gauges); } MemoryUsageGaugeSet();  MemoryUsageGaugeSet(MemoryMXBean mxBean,
                               Collection<MemoryPoolMXBean> memoryPools); @Override Map<MetricName, Metric> getMetrics(); @Override long lastUpdateTime();  }
@Test public void hasASetOfGauges() throws Exception { assertThat(gauges.getMetrics().keySet()) .containsOnly( HEAP_INIT, HEAP_COMMITTED, HEAP_USED, HEAP_USAGE, HEAP_MAX, NON_HEAP_INIT, NON_HEAP_COMMITTED, NON_HEAP_USED, NON_HEAP_USAGE, NON_HEAP_MAX, TOTAL_INIT, TOTAL_COMMITTED, TOTAL_USED, TOTAL_MAX, BIG_POOL.resolve("init"), BIG_POOL.resolve("committed"), BIG_POOL.resolve("used"), BIG_POOL.resolve("usage"), BIG_POOL.resolve("max"), WEIRD_POOL.resolve("init"), WEIRD_POOL.resolve("committed"), WEIRD_POOL.resolve("used"), WEIRD_POOL.resolve("used_after_gc"), WEIRD_POOL.resolve("usage"), WEIRD_POOL.resolve("max")); }
@Test public void hasAGaugeForTotalCommitted() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(TOTAL_COMMITTED); assertThat(gauge.getValue()) .isEqualTo(11L); }
@Test public void hasAGaugeForTotalInit() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(TOTAL_INIT); assertThat(gauge.getValue()) .isEqualTo(22L); }
@Test public void hasAGaugeForTotalUsed() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(TOTAL_USED); assertThat(gauge.getValue()) .isEqualTo(33L); }
@Test public void hasAGaugeForTotalMax() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(TOTAL_MAX); assertThat(gauge.getValue()) .isEqualTo(44L); }
@Test public void hasAGaugeForHeapCommitted() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(HEAP_COMMITTED); assertThat(gauge.getValue()) .isEqualTo(10L); }
@Test public void hasAGaugeForHeapInit() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(HEAP_INIT); assertThat(gauge.getValue()) .isEqualTo(20L); }
@Test public void hasAGaugeForHeapUsed() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(HEAP_USED); assertThat(gauge.getValue()) .isEqualTo(30L); }
@Test public void hasAGaugeForHeapMax() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(HEAP_MAX); assertThat(gauge.getValue()) .isEqualTo(40L); }
@Test public void hasAGaugeForHeapUsage() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(HEAP_USAGE); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForNonHeapCommitted() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(NON_HEAP_COMMITTED); assertThat(gauge.getValue()) .isEqualTo(1L); }
@Test public void hasAGaugeForNonHeapInit() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(NON_HEAP_INIT); assertThat(gauge.getValue()) .isEqualTo(2L); }
@Test public void hasAGaugeForNonHeapUsed() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(NON_HEAP_USED); assertThat(gauge.getValue()) .isEqualTo(3L); }
@Test public void hasAGaugeForNonHeapMax() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(NON_HEAP_MAX); assertThat(gauge.getValue()) .isEqualTo(4L); }
@Test public void hasAGaugeForNonHeapUsage() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(NON_HEAP_USAGE); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForMemoryPoolUsage() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(BIG_POOL.resolve("usage")); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForWeirdMemoryPoolInit() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("init")); assertThat(gauge.getValue()) .isEqualTo(200L); }
@Test public void hasAGaugeForWeirdMemoryPoolCommitted() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("committed")); assertThat(gauge.getValue()) .isEqualTo(100L); }
@Test public void hasAGaugeForWeirdMemoryPoolUsed() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("used")); assertThat(gauge.getValue()) .isEqualTo(300L); }
@Test public void hasAGaugeForWeirdMemoryPoolUsage() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("usage")); assertThat(gauge.getValue()) .isEqualTo(3.0); }
@Test public void hasAGaugeForWeirdMemoryPoolMax() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("max")); assertThat(gauge.getValue()) .isEqualTo(-1L); }
@Test public void hasAGaugeForWeirdCollectionPoolUsed() throws Exception { final Gauge gauge = (Gauge) gauges.getMetrics().get(WEIRD_POOL.resolve("used_after_gc")); assertThat(gauge.getValue()) .isEqualTo(290L); }
@Test public void autoDetectsMemoryUsageBeanAndMemoryPools() throws Exception { assertThat(new MemoryUsageGaugeSet().getMetrics().keySet()) .isNotEmpty(); }