Druid監控踩坑指南

Createsequence發表於2024-03-31

概述

最近專案組在準備接入各種指標監控系統,筆者負責的部分剛好涉及到了 Druid,故記錄一下在過程中遇到的各種情況和坑。

1. 直接使用 Druid

直接使用 Druid 的監控功能,需要直接將它提供的 Servlet 配置到 Web 容器中。具體可以直接參照官方文件。

  • 配置資訊採集:https://github.com/alibaba/druid/wiki/配置_StatFilter
  • 開啟監控:https://github.com/alibaba/druid/wiki/配置_StatViewServlet配置

此外,在這個過程中大部分問題官方文件中也有解答https://github.com/alibaba/druid/wiki/常見問題

2. 使用 starter

2.1. 啟用監控

如果使用 spring-boot-starter ,則可以基於 springboot 整合。
參照:SpringBoot——開啟Druid監控統計功能

2.2. SQL監控無資料問題

開啟配置後,雖然可用訪問監控頁面了,但是發現 SQL 監控依然沒有資料。
參照:SpringBoot中使用 Druid 資料庫連線池, 後臺SQL監控無效
不過筆者在嘗試過上述兩種方案後,在 SQL 監控依然無法獲取相應的監控資料,在查詢 issues 後發現瞭解決方案:SQL監控無資料--DataSource注入問題導致沒有資料
如果用 javaConf 或者 xml 的方式手動配置 Bean,需要指定開啟的 Filters。

@Bean("druidDataSource")
public DataSource dataSource(Environment env) throws SQLException {
  DruidDataSource dataSource = new DruidDataSource();
  dataSource.setFilters("stat");
  ......
  return dataSource;
}

如果不手動指定 Bean,使用配置指定 Druid 資料,會自動開啟 statFilter

spring.datasource.url=your_url 
spring.datasource.username=username 
spring.datasource.password=password
pring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

實際上,筆者的專案就是第一種情況,我們的資料來源是在配置類中單獨配置的,因此需要透過 dataSource.setFilters("stat") 手動開啟狀態採集器,其餘配置保持不變,重啟專案後正常。

3. 手動採集

如果不依賴 Druid 的監控頁面,也可以自己獲取執行緒池進行採集,Druid 已經提供好的響應的 API,我們只需要將相應的指標收集任務註冊好即可。
這裡以 Metrics 為例:

/**
 * DruidMetricsCollector
 *
 * @author huangchengxing
 */
@ConditionalOnClass({DruidDataSource.class, MeterRegistry.class})
@Component
@Slf4j
@RequiredArgsConstructor
public class DruidMetricsCollector implements ApplicationContextAware, InitializingBean {

    private static final String LABEL_NAME = "druid-data-source";
    private final MeterRegistry registry;
    @Setter
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("Metrics components: druid register finish");
        Map<String, DataSource> dataSourceMap = applicationContext.getBeansOfType(DataSource.class);
        dataSourceMap.forEach((name, ds) -> {
            DruidDataSource druidDataSource = null;
            try {
                druidDataSource = ds.unwrap(DruidDataSource.class);
            } catch (SQLException e) {
                log.warn("Failed to unwrap DruidDataSource from DataSource: {}", name, e);
            }
            if (druidDataSource != null) {
                log.info("Registering metrics for DruidDataSource: {}", name);
                registerMetrics(druidDataSource);
            }
        });
    }

    private void registerMetrics(DruidDataSource druidDataSource) {
        registerGauge(druidDataSource, "druid_initial_size", "Initial size", (datasource) -> (double) druidDataSource.getInitialSize());
        registerGauge(druidDataSource, "druid_min_idle", "Min idle", datasource -> (double) druidDataSource.getMinIdle());
        registerGauge(druidDataSource, "druid_max_active", "Max active", datasource -> (double) druidDataSource.getMaxActive());

        registerGauge(druidDataSource, "druid_active_count", "Active count", datasource -> (double) druidDataSource.getActiveCount());
        registerGauge(druidDataSource, "druid_active_peak", "Active peak", datasource -> (double) druidDataSource.getActivePeak());
        registerGauge(druidDataSource, "druid_pooling_peak", "Pooling peak", datasource -> (double) druidDataSource.getPoolingPeak());
        registerGauge(druidDataSource, "druid_pooling_count", "Pooling count", datasource -> (double) druidDataSource.getPoolingCount());
        registerGauge(druidDataSource, "druid_wait_thread_count", "Wait thread count", datasource -> (double) druidDataSource.getWaitThreadCount());

        registerGauge(druidDataSource, "druid_not_empty_wait_count", "Not empty wait count", datasource -> (double) druidDataSource.getNotEmptyWaitCount());
        registerGauge(druidDataSource, "druid_not_empty_wait_millis", "Not empty wait millis", datasource -> (double) druidDataSource.getNotEmptyWaitMillis());
        registerGauge(druidDataSource, "druid_not_empty_thread_count", "Not empty thread count", datasource -> (double) druidDataSource.getNotEmptyWaitThreadCount());

        registerGauge(druidDataSource, "druid_logic_connect_count", "Logic connect count", datasource -> (double) druidDataSource.getConnectCount());
        registerGauge(druidDataSource, "druid_logic_close_count", "Logic close count", datasource -> (double) druidDataSource.getCloseCount());
        registerGauge(druidDataSource, "druid_logic_connect_error_count", "Logic connect error count", datasource -> (double) druidDataSource.getConnectErrorCount());
        registerGauge(druidDataSource, "druid_physical_connect_count", "Physical connect count", datasource -> (double) druidDataSource.getCreateCount());
        registerGauge(druidDataSource, "druid_physical_close_count", "Physical close count", datasource -> (double) druidDataSource.getDestroyCount());
        registerGauge(druidDataSource, "druid_physical_connect_error_count", "Physical connect error count", datasource -> (double) druidDataSource.getCreateErrorCount());

        registerGauge(druidDataSource, "druid_error_count", "Error count", datasource -> (double) druidDataSource.getErrorCount());
        registerGauge(druidDataSource, "druid_execute_count", "Execute count", datasource -> (double) druidDataSource.getExecuteCount());
        registerGauge(druidDataSource, "druid_start_transaction_count", "Start transaction count", datasource -> (double) druidDataSource.getStartTransactionCount());
        registerGauge(druidDataSource, "druid_commit_count", "Commit count", datasource -> (double) druidDataSource.getCommitCount());
        registerGauge(druidDataSource, "druid_rollback_count", "Rollback count", datasource -> (double) druidDataSource.getRollbackCount());

        registerGauge(druidDataSource, "druid_prepared_statement_open_count", "Prepared statement open count", datasource -> (double) druidDataSource.getPreparedStatementCount());
        registerGauge(druidDataSource, "druid_prepared_statement_closed_count", "Prepared statement closed count", datasource -> (double) druidDataSource.getClosedPreparedStatementCount());
        registerGauge(druidDataSource, "druid_ps_cache_access_count", "PS cache access count", datasource -> (double) druidDataSource.getCachedPreparedStatementAccessCount());
        registerGauge(druidDataSource, "druid_ps_cache_hit_count", "PS cache hit count", datasource -> (double) druidDataSource.getCachedPreparedStatementHitCount());
        registerGauge(druidDataSource, "druid_ps_cache_miss_count", "PS cache miss count", datasource -> (double) druidDataSource.getCachedPreparedStatementMissCount());
        registerGauge(druidDataSource, "druid_execute_query_count", "Execute query count", datasource -> (double) druidDataSource.getExecuteQueryCount());
        registerGauge(druidDataSource, "druid_execute_update_count", "Execute update count", datasource -> (double) druidDataSource.getExecuteUpdateCount());
        registerGauge(druidDataSource, "druid_execute_batch_count", "Execute batch count", datasource -> (double) druidDataSource.getExecuteBatchCount());

        registerGauge(druidDataSource, "druid_max_wait", "Max wait", datasource -> (double) druidDataSource.getMaxWait());
        registerGauge(druidDataSource, "druid_max_wait_thread_count", "Max wait thread count", datasource -> (double) druidDataSource.getMaxWaitThreadCount());
        registerGauge(druidDataSource, "druid_login_timeout", "Login timeout", datasource -> (double) druidDataSource.getLoginTimeout());
        registerGauge(druidDataSource, "druid_query_timeout", "Query timeout", datasource -> (double) druidDataSource.getQueryTimeout());
        registerGauge(druidDataSource, "druid_transaction_query_timeout", "Transaction query timeout", datasource -> (double) druidDataSource.getTransactionQueryTimeout());
    }

    private void registerGauge(DruidDataSource weakRef, String metric, String help, ToDoubleFunction<DruidDataSource> measure) {
        Gauge.builder(metric, weakRef, measure)
            .description(help)
            .tag(LABEL_NAME, weakRef.getName())
            .register(this.registry);
    }
}

專案啟動後,確保該元件被 Spring 管理即可。

相關文章