MemoryUsageGaugeSet implements MetricSet { @Override public Map<String, Metric> getMetrics() { final Map<String, Metric> gauges = new HashMap<>(); gauges.put("total.init", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getInit() + mxBean.getNonHeapMemoryUsage().getInit()); gauges.put("total.used", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getUsed() + mxBean.getNonHeapMemoryUsage().getUsed()); gauges.put("total.max", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax()); gauges.put("total.committed", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getCommitted() + mxBean.getNonHeapMemoryUsage().getCommitted()); gauges.put("heap.init", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getInit()); gauges.put("heap.used", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getUsed()); gauges.put("heap.max", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getMax()); gauges.put("heap.committed", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getCommitted()); gauges.put("heap.usage", new RatioGauge() { @Override protected Ratio getRatio() { final MemoryUsage usage = mxBean.getHeapMemoryUsage(); return Ratio.of(usage.getUsed(), usage.getMax()); } }); gauges.put("non-heap.init", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getInit()); gauges.put("non-heap.used", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getUsed()); gauges.put("non-heap.max", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getMax()); gauges.put("non-heap.committed", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getCommitted()); gauges.put("non-heap.usage", new RatioGauge() { @Override protected Ratio getRatio() { final MemoryUsage usage = mxBean.getNonHeapMemoryUsage(); return Ratio.of(usage.getUsed(), usage.getMax()); } }); for (final MemoryPoolMXBean pool : memoryPools) { final String poolName = name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("-")); gauges.put(name(poolName, "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(name(poolName, "max"), (Gauge<Long>) () -> pool.getUsage().getMax()); gauges.put(name(poolName, "used"), (Gauge<Long>) () -> pool.getUsage().getUsed()); gauges.put(name(poolName, "committed"), (Gauge<Long>) () -> pool.getUsage().getCommitted()); if (pool.getCollectionUsage() != null) { gauges.put(name(poolName, "used-after-gc"), (Gauge<Long>) () -> pool.getCollectionUsage().getUsed()); } gauges.put(name(poolName, "init"), (Gauge<Long>) () -> pool.getUsage().getInit()); } return Collections.unmodifiableMap(gauges); } MemoryUsageGaugeSet();  MemoryUsageGaugeSet(MemoryMXBean mxBean,
                               Collection<MemoryPoolMXBean> memoryPools); @Override Map<String, Metric> getMetrics();  }
@Test public void hasAGaugeForHeapUsed() { final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.used"); assertThat(gauge.getValue()) .isEqualTo(30L); }
@Test public void hasAGaugeForHeapMax() { final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.max"); assertThat(gauge.getValue()) .isEqualTo(40L); }
@Test public void hasAGaugeForHeapUsage() { final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.usage"); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForNonHeapCommitted() { final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.committed"); assertThat(gauge.getValue()) .isEqualTo(1L); }
@Test public void hasAGaugeForNonHeapInit() { final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.init"); assertThat(gauge.getValue()) .isEqualTo(2L); }
@Test public void hasAGaugeForNonHeapUsed() { final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.used"); assertThat(gauge.getValue()) .isEqualTo(3L); }
@Test public void hasAGaugeForNonHeapMax() { final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.max"); assertThat(gauge.getValue()) .isEqualTo(4L); }
@Test public void hasAGaugeForNonHeapUsage() { final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.usage"); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForMemoryPoolUsage() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Big-Pool.usage"); assertThat(gauge.getValue()) .isEqualTo(0.75); }
@Test public void hasAGaugeForWeirdMemoryPoolInit() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.init"); assertThat(gauge.getValue()) .isEqualTo(200L); }
@Test public void hasAGaugeForWeirdMemoryPoolCommitted() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.committed"); assertThat(gauge.getValue()) .isEqualTo(100L); }
@Test public void hasAGaugeForWeirdMemoryPoolUsed() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.used"); assertThat(gauge.getValue()) .isEqualTo(300L); }
@Test public void hasAGaugeForWeirdMemoryPoolUsage() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.usage"); assertThat(gauge.getValue()) .isEqualTo(3.0); }
@Test public void hasAGaugeForWeirdMemoryPoolMax() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.max"); assertThat(gauge.getValue()) .isEqualTo(-1L); }
@Test public void hasAGaugeForWeirdCollectionPoolUsed() { final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Weird-Pool.used-after-gc"); assertThat(gauge.getValue()) .isEqualTo(290L); }
@Test public void autoDetectsMemoryUsageBeanAndMemoryPools() { assertThat(new MemoryUsageGaugeSet().getMetrics().keySet()) .isNotEmpty(); }
@Test public void hasASetOfGauges() { 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", "pools.Big-Pool.init", "pools.Big-Pool.committed", "pools.Big-Pool.used", "pools.Big-Pool.usage", "pools.Big-Pool.max", "pools.Weird-Pool.init", "pools.Weird-Pool.committed", "pools.Weird-Pool.used", "pools.Weird-Pool.used-after-gc", "pools.Weird-Pool.usage", "pools.Weird-Pool.max"); }
@Test public void hasAGaugeForTotalCommitted() { final Gauge gauge = (Gauge) gauges.getMetrics().get("total.committed"); assertThat(gauge.getValue()) .isEqualTo(11L); }
@Test public void hasAGaugeForTotalInit() { final Gauge gauge = (Gauge) gauges.getMetrics().get("total.init"); assertThat(gauge.getValue()) .isEqualTo(22L); }
@Test public void hasAGaugeForTotalUsed() { final Gauge gauge = (Gauge) gauges.getMetrics().get("total.used"); assertThat(gauge.getValue()) .isEqualTo(33L); }
@Test public void hasAGaugeForTotalMax() { final Gauge gauge = (Gauge) gauges.getMetrics().get("total.max"); assertThat(gauge.getValue()) .isEqualTo(44L); }
@Test public void hasAGaugeForHeapCommitted() { final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.committed"); assertThat(gauge.getValue()) .isEqualTo(10L); }
@Test public void hasAGaugeForHeapInit() { final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.init"); assertThat(gauge.getValue()) .isEqualTo(20L); }