Skip to content

Commit a7d7488

Browse files
committedDec 5, 2024
Merge branch '1.13.x' into 1.14.x
2 parents c95db08 + 8cd92d9 commit a7d7488

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetrics.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,11 @@ private void monitor(MeterRegistry registry, ForkJoinPool fj) {
406406
+ "underestimates the actual total number of steals when the pool " + "is not quiescent")
407407
.register(registry),
408408

409-
Gauge.builder(metricPrefix + "executor.queued", fj, ForkJoinPool::getQueuedTaskCount)
409+
Gauge
410+
.builder(metricPrefix + "executor.queued", fj,
411+
pool -> pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount())
410412
.tags(tags)
411-
.description("An estimate of the total number of tasks currently held in queues by worker threads")
413+
.description("The approximate number of tasks that are queued for execution")
412414
.register(registry),
413415

414416
Gauge.builder(metricPrefix + "executor.active", fj, ForkJoinPool::getActiveThreadCount)

‎micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetricsTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.params.provider.CsvSource;
3232

3333
import java.util.concurrent.*;
34+
import java.util.concurrent.atomic.AtomicBoolean;
3435

3536
import static org.assertj.core.api.AssertionsForClassTypes.*;
3637
import static org.awaitility.Awaitility.await;
@@ -302,6 +303,32 @@ void monitorScheduledExecutorServiceWithRepetitiveTasks(String metricPrefix, Str
302303
assertThat(registry.get(expectedMetricPrefix + "executor.idle").tags(userTags).timer().count()).isEqualTo(0L);
303304
}
304305

306+
@Test
307+
@Issue("#5650")
308+
void queuedSubmissionsAreIncludedInExecutorQueuedMetric() {
309+
ForkJoinPool pool = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, false, 1, 1, 1,
310+
a -> true, 555, TimeUnit.MILLISECONDS);
311+
ExecutorServiceMetrics.monitor(registry, pool, "myForkJoinPool");
312+
AtomicBoolean busy = new AtomicBoolean(true);
313+
314+
// will be an active task
315+
pool.execute(() -> {
316+
while (busy.get()) {
317+
}
318+
});
319+
320+
// will be queued for submission
321+
pool.execute(() -> {
322+
});
323+
pool.execute(() -> {
324+
});
325+
326+
double queued = registry.get("executor.queued").tag("name", "myForkJoinPool").gauge().value();
327+
busy.set(false);
328+
329+
assertThat(queued).isEqualTo(2.0);
330+
}
331+
305332
@SuppressWarnings("unchecked")
306333
private <T extends Executor> T monitorExecutorService(String executorName, String metricPrefix, T exec) {
307334
if (metricPrefix == null) {

0 commit comments

Comments
 (0)