Skip to content

Commit

Permalink
Merge branch '1.12.x' into 1.13.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Jul 31, 2024
2 parents b3ab39e + f0eb24b commit 71dcae5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,18 @@ public void bindTo(@NonNull MeterRegistry registry) {

private Iterable<Tag> nameTag(ObjectName name, String type)
throws AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
Tags tags = Tags.of("name", name.getKeyProperty("name"), "type", type);
return Tags.of("name", name.getKeyProperty("name"), "type", type, "factoryType", getFactoryType(name, type));
}

private String getFactoryType(ObjectName name, String type)
throws ReflectionException, AttributeNotFoundException, InstanceNotFoundException, MBeanException {
if (Objects.equals(type, "GenericObjectPool")) {
// for GenericObjectPool, we want to include the name and factoryType as tags
String factoryType = mBeanServer.getAttribute(name, "FactoryType").toString();
tags = Tags.concat(tags, "factoryType", factoryType);
return mBeanServer.getAttribute(name, "FactoryType").toString();
}
else {
return "none";
}
return tags;
}

private void registerMetricsEventually(String type, BiConsumer<ObjectName, Tags> perObject) {
Expand Down Expand Up @@ -170,36 +175,34 @@ private void registerNotificationListener(String type, BiConsumer<ObjectName, Ta
// 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();
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 {
nameTags = nameTag(o, type);
break;
}
catch (AttributeNotFoundException | MBeanException | ReflectionException
| InstanceNotFoundException e) {
if (i == maxTries - 1) {
log.error("can not set name tag", e);
}
(notification, handback) -> executor.execute(() -> {
MBeanServerNotification mbs = (MBeanServerNotification) notification;
ObjectName o = mbs.getMBeanName();
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 {
nameTags = nameTag(o, type);
break;
}
catch (AttributeNotFoundException | MBeanException | ReflectionException
| InstanceNotFoundException e) {
if (i == maxTries - 1) {
log.error("can not set name tag", e);
}
}
perObject.accept(o, Tags.concat(tags, nameTags));
});
};
}
perObject.accept(o, Tags.concat(tags, nameTags));
});

