跳到主要内容

Apache Commons Pool 监控

DeepSeek V3 中英对照 Commons Pool Apache Commons Pool Instrumentation

Apache Commons Pool 是一个开源的软件库,提供了一个对象池 API 和多种对象池实现。

以下是一个如何使用 Micrometer 对 Apache Commons Pool 进行监控的示例。

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

// Generic Pool instrumentation (with examples of created meters)
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(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(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());
}

// Generic Keyed Pool instrumentation (with examples of created meters)
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());

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