Skip to content

TaskExecutorMetricsAutoConfiguration cannot support recording task execution time #27041

Open
@csdbianhua

Description

@csdbianhua

Description:
I need to expose task execution time and idle time metrics of ‘ThreadPoolTaskExecutor’.
After few tasks being executed, all metrics except those two show up.
I found that TaskExecutorMetricsAutoConfiguration's behavior is iterating over ThreadPoolTaskExecutors and invoking ExecutorServiceMetrics#monitor. It do can register "executor.active", "executor.completed" and other metrics, but "executor"(task execution time) and "executor.idle"(task idle time) which processing in TimedExecutorService are left out.

Expectation:
As I mentioned in #23818, I need to use the TimedExecutorService returned by ExecutorServiceMetrics#monitor to wrap the tasks I submitted, then I can get task execution time and idle time.
So maybe we can wrap every ThreadPoolTaskExecutor, then task execution time and idle time should be recorded properly.

Version:
2.6.0-SNAPSHOT

Sample:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>thread-pool-metrics-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>thread-pool-metrics-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

ThreadPoolMetricsDemoApplication.java

@SpringBootApplication
public class ThreadPoolMetricsDemoApplication {

    @Bean
    ThreadPoolTaskExecutor myExecutor() {
        return new ThreadPoolTaskExecutor();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        try (var ctx = SpringApplication.run(ThreadPoolMetricsDemoApplication.class, args)) {
            var executor = ctx.getBean("myExecutor", ThreadPoolTaskExecutor.class);
            executor.submit(()->{}).get();
            var meterRegistry = ctx.getBean(MeterRegistry.class);
            var timer = meterRegistry.get("executor").timer();
            System.out.printf("Timer count should be 1 but got %s \n", timer.count());
            var timedExecutor = ExecutorServiceMetrics.monitor(meterRegistry, executor.getThreadPoolExecutor(), "myExecutor");
            timedExecutor.submit(()->{}).get();
            System.out.printf("Timer count should be 1 and got %s \n", timer.count());
        }
    }
}
Timer count should be 1 but got 0 
Timer count should be 1 and got 1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions