bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/conf/system_elastic5.go (about)

     1  package conf
     2  
     3  import (
     4  	"time"
     5  
     6  	"bosun.org/cmd/bosun/expr"
     7  	elastic "gopkg.in/olivere/elastic.v5"
     8  )
     9  
    10  // ParseESConfig return expr.ElasticHost
    11  func parseESConfig5(value ElasticConf) expr.ElasticConfig {
    12  	var esConf expr.ElasticConfig
    13  	var options ESClientOptions
    14  	var opts []elastic.ClientOptionFunc
    15  
    16  	// method to append clinet options
    17  	addClientOptions := func(item elastic.ClientOptionFunc) {
    18  		opts = append(opts, item)
    19  	}
    20  
    21  	options = value.ClientOptions
    22  	if !options.Enabled {
    23  		esConf.SimpleClient = value.SimpleClient
    24  		esConf.Hosts = value.Hosts
    25  		esConf.ClientOptionFuncs = opts[0:0]
    26  		return esConf
    27  	}
    28  
    29  	// SetURL
    30  	addClientOptions(elastic.SetURL(value.Hosts...))
    31  
    32  	if options.BasicAuthUsername != "" && options.BasicAuthPassword != "" {
    33  		addClientOptions(elastic.SetBasicAuth(options.BasicAuthUsername, options.BasicAuthPassword))
    34  	}
    35  
    36  	if options.Scheme == "https" {
    37  		addClientOptions(elastic.SetScheme(options.Scheme))
    38  	}
    39  
    40  	// Default Enable
    41  	addClientOptions(elastic.SetSniff(options.SnifferEnabled))
    42  
    43  	if options.SnifferTimeoutStartup > 5 {
    44  		options.SnifferTimeoutStartup = options.SnifferTimeoutStartup * time.Second
    45  		addClientOptions(elastic.SetSnifferTimeoutStartup(options.SnifferTimeoutStartup))
    46  	}
    47  
    48  	if options.SnifferTimeout > 2 {
    49  		options.SnifferTimeout = options.SnifferTimeout * time.Second
    50  		addClientOptions(elastic.SetSnifferTimeout(options.SnifferTimeout))
    51  	}
    52  
    53  	if options.SnifferInterval > 15 {
    54  		options.SnifferInterval = options.SnifferInterval * time.Minute
    55  		addClientOptions(elastic.SetSnifferInterval(options.SnifferTimeout))
    56  	}
    57  
    58  	//Default Enable
    59  	addClientOptions(elastic.SetHealthcheck(options.HealthcheckEnabled))
    60  
    61  	if options.HealthcheckTimeoutStartup > 5 {
    62  		options.HealthcheckTimeoutStartup = options.HealthcheckTimeoutStartup * time.Second
    63  		addClientOptions(elastic.SetHealthcheckTimeoutStartup(options.HealthcheckTimeoutStartup))
    64  	}
    65  
    66  	if options.HealthcheckTimeout > 1 {
    67  		options.HealthcheckTimeout = options.HealthcheckTimeout * time.Second
    68  		addClientOptions(elastic.SetHealthcheckTimeout(options.HealthcheckTimeout))
    69  	}
    70  
    71  	if options.HealthcheckInterval > 60 {
    72  		options.HealthcheckInterval = options.HealthcheckInterval * time.Second
    73  		addClientOptions(elastic.SetHealthcheckInterval(options.HealthcheckInterval))
    74  	}
    75  
    76  	if options.MaxRetries > 0 {
    77  		addClientOptions(elastic.SetMaxRetries(options.MaxRetries))
    78  	}
    79  	esConf.Hosts = esConf.Hosts[0:0]
    80  	esConf.SimpleClient = false
    81  	esConf.ClientOptionFuncs = opts
    82  
    83  	return esConf
    84  }