1

自带的tomcat的metrics过于简单,没有线程池的任务队列信息,故这里扩展增加一下,方便监控。

参考TomcatPublicMetrics

public class AdvancedTomcatMetrics implements PublicMetrics,ApplicationContextAware{

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public Collection<Metric<?>> metrics() {
        if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
            EmbeddedServletContainer embeddedServletContainer = ((EmbeddedWebApplicationContext)applicationContext)
                    .getEmbeddedServletContainer();
            if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
                Connector connector = ((TomcatEmbeddedServletContainer) embeddedServletContainer).getTomcat().getConnector();
                ProtocolHandler handler = connector.getProtocolHandler();
                org.apache.tomcat.util.threads.ThreadPoolExecutor executor = (ThreadPoolExecutor) handler.getExecutor();
                //register tomcat thread pool stat
                List<Metric<?>> metrics = new ArrayList<Metric<?>>();
                metrics.add(new Metric<Integer>("tomcat.threads.active_count",executor.getActiveCount()));
                metrics.add(new Metric<Integer>("tomcat.threads.largest_pool_size",executor.getLargestPoolSize()));
                metrics.add(new Metric<Long>("tomcat.threads.task_count",executor.getTaskCount()));
                metrics.add(new Metric<Long>("tomcat.threads.completed_task_count",executor.getCompletedTaskCount()));
                metrics.add(new Metric<Integer>("tomcat.threads.submitted_count",executor.getSubmittedCount()));
//                metrics.add(new Metric<Integer>("tomcat.threads.pool_size",executor.getPoolSize()));
//                metrics.add(new Metric<Integer>("tomcat.threads.core_pool_size",executor.getCorePoolSize()));
//                metrics.add(new Metric<Integer>("tomcat.threads.max_pool_size",executor.getMaximumPoolSize()));
                return metrics;
            }
        }
        return Collections.emptySet();
    }
}

自动配置

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass({ Servlet.class, Tomcat.class })
    @ConditionalOnWebApplication
    public AdvancedTomcatMetrics advancedTomcatMetrics(){
        return new AdvancedTomcatMetrics();
    }

codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...