From 52875646039ecadabbfc23fee993a3a24876d4c2 Mon Sep 17 00:00:00 2001 From: caojie09 Date: Wed, 27 Jun 2018 17:31:04 +0800 Subject: [PATCH] Report codecache metrics(#22) * upgrade version 1.4.3 * report code cache usage. --- client/lookout-api/pom.xml | 2 +- client/lookout-client/pom.xml | 2 +- client/lookout-common/pom.xml | 2 +- client/lookout-core/pom.xml | 2 +- client/lookout-ext-jvm/pom.xml | 2 +- .../lookout/jvm/JvmMemoryMetricsImporter.java | 53 +++++++++++++++++++ .../JvmInfoMetricsImporterTest.java | 9 ++++ client/lookout-ext-os/pom.xml | 2 +- client/lookout-reg-dropwizard/pom.xml | 2 +- client/lookout-reg-prometheus/pom.xml | 2 +- client/lookout-reg-server/pom.xml | 2 +- client/lookout-sofa-boot-starter/pom.xml | 2 +- client/pom.xml | 2 +- .../lookout-client-samples-boot/pom.xml | 2 +- .../pom.xml | 2 +- .../lookout-client-samples-java/pom.xml | 2 +- .../lookout-client-samples-prometheus/pom.xml | 2 +- 17 files changed, 77 insertions(+), 15 deletions(-) diff --git a/client/lookout-api/pom.xml b/client/lookout-api/pom.xml index 2cf3380..4792c4d 100644 --- a/client/lookout-api/pom.xml +++ b/client/lookout-api/pom.xml @@ -5,7 +5,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-client/pom.xml b/client/lookout-client/pom.xml index 2a7acc7..5eb95f1 100644 --- a/client/lookout-client/pom.xml +++ b/client/lookout-client/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-common/pom.xml b/client/lookout-common/pom.xml index 05f0334..2804425 100644 --- a/client/lookout-common/pom.xml +++ b/client/lookout-common/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-core/pom.xml b/client/lookout-core/pom.xml index 8e287ea..241b932 100644 --- a/client/lookout-core/pom.xml +++ b/client/lookout-core/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-ext-jvm/pom.xml b/client/lookout-ext-jvm/pom.xml index 07129cc..1671bc2 100644 --- a/client/lookout-ext-jvm/pom.xml +++ b/client/lookout-ext-jvm/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-ext-jvm/src/main/java/com/alipay/lookout/jvm/JvmMemoryMetricsImporter.java b/client/lookout-ext-jvm/src/main/java/com/alipay/lookout/jvm/JvmMemoryMetricsImporter.java index ba38883..a17809c 100644 --- a/client/lookout-ext-jvm/src/main/java/com/alipay/lookout/jvm/JvmMemoryMetricsImporter.java +++ b/client/lookout-ext-jvm/src/main/java/com/alipay/lookout/jvm/JvmMemoryMetricsImporter.java @@ -20,20 +20,29 @@ import com.alipay.lookout.api.Id; import com.alipay.lookout.api.Registry; import com.alipay.lookout.api.composite.MixinMetric; +import com.alipay.lookout.common.log.LookoutLoggerFactory; import com.alipay.lookout.spi.MetricsImporter; +import org.slf4j.Logger; import java.lang.management.ManagementFactory; +import java.lang.management.MemoryPoolMXBean; +import java.util.List; /** * Created by kevin.luy@alipay.com on 2017/2/16. */ public class JvmMemoryMetricsImporter implements MetricsImporter { + private static final Logger LOGGER = LookoutLoggerFactory + .getLogger(JvmMemoryMetricsImporter.class); + private static final String CODE_CACHE_NAME = "Code Cache"; + @Override public void register(Registry registry) { Id id = registry.createId("jvm.memory"); MixinMetric mixin = registry.mixinMetric(id); heapImport(mixin); nonheapImport(mixin); + codeCacheImport(mixin); } private void nonheapImport(MixinMetric mixin) { @@ -90,4 +99,48 @@ public Long value() { } }); } + + private void codeCacheImport(MixinMetric mixin) { + final MemoryPoolMXBean codeCacheMXBean = getCodeCacheMXBean(); + if (codeCacheMXBean == null) { + LOGGER.info("can't get code cache MemoryPoolMXBean, won't report code cache usage."); + return; + } + + mixin.gauge("codecache.init", new Gauge() { + @Override + public Long value() { + return codeCacheMXBean.getUsage().getInit(); + } + }); + mixin.gauge("codecache.used", new Gauge() { + @Override + public Long value() { + return codeCacheMXBean.getUsage().getUsed(); + } + }); + mixin.gauge("codecache.committed", new Gauge() { + @Override + public Long value() { + return codeCacheMXBean.getUsage().getCommitted(); + } + }); + mixin.gauge("codecache.max", new Gauge() { + @Override + public Long value() { + return codeCacheMXBean.getUsage().getMax(); + } + }); + } + + private MemoryPoolMXBean getCodeCacheMXBean() { + List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { + if (CODE_CACHE_NAME.equals(memoryPoolMXBean.getName())) { + return memoryPoolMXBean; + } + } + + return null; + } } diff --git a/client/lookout-ext-jvm/src/test/java/com.alipay.lookout.jvm.memory/JvmInfoMetricsImporterTest.java b/client/lookout-ext-jvm/src/test/java/com.alipay.lookout.jvm.memory/JvmInfoMetricsImporterTest.java index 7a0ada7..e8a25c5 100644 --- a/client/lookout-ext-jvm/src/test/java/com.alipay.lookout.jvm.memory/JvmInfoMetricsImporterTest.java +++ b/client/lookout-ext-jvm/src/test/java/com.alipay.lookout.jvm.memory/JvmInfoMetricsImporterTest.java @@ -77,6 +77,15 @@ public void testMemInfo() { Assert.assertTrue(gauge.value().longValue() >= 0); gauge = mixin.gauge("heap.max", null); Assert.assertTrue(gauge.value().longValue() >= -1); + + gauge = mixin.gauge("codecache.init", null); + Assert.assertTrue(gauge.value().longValue() >= 0); + gauge = mixin.gauge("codecache.used", null); + Assert.assertTrue(gauge.value().longValue() >= 0); + gauge = mixin.gauge("codecache.committed", null); + Assert.assertTrue(gauge.value().longValue() >= 0); + gauge = mixin.gauge("codecache.max", null); + Assert.assertTrue(gauge.value().longValue() >= -1); } @Test diff --git a/client/lookout-ext-os/pom.xml b/client/lookout-ext-os/pom.xml index 87ded21..e003e2c 100644 --- a/client/lookout-ext-os/pom.xml +++ b/client/lookout-ext-os/pom.xml @@ -5,7 +5,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-reg-dropwizard/pom.xml b/client/lookout-reg-dropwizard/pom.xml index e21e58d..58bb084 100644 --- a/client/lookout-reg-dropwizard/pom.xml +++ b/client/lookout-reg-dropwizard/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-reg-prometheus/pom.xml b/client/lookout-reg-prometheus/pom.xml index b905c97..30cd4a7 100644 --- a/client/lookout-reg-prometheus/pom.xml +++ b/client/lookout-reg-prometheus/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-reg-server/pom.xml b/client/lookout-reg-server/pom.xml index fff75f6..cfaa468 100644 --- a/client/lookout-reg-server/pom.xml +++ b/client/lookout-reg-server/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/lookout-sofa-boot-starter/pom.xml b/client/lookout-sofa-boot-starter/pom.xml index 246e416..9d369bd 100644 --- a/client/lookout-sofa-boot-starter/pom.xml +++ b/client/lookout-sofa-boot-starter/pom.xml @@ -6,7 +6,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 ../pom.xml diff --git a/client/pom.xml b/client/pom.xml index 7db42e7..ea5d545 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -5,7 +5,7 @@ com.alipay.sofa.lookout lookout-client-parent - 1.4.2 + 1.4.3 pom ${project.groupId}:${project.artifactId} It is a library which allows you to instrument your app with custom metrics diff --git a/client/samples/lookout-client-samples-boot/pom.xml b/client/samples/lookout-client-samples-boot/pom.xml index 898a8a8..4466482 100644 --- a/client/samples/lookout-client-samples-boot/pom.xml +++ b/client/samples/lookout-client-samples-boot/pom.xml @@ -21,7 +21,7 @@ UTF-8 UTF-8 1.8 - 1.4.2 + 1.4.3 diff --git a/client/samples/lookout-client-samples-dropwizard-boot/pom.xml b/client/samples/lookout-client-samples-dropwizard-boot/pom.xml index b972c1c..a865b33 100644 --- a/client/samples/lookout-client-samples-dropwizard-boot/pom.xml +++ b/client/samples/lookout-client-samples-dropwizard-boot/pom.xml @@ -21,7 +21,7 @@ UTF-8 UTF-8 1.8 - 1.4.2 + 1.4.3 diff --git a/client/samples/lookout-client-samples-java/pom.xml b/client/samples/lookout-client-samples-java/pom.xml index d8d55df..d2cb17a 100644 --- a/client/samples/lookout-client-samples-java/pom.xml +++ b/client/samples/lookout-client-samples-java/pom.xml @@ -23,7 +23,7 @@ com.alipay.sofa.lookout lookout-client - 1.4.2 + 1.4.3 diff --git a/client/samples/lookout-client-samples-prometheus/pom.xml b/client/samples/lookout-client-samples-prometheus/pom.xml index c6c53d5..8d9ac53 100644 --- a/client/samples/lookout-client-samples-prometheus/pom.xml +++ b/client/samples/lookout-client-samples-prometheus/pom.xml @@ -21,7 +21,7 @@ UTF-8 UTF-8 1.8 - 1.4.2 + 1.4.3