使用 DropWizard metrics-Jvm 获取 JMX 值

To get the JMX values using the DropWizard metrics-Jvm

我尝试使用 dropWizard 库获取 JVM 指标值(MemoryUsageGaugeSet、GarbageCollectionUsageGaugeSet)

 <dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-jvm</artifactId>
    <version>3.1.2</version>
 </dependency>

代码实现。

  public static void main(String[] args ){
  MemoryUsageGaugeSet memory = new MemoryUsageGaugeSet();
  Map<String, Metric> memoryMap = memory.getMetrics();
  system.out.println( memoryMap);}

我得到的结果是

{heap.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$8@d16011,非heap.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$11@cc09904,pools.Code-Cache.used= com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@748032cc, pools.Code-Cache.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@2b6e0423, heap.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$6@203428b7, pools.PS -旧-Gen.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@28b49d0c, total.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$4@5dd7cc84, pools.PS-旧-Gen.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@6438401, total.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$1@7b365ee, 非heap.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$12@be3d4e9, pools.PS-Eden-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@68339afd, pools.Compressed-Class-Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@724a6876, pools.Metaspace.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15 @6ecaffc4,pools.Metaspace.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@7b1b3a68,pools.PS-Eden-Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@f8ba384,pools.PS-Eden-Space.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@2e2373db, pools.Compressed-Class-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@17ae8d04, heap.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$9@35c43de3, pools.PS-伊甸园-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@67013f5c, pools.PS-幸存者-Space.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@ 520935cc, pools.Code-Cache.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@31ac5424, pools.Compressed-Class-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@25135cd7, pools.Metaspace.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@4b066cf6, pools.PS-Survivor-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@50ba970f, pools.PS-Old-Gen.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@3e4e448, pools.Code-Cache.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@3c2ef507, pools.PS-幸存者-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@44374fa8, total.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$2@40b1aace, pools.Code-Cache.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@6453d034, pools.PS-旧-Gen.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@2f9e8774, heap.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$5@512ad720, 非heap.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$13@1c80e011, pools.Compressed-Class-Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@662841c5,非heap.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$10@3dd6cf53,pools.PS-Eden -Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@3a428c1e,非heap.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$14@21d9632f,heap.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$7@5db28b8,pools.PS-幸存者-Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@efcd307,pools.Metaspace.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@20f09cba,total.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$3@4ada5390 , pools.Metaspace.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@16982c9b, pools.Compressed-Class-Space.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@7e94b1a4, pools.PS-幸存者-Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@5aa5f780, pools.PS-旧-Gen.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@427b7997}

我得到了对象的引用,但我想得到这些引用的值。为此要做什么?

要get/print一个对象的值你需要在class中实现toString()MemoryUsageGaugeSet没有。 验证@javadoc

一种方法是使用您自己的实现扩展 class toString()

另一种方法是遍历地图并打印每个值。您可能想尝试下面的内容..

Map<String, Metric> memoryMap = memory.getMetrics();

for (String key : memoryMap.keySet()) {
    Metric m = memoryMap.get(key);
    System.out.println(m.getClass());

    if (m instanceof Gauge) {
        System.out.println(((Gauge<Long>) m).getValue());
    }
}

MemoryUsageGaugeSet 结果 Gauge<Long> 这是 Metric.

的实现

希望对您有所帮助。