spring-projects / spring-projects/spring-boot

TaskExecutorMetricsAutoConfiguration cannot support recording task execution time

Open
#27,041 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

theme: observability type: enhancement
Dominant language
Java
Stars
81.5k
Forks
42.7k
Avg merge
2d 4h
Merged PRs (30d)
65

Description

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 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/task/TaskExecutorMetricsAutoConfiguration.java and compare its ExecutorServiceMetrics#monitor usage with Micrometer's TimedExecutorService. Reproduce the sample's executor and timer behavior, then add coverage for task execution and idle-time metrics. Done means submitted tasks through an auto-configured ThreadPoolTaskExecutor record those metrics.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend, observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.