Apache Commons Pool Instrumentation

Apache Commons Pool is an open source software library provides an object-pooling API and a number of object pool implementations.

Below you can find an example of how to instrument Apache Commons Pool with Micrometer.

// Setting up instrumentation
private final CommonsObjectPool2Metrics commonsObjectPool2Metrics = new CommonsObjectPool2Metrics(tags);


// Generic Pool instrumentation (with examples of created meters)
try (GenericObjectPool<Object> p = createGenericObjectPool()) {
    MeterRegistry registry = new SimpleMeterRegistry();
    commonsObjectPool2Metrics.bindTo(registry);

    registry.get("commons.pool2.num.idle").tags(tags).gauge();
    registry.get("commons.pool2.num.waiters").tags(tags).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());

    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());
}

// Generic Keyed Pool instrumentation (with examples of created meters)
try (GenericKeyedObjectPool<Object, Object> p = createGenericKeyedObjectPool()) {
    Tags tagsToMatch = tags.and("type", "GenericKeyedObjectPool");
    commonsObjectPool2Metrics.bindTo(registry);

    Arrays.asList("commons.pool2.num.idle", "commons.pool2.num.waiters")
        .forEach(name -> registry.get(name).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(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(tagsToMatch).timeGauge());
}