Skip to content

Commit c5514c1

Browse files
committed
Include queued submissions in executor.queued
This was missed when the metric was originally implemented for ForkJoinPool. Also aligns the description with the other `executor.queued` gauge for `ThreadPoolExecutor`. Resolves gh-5650
1 parent 7d34b2e commit c5514c1

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
@@ -387,9 +387,11 @@ private void monitor(MeterRegistry registry, ForkJoinPool fj) {
387387
+ "underestimates the actual total number of steals when the pool " + "is not quiescent")
388388
.register(registry);
389389

390-
Gauge.builder(metricPrefix + "executor.queued", fj, ForkJoinPool::getQueuedTaskCount)
390+
Gauge
391+
.builder(metricPrefix + "executor.queued", fj,
392+
pool -> pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount())
391393
.tags(tags)
392-
.description("An estimate of the total number of tasks currently held in queues by worker threads")
394+
.description("The approximate number of tasks that are queued for execution")
393395
.register(registry);
394396

395397
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

@@ -259,6 +260,32 @@ void monitorScheduledExecutorServiceWithRepetitiveTasks(String metricPrefix, Str
259260
assertThat(registry.get(expectedMetricPrefix + "executor.idle").tags(userTags).timer().count()).isEqualTo(0L);
260261
}
261262

263+
@Test
264+
@Issue("#5650")
265+
void queuedSubmissionsAreIncludedInExecutorQueuedMetric() {
266+
ForkJoinPool pool = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, false, 1, 1, 1,
267+
a -> true, 555, TimeUnit.MILLISECONDS);
268+
ExecutorServiceMetrics.monitor(registry, pool, "myForkJoinPool");
269+
AtomicBoolean busy = new AtomicBoolean(true);
270+
271+
// will be an active task
272+
pool.execute(() -> {
273+
while (busy.get()) {
274+
}
275+
});
276+
277+
// will be queued for submission
278+
pool.execute(() -> {
279+
});
280+
pool.execute(() -> {
281+
});
282+
283+
double queued = registry.get("executor.queued").tag("name", "myForkJoinPool").gauge().value();
284+
busy.set(false);
285+
286+
assertThat(queued).isEqualTo(2.0);
287+
}
288+
262289
@SuppressWarnings("unchecked")
263290
private <T extends Executor> T monitorExecutorService(String executorName, String metricPrefix, T exec) {
264291
if (metricPrefix == null) {

0 commit comments

Comments
 (0)