springboot增長tomcat的metrics

自帶的tomcat的metrics過於簡單,沒有線程池的任務隊列信息,故這裏擴展增長一下,方便監控。apache

參考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();
    }
相關文章
相關標籤/搜索