Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AutoShutdownDelegatedExecutorService in ExecutorServiceMetrics #5811

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ else if (allowIllegalReflectiveAccess) {
if (className.equals("java.util.concurrent.Executors$DelegatedScheduledExecutorService")) {
monitor(registry, unwrapThreadPoolExecutor(executorService, executorService.getClass()));
}
else if (className.equals("java.util.concurrent.Executors$FinalizableDelegatedExecutorService")) {
else if (className.equals("java.util.concurrent.Executors$FinalizableDelegatedExecutorService")
|| className.equals("java.util.concurrent.Executors$AutoShutdownDelegatedExecutorService")) {
monitor(registry,
unwrapThreadPoolExecutor(executorService, executorService.getClass().getSuperclass()));
}
Expand Down
9 changes: 7 additions & 2 deletions micrometer-java21/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ task reflectiveTests(type: Test) {
includeTags 'reflective'
}

// This hack is needed since VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang, see its javadoc
jvmArgs += ['--add-opens', 'java.base/java.lang=ALL-UNNAMED']
// This hack is needed for the following tests:
// - VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang.
// - ExecutorServiceMetricsReflectiveTests utilizes reflection against java.util.concurrent.
jvmArgs += [
'--add-opens', 'java.base/java.lang=ALL-UNNAMED',
'--add-opens', 'java.base/java.util.concurrent=ALL-UNNAMED'
]
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2025 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jvm;

import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ExecutorServiceMetrics} with reflection enabled.
*
* @author Tommy Ludwig
*/
@Tag("reflective")
class ExecutorServiceMetricsReflectiveTests {

SimpleMeterRegistry registry = new SimpleMeterRegistry();

@Test
void threadPoolMetricsWith_AutoShutdownDelegatedExecutorService() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
ExecutorService unmonitored = Executors.newSingleThreadExecutor();
assertThat(unmonitored.getClass().getName())
.isEqualTo("java.util.concurrent.Executors$AutoShutdownDelegatedExecutorService");
ExecutorService monitored = ExecutorServiceMetrics.monitor(registry, unmonitored, "test");
monitored.execute(latch::countDown);
assertThat(latch.await(100, TimeUnit.MILLISECONDS)).isTrue();
assertThat(registry.get("executor.completed").tag("name", "test").functionCounter().count()).isEqualTo(1L);
}

}
Loading