package com.tzy.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
public class DruidMetricsConfiguration {

    @Resource
    private DruidDataSource druidDataSource;

    @Resource
    private MeterRegistry meterRegistry;

    @PostConstruct
    public void init() {
        meterRegistry.gauge("druid.activeCount", druidDataSource, DruidDataSource::getActiveCount);
        meterRegistry.gauge("druid.poolingCount", druidDataSource, DruidDataSource::getPoolingCount);
        meterRegistry.gauge("druid.connectCount", druidDataSource, DruidDataSource::getConnectCount);
        meterRegistry.gauge("druid.closeCount", druidDataSource, DruidDataSource::getCloseCount);
        meterRegistry.gauge("druid.recycleErrorCount", druidDataSource, DruidDataSource::getRecycleErrorCount);
        meterRegistry.gauge("druid.connectErrorCount", druidDataSource, DruidDataSource::getConnectErrorCount);
        meterRegistry.gauge("druid.recycleCount", druidDataSource, DruidDataSource::getRecycleCount);
        meterRegistry.gauge("druid.removeAbandonedCount", druidDataSource, DruidDataSource::getRemoveAbandonedCount);
        meterRegistry.gauge("druid.notEmptyWaitCount", druidDataSource, DruidDataSource::getNotEmptyWaitCount);
        meterRegistry.gauge("druid.notEmptySignalCount", druidDataSource, DruidDataSource::getNotEmptySignalCount);
        meterRegistry.gauge("druid.notEmptyWaitNanos", druidDataSource, DruidDataSource::getNotEmptyWaitNanos);
        meterRegistry.gauge("druid.activePeak", druidDataSource, DruidDataSource::getActivePeak);
        meterRegistry.gauge("druid.poolingPeak", druidDataSource, DruidDataSource::getPoolingPeak);
        meterRegistry.gauge("druid.discardCount", druidDataSource, DruidDataSource::getDiscardCount);
        meterRegistry.gauge("druid.notEmptyWaitThreadCount", druidDataSource, DruidDataSource::getNotEmptyWaitThreadCount);
        meterRegistry.gauge("druid.notEmptyWaitThreadPeak", druidDataSource, DruidDataSource::getNotEmptyWaitThreadPeak);
    }
}