NotificationFilter filter = (NotificationFilter) notification -> {
NotificationFilter filter = notification -> {
if (!MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(notification.getType()))
return false;
ObjectName obj = ((MBeanServerNotification) notification).getMBeanName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -41,50 +42,55 @@ class CommonsObjectPool2MetricsTest {

private int genericObjectPoolCount = 0;

private Tags tags = Tags.of("app", "myapp", "version", "1");
private final Tags tags = Tags.of("app", "myapp", "version", "1");

private final MeterRegistry registry = new SimpleMeterRegistry();
private MeterRegistry registry;

// tag::setup[]
private final CommonsObjectPool2Metrics commonsObjectPool2Metrics = new CommonsObjectPool2Metrics(tags);

// end::setup[]
@BeforeEach
void setUp() {
registry = new SimpleMeterRegistry();
}

@AfterEach
void afterEach() {
void tearDown() {
commonsObjectPool2Metrics.close();
}

@Test
void verifyMetricsWithExpectedTags() {
void verifyGenericObjectPoolMetricsWithExpectedTags() {
// tag::generic_pool[]
try (GenericObjectPool<Object> p = createGenericObjectPool()) {
MeterRegistry registry = new SimpleMeterRegistry();
try (GenericObjectPool<Object> ignored = createGenericObjectPool()) {
commonsObjectPool2Metrics.bindTo(registry);
Tags tagsToMatch = tags.and("name", "pool", "type", "GenericObjectPool", "factoryType",
"io.micrometer.core.instrument.binder.commonspool2.CommonsObjectPool2MetricsTest$1<java.lang.Object>");

registry.get("commons.pool2.num.idle").tags(tags).gauge();
registry.get("commons.pool2.num.waiters").tags(tags).gauge();
registry.get("commons.pool2.num.idle").tags(tagsToMatch).gauge();
registry.get("commons.pool2.num.waiters").tags(tagsToMatch).gauge();

Arrays
.asList("commons.pool2.created", "commons.pool2.borrowed", "commons.pool2.returned",
"commons.pool2.destroyed", "commons.pool2.destroyed.by.evictor",
"commons.pool2.destroyed.by.borrow.validation")
.forEach(name -> registry.get(name).tags(tags).functionCounter());
.forEach(name -> registry.get(name).tags(tagsToMatch).functionCounter());

Arrays
.asList("commons.pool2.max.borrow.wait", "commons.pool2.mean.active", "commons.pool2.mean.idle",
"commons.pool2.mean.borrow.wait")
.forEach(name -> registry.get(name).tags(tags).timeGauge());
.forEach(name -> registry.get(name).tags(tagsToMatch).timeGauge());
}
// end::generic_pool[]
}

@Test
void verifyGenericKeyedObjectPoolMetricsWithExpectedTags() {
// tag::generic_keyed_pool[]
try (GenericKeyedObjectPool<Object, Object> p = createGenericKeyedObjectPool()) {
Tags tagsToMatch = tags.and("type", "GenericKeyedObjectPool");
try (GenericKeyedObjectPool<Object, Object> ignored = createGenericKeyedObjectPool()) {
commonsObjectPool2Metrics.bindTo(registry);
Tags tagsToMatch = tags.and("name", "pool", "type", "GenericKeyedObjectPool", "factoryType", "none");

Arrays.asList("commons.pool2.num.idle", "commons.pool2.num.waiters")
.forEach(name -> registry.get(name).tags(tagsToMatch).gauge());
Expand Down Expand Up @@ -113,13 +119,13 @@ void verifyIdleAndActiveInstancesAreReported() throws Exception {
});

try (GenericObjectPool<Object> genericObjectPool = createGenericObjectPool()) {
latch.await(10, TimeUnit.SECONDS);
final Object o = genericObjectPool.borrowObject(10_000L);
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
Object object = genericObjectPool.borrowObject(10_000L);

assertThat(registry.get("commons.pool2.num.active").gauge().value()).isEqualTo(1.0);
assertThat(registry.get("commons.pool2.num.idle").gauge().value()).isEqualTo(0.0);

genericObjectPool.returnObject(o);
genericObjectPool.returnObject(object);

assertThat(registry.get("commons.pool2.num.active").gauge().value()).isEqualTo(0.0);
assertThat(registry.get("commons.pool2.num.idle").gauge().value()).isEqualTo(1.0);
Expand All @@ -128,11 +134,9 @@ void verifyIdleAndActiveInstancesAreReported() throws Exception {

@Test
void metricsReportedPerMultiplePools() {
try (final GenericObjectPool<Object> p1 = createGenericObjectPool();
final GenericObjectPool<Object> p2 = createGenericObjectPool();
final GenericObjectPool<Object> p3 = createGenericObjectPool()) {

MeterRegistry registry = new SimpleMeterRegistry();
try (GenericObjectPool<Object> ignored1 = createGenericObjectPool();
GenericObjectPool<Object> ignored2 = createGenericObjectPool();
GenericObjectPool<Object> ignored3 = createGenericObjectPool()) {
commonsObjectPool2Metrics.bindTo(registry);

registry.get("commons.pool2.num.waiters").tag("name", "pool" + genericObjectPoolCount).gauge();
Expand All @@ -142,7 +146,6 @@ void metricsReportedPerMultiplePools() {

@Test
void newPoolsAreDiscoveredByListener() throws InterruptedException {
MeterRegistry registry = new SimpleMeterRegistry();
commonsObjectPool2Metrics.bindTo(registry);

CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -151,8 +154,8 @@ void newPoolsAreDiscoveredByListener() throws InterruptedException {
latch.countDown();
});

try (GenericObjectPool<Object> p = createGenericObjectPool()) {
latch.await(10, TimeUnit.SECONDS);
try (GenericObjectPool<Object> ignored = createGenericObjectPool()) {
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
}
}

Expand All @@ -161,7 +164,7 @@ private GenericObjectPool<Object> createGenericObjectPool() {
GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(10);

return new GenericObjectPool<>(new BasePooledObjectFactory<Object>() {
return new GenericObjectPool<>(new BasePooledObjectFactory<>() {
@Override
public Object create() {
return new Object();
Expand All @@ -175,7 +178,7 @@ public PooledObject<Object> wrap(Object testObject) {
}

private GenericKeyedObjectPool<Object, Object> createGenericKeyedObjectPool() {
return new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<Object, Object>() {
return new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<>() {
@Override
public Object create(Object key) {
return key;
Expand Down

0 comments on commit 71dcae5

Please sign in to comment.