From 6b1687e32882fc99f23c98d25374c2083ccfae90 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 20 May 2020 11:51:45 +0900 Subject: [PATCH] Register NotificationListener unconditionally in CommonsObjectPool2Metrics (#2111) This commit also polishes it, and the changes are purely cosmetic or obvious. --- .../core/instrument/binder/BaseUnits.java | 6 ++ .../CommonsObjectPool2Metrics.java | 74 +++++++++++-------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/BaseUnits.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/BaseUnits.java index 9a6adae970..ee9e96df0f 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/BaseUnits.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/BaseUnits.java @@ -92,6 +92,12 @@ public final class BaseUnits { */ public static final String PERCENT = "percent"; + /** + * For objects. + * @since 1.6.0 + */ + public static final String OBJECTS = "objects"; + private BaseUnits() { } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/commonspool2/CommonsObjectPool2Metrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/commonspool2/CommonsObjectPool2Metrics.java index 173b5f55f6..a723fc8364 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/commonspool2/CommonsObjectPool2Metrics.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/commonspool2/CommonsObjectPool2Metrics.java @@ -17,6 +17,7 @@ package io.micrometer.core.instrument.binder.commonspool2; import io.micrometer.core.instrument.*; +import io.micrometer.core.instrument.binder.BaseUnits; import io.micrometer.core.instrument.binder.MeterBinder; import io.micrometer.core.lang.NonNull; import io.micrometer.core.lang.Nullable; @@ -43,13 +44,13 @@ * @since 1.6.0 */ public class CommonsObjectPool2Metrics implements MeterBinder, AutoCloseable { - private final static InternalLogger log = InternalLoggerFactory.getInstance(CommonsObjectPool2Metrics.class); + private static final InternalLogger log = InternalLoggerFactory.getInstance(CommonsObjectPool2Metrics.class); private static final String JMX_DOMAIN = "org.apache.commons.pool2"; private static final String METRIC_NAME_PREFIX = "commons.pool2."; private static final String[] TYPES = new String[]{"GenericObjectPool", "GenericKeyedObjectPool"}; - private final Executor executor = Executors.newSingleThreadExecutor(); + private final ExecutorService executor = Executors.newSingleThreadExecutor(); private final MBeanServer mBeanServer; private final Iterable tags; @@ -82,37 +83,50 @@ public void bindTo(@NonNull MeterRegistry registry) { registerMetricsEventually( type, (o, tags) -> { - registerGaugeForObject(registry, o, "NumIdle", "num.idle", tags, "The number of instances currently idle in this pool", "objects"); + registerGaugeForObject(registry, o, + "NumIdle", "num.idle", tags, + "The number of instances currently idle in this pool", BaseUnits.OBJECTS); + registerGaugeForObject(registry, o, + "NumWaiters", "num.waiters", tags, + "The estimate of the number of threads currently blocked waiting for an object from the pool", + BaseUnits.THREADS); - registerGaugeForObject(registry, o, "NumWaiters", "num.waiters", tags, "The estimate of the number of threads currently blocked waiting for an object from the pool", "threads"); - - registerFunctionCounterForObject(registry, o, "CreatedCount", "created", tags, "The total number of objects created for this pool over the lifetime of the pool", "objects"); - registerFunctionCounterForObject(registry, o, "BorrowedCount", "borrowed", tags, "The total number of objects successfully borrowed from this pool over the lifetime of the pool", "objects"); - registerFunctionCounterForObject(registry, o, "ReturnedCount", "returned", tags, "The total number of objects returned to this pool over the lifetime of the pool", "objects"); - registerFunctionCounterForObject(registry, o, "DestroyedCount", "destroyed", tags, "The total number of objects destroyed by this pool over the lifetime of the pool", "objects"); - registerFunctionCounterForObject( - registry, o, "DestroyedByEvictorCount", "destroyed.by.evictor", tags, "The total number of objects destroyed by the evictor associated with this pool over the lifetime of the pool", "objects"); - registerFunctionCounterForObject( - registry, - o, - "DestroyedByBorrowValidationCount", - "destroyed.by.borrow.validation", - tags, + registerFunctionCounterForObject(registry, o, + "CreatedCount", "created", tags, + "The total number of objects created for this pool over the lifetime of the pool", + BaseUnits.OBJECTS); + registerFunctionCounterForObject(registry, o, + "BorrowedCount", "borrowed", tags, + "The total number of objects successfully borrowed from this pool over the lifetime of the pool", + BaseUnits.OBJECTS); + registerFunctionCounterForObject(registry, o, + "ReturnedCount", "returned", tags, + "The total number of objects returned to this pool over the lifetime of the pool", + BaseUnits.OBJECTS); + registerFunctionCounterForObject(registry, o, + "DestroyedCount", "destroyed", tags, + "The total number of objects destroyed by this pool over the lifetime of the pool", + BaseUnits.OBJECTS); + registerFunctionCounterForObject(registry, o, + "DestroyedByEvictorCount", "destroyed.by.evictor", tags, + "The total number of objects destroyed by the evictor associated with this pool over the lifetime of the pool", + BaseUnits.OBJECTS); + registerFunctionCounterForObject(registry, o, + "DestroyedByBorrowValidationCount", "destroyed.by.borrow.validation", tags, "The total number of objects destroyed by this pool as a result of failing validation during borrowObject() over the lifetime of the pool", - "objects"); + BaseUnits.OBJECTS); - registerTimeGaugeForObject(registry, o, "MaxBorrowWaitTimeMillis", "max.borrow.wait", tags, + registerTimeGaugeForObject(registry, o, + "MaxBorrowWaitTimeMillis", "max.borrow.wait", tags, "The maximum time a thread has waited to borrow objects from the pool"); - registerTimeGaugeForObject(registry, o, "MeanActiveTimeMillis", "mean.active", tags, + registerTimeGaugeForObject(registry, o, + "MeanActiveTimeMillis", "mean.active", tags, "The mean time objects are active"); - registerTimeGaugeForObject(registry, o, "MeanIdleTimeMillis", "mean.idle", tags, + registerTimeGaugeForObject(registry, o, + "MeanIdleTimeMillis", "mean.idle", tags, "The mean time objects are idle"); - registerTimeGaugeForObject( - registry, - o, - "MeanBorrowWaitTimeMillis", - "mean.borrow.wait", - tags, + registerTimeGaugeForObject(registry, o, + "MeanBorrowWaitTimeMillis", "mean.borrow.wait", tags, "The mean time threads wait to borrow an object"); }); } @@ -144,7 +158,6 @@ private void registerMetricsEventually(String type, BiConsumer } perObject.accept(o, Tags.concat(tags, nameTags)); } - return; } } catch (MalformedObjectNameException e) { throw new RuntimeException("Error registering commons pool2 based metrics", e); @@ -162,20 +175,20 @@ private void registerMetricsEventually(String type, BiConsumer */ private void registerNotificationListener(String type, BiConsumer perObject) { NotificationListener notificationListener = - // in notifcation listener, we cannot get attributes for the registered object, + // in notification listener, we cannot get attributes for the registered object, // so we do it later time in a separate thread. (notification, handback) -> { executor.execute( () -> { MBeanServerNotification mbs = (MBeanServerNotification) notification; ObjectName o = mbs.getMBeanName(); - mbs.getUserData(); Iterable nameTags = emptyList(); int maxTries = 3; for (int i = 0; i < maxTries; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw new RuntimeException(e); } try { @@ -222,6 +235,7 @@ private void registerNotificationListener(String type, BiConsumer