Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor Spring Embedded Tomcat metrics

I would like to monitor the embedded Tomcat in my Spring Boot Service. Spring itself gives me some session usage stats, but I need additional information about the underlying thread pool like active connections, queue length etc.

I've searched the registered beans but can't find the thread pool that is used.

Any thoughts on how to retrieve that information?

like image 226
fab Avatar asked Apr 11 '17 13:04

fab


Video Answer


1 Answers

Hi question from 18 months ago!

So, turns out the embedded Tomcat metrics are pretty easy to monitor with Spring Boot 2.0 and its new Metrics package. A reason to upgrade that app.

Here's some sample code to get you started.

class SomeClass {

@Autowired
private MeterRegistry repo;

@ReadOperation
public WebEndpointResponse<Map> invoke() {
    Gauge busyThreads = repo.get("tomcat.threads.busy").gauge();
    Gauge allThreads  = repo.get("tomcat.threads.config.max").gauge();  // yes, could do @Value("${server.tomcat.max-threads:200}") and have it injected

    double busyThreadsCount = busyThreads.value();
    double allThreadsCount = allThreads.value();

    ....
}
}

See more:

  • https://github.com/rwilcox/k8_spring_actuators <-- where I have a Readiness actuator that fails if the load on the Tomcat threads are too high.
  • The source code for the Tomcat metrics stuff. One of my tasks soon may be to backport this code into a Spring Boot 1.x application. The TomcatMetrics class asks Tomcat for it specifically, so it shouldn't be too too hard to pull that into a Spring Boot 1.x actuator.
like image 134
RyanWilcox Avatar answered Oct 27 '22 04:10

RyanWilcox