Skip to content

Commit

Permalink
Register NotificationListener unconditionally in CommonsObjectPool2Me…
Browse files Browse the repository at this point in the history
…trics (#2111)

This commit also polishes it, and the changes are purely cosmetic or obvious.
  • Loading branch information
izeye committed May 20, 2020
1 parent 22700b3 commit 6b1687e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
Expand Up @@ -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() {
}

Expand Down
Expand Up @@ -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;
Expand All @@ -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<Tag> tags;
Expand Down Expand Up @@ -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");
});
}
Expand Down Expand Up @@ -144,7 +158,6 @@ private void registerMetricsEventually(String type, BiConsumer<ObjectName, Tags>
}
perObject.accept(o, Tags.concat(tags, nameTags));
}
return;
}
} catch (MalformedObjectNameException e) {
throw new RuntimeException("Error registering commons pool2 based metrics", e);
Expand All @@ -162,20 +175,20 @@ private void registerMetricsEventually(String type, BiConsumer<ObjectName, Tags>
*/
private void registerNotificationListener(String type, BiConsumer<ObjectName, Tags> 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<Tag> 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 {
Expand Down Expand Up @@ -222,6 +235,7 @@ private void registerNotificationListener(String type, BiConsumer<ObjectName, Ta
@Override
public void close() {
notificationListenerCleanUpRunnables.forEach(Runnable::run);
executor.shutdown();
}

private void registerGaugeForObject(
Expand Down

0 comments on commit 6b1687e

Please sign in to comment.