mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-10-31 17:05:07 +01:00 
			
		
		
		
	simplify HttpSink config
This commit is contained in:
		| @@ -14,11 +14,8 @@ import ( | |||||||
|  |  | ||||||
| type HttpSinkConfig struct { | type HttpSinkConfig struct { | ||||||
| 	defaultSinkConfig | 	defaultSinkConfig | ||||||
| 	Host            string `json:"host,omitempty"` | 	URL             string `json:"url,omitempty"` | ||||||
| 	Port            string `json:"port,omitempty"` |  | ||||||
| 	Database        string `json:"database,omitempty"` |  | ||||||
| 	JWT             string `json:"jwt,omitempty"` | 	JWT             string `json:"jwt,omitempty"` | ||||||
| 	SSL             bool   `json:"ssl,omitempty"` |  | ||||||
| 	Timeout         string `json:"timeout,omitempty"` | 	Timeout         string `json:"timeout,omitempty"` | ||||||
| 	MaxIdleConns    int    `json:"max_idle_connections,omitempty"` | 	MaxIdleConns    int    `json:"max_idle_connections,omitempty"` | ||||||
| 	IdleConnTimeout string `json:"idle_connection_timeout,omitempty"` | 	IdleConnTimeout string `json:"idle_connection_timeout,omitempty"` | ||||||
| @@ -28,7 +25,6 @@ type HttpSinkConfig struct { | |||||||
| type HttpSink struct { | type HttpSink struct { | ||||||
| 	sink | 	sink | ||||||
| 	client          *http.Client | 	client          *http.Client | ||||||
| 	url, jwt        string |  | ||||||
| 	encoder         *influx.Encoder | 	encoder         *influx.Encoder | ||||||
| 	buffer          *bytes.Buffer | 	buffer          *bytes.Buffer | ||||||
| 	config          HttpSinkConfig | 	config          HttpSinkConfig | ||||||
| @@ -41,7 +37,6 @@ type HttpSink struct { | |||||||
| func (s *HttpSink) Init(config json.RawMessage) error { | func (s *HttpSink) Init(config json.RawMessage) error { | ||||||
| 	// Set default values | 	// Set default values | ||||||
| 	s.name = "HttpSink" | 	s.name = "HttpSink" | ||||||
| 	s.config.SSL = false |  | ||||||
| 	s.config.MaxIdleConns = 10 | 	s.config.MaxIdleConns = 10 | ||||||
| 	s.config.IdleConnTimeout = "5s" | 	s.config.IdleConnTimeout = "5s" | ||||||
| 	s.config.Timeout = "5s" | 	s.config.Timeout = "5s" | ||||||
| @@ -57,8 +52,8 @@ func (s *HttpSink) Init(config json.RawMessage) error { | |||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if len(s.config.Host) == 0 || len(s.config.Port) == 0 || len(s.config.Database) == 0 { | 	if len(s.config.URL) == 0 { | ||||||
| 		return errors.New("`host`, `port` and `database` config options required for TCP sink") | 		return errors.New("`url` config option is required for HTTP sink") | ||||||
| 	} | 	} | ||||||
| 	if s.config.MaxIdleConns > 0 { | 	if s.config.MaxIdleConns > 0 { | ||||||
| 		s.maxIdleConns = s.config.MaxIdleConns | 		s.maxIdleConns = s.config.MaxIdleConns | ||||||
| @@ -80,12 +75,6 @@ func (s *HttpSink) Init(config json.RawMessage) error { | |||||||
| 		IdleConnTimeout: s.idleConnTimeout, | 		IdleConnTimeout: s.idleConnTimeout, | ||||||
| 	} | 	} | ||||||
| 	s.client = &http.Client{Transport: tr, Timeout: s.timeout} | 	s.client = &http.Client{Transport: tr, Timeout: s.timeout} | ||||||
| 	proto := "http" |  | ||||||
| 	if s.config.SSL { |  | ||||||
| 		proto = "https" |  | ||||||
| 	} |  | ||||||
| 	s.url = fmt.Sprintf("%s://%s:%s/%s", proto, s.config.Host, s.config.Port, s.config.Database) |  | ||||||
| 	s.jwt = s.config.JWT |  | ||||||
| 	s.buffer = &bytes.Buffer{} | 	s.buffer = &bytes.Buffer{} | ||||||
| 	s.encoder = influx.NewEncoder(s.buffer) | 	s.encoder = influx.NewEncoder(s.buffer) | ||||||
| 	s.encoder.SetPrecision(time.Second) | 	s.encoder.SetPrecision(time.Second) | ||||||
| @@ -115,14 +104,14 @@ func (s *HttpSink) Flush() error { | |||||||
| 	s.batchCounter = 0 | 	s.batchCounter = 0 | ||||||
|  |  | ||||||
| 	// Create new request to send buffer | 	// Create new request to send buffer | ||||||
| 	req, err := http.NewRequest(http.MethodPost, s.url, s.buffer) | 	req, err := http.NewRequest(http.MethodPost, s.config.URL, s.buffer) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Set authorization header | 	// Set authorization header | ||||||
| 	if len(s.jwt) != 0 { | 	if len(s.config.JWT) != 0 { | ||||||
| 		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.jwt)) | 		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.config.JWT)) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Send | 	// Send | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user