package com.jay.monitor.data.client.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.jay.monitor.data.client.MonitorDataClient;
import com.jay.monitor.data.client.MonitorSendProducer;
import com.jay.monitor.data.client.config.props.MonitorProperties;
import com.jay.monitor.data.client.ext.MonitorRuleFactory;
import com.jay.monitor.data.client.filter.DruidMonitorFilter;
import com.jay.monitor.data.core.enums.MonitorDataConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

import java.net.InetAddress;

/**
 * 客户端监控配置
 *
 * @author ： liukx
 * @time ： 2020/8/26 - 15:39
 */
@Configuration
@EnableConfigurationProperties({MonitorProperties.class})
@Import({MonitorSendProducer.class})
public class ClientMonitorAutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "spring.monitor-data", value = "host")
    public MonitorDataClient monitorDataClient(@Autowired MonitorProperties monitorProperties,@Autowired Environment env) throws Exception {
        MonitorDataClient client = new MonitorDataClient(monitorProperties.getHost(), monitorProperties.getPort());
        client.setQueueSize(monitorProperties.getQueueSize());
        client.addInvokeContext(MonitorDataConstants.INVOKE_CLIENT_NAME, env.getProperty("spring.application.name","未知服务"));
        client.addInvokeContext(MonitorDataConstants.INVOKE_CLIENT_IP, InetAddress.getLocalHost().getHostAddress());
        client.init();
        return client;
    }

//    @Bean
////    @ConditionalOnBean(value = {DruidDataSource.class})
//    public DruidMonitorFilter druidMonitorFilter() {
//        DruidMonitorFilter monitorFilter = new DruidMonitorFilter();
//        return monitorFilter;
//    }

    @Bean
    public MonitorRuleFactory monitorRuleFactory() {
        return new MonitorRuleFactory();
    }

}
