@Test public void autoDetectsMemoryUsageBeanAndMemoryPools() throws Exception { assertThat(new MemoryUsageGaugeSet().getMetrics().keySet()) .isNotEmpty(); }
@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 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 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); }
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(); }
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();  }