2021-10-12 13:44:38 +02:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-04 18:12:24 +01:00
|
|
|
"encoding/json"
|
2021-10-12 13:44:38 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
|
|
|
influx "github.com/influxdata/line-protocol"
|
2021-10-12 13:44:38 +02:00
|
|
|
)
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
type HttpSinkConfig struct {
|
|
|
|
defaultSinkConfig
|
2022-02-08 18:06:07 +01:00
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
Port string `json:"port,omitempty"`
|
|
|
|
Database string `json:"database,omitempty"`
|
|
|
|
JWT string `json:"jwt,omitempty"`
|
|
|
|
SSL bool `json:"ssl,omitempty"`
|
|
|
|
Timeout string `json:"timeout,omitempty"`
|
|
|
|
MaxIdleConns int `json:"max_idle_connections,omitempty"`
|
|
|
|
IdleConnTimeout string `json:"idle_connection_timeout,omitempty"`
|
2022-02-09 19:47:49 +01:00
|
|
|
BatchSize int `json:"batch_size,omitempty"`
|
2022-02-04 18:12:24 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:44:38 +02:00
|
|
|
type HttpSink struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
sink
|
2022-02-08 18:06:07 +01:00
|
|
|
client *http.Client
|
|
|
|
url, jwt string
|
|
|
|
encoder *influx.Encoder
|
|
|
|
buffer *bytes.Buffer
|
|
|
|
config HttpSinkConfig
|
|
|
|
maxIdleConns int
|
|
|
|
idleConnTimeout time.Duration
|
|
|
|
timeout time.Duration
|
2022-02-09 19:47:49 +01:00
|
|
|
batchCounter int
|
2021-10-12 13:44:38 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
func (s *HttpSink) Init(config json.RawMessage) error {
|
2022-02-09 19:47:49 +01:00
|
|
|
// Set default values
|
2022-01-25 15:37:43 +01:00
|
|
|
s.name = "HttpSink"
|
2022-02-04 18:12:24 +01:00
|
|
|
s.config.SSL = false
|
2022-02-08 18:06:07 +01:00
|
|
|
s.config.MaxIdleConns = 10
|
|
|
|
s.config.IdleConnTimeout = "5s"
|
|
|
|
s.config.Timeout = "5s"
|
2022-02-09 19:47:49 +01:00
|
|
|
s.config.BatchSize = 20
|
|
|
|
|
2022-02-09 23:22:54 +01:00
|
|
|
// Reset counter
|
|
|
|
s.batchCounter = 0
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Read config
|
2022-02-04 18:12:24 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &s.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.config.Host) == 0 || len(s.config.Port) == 0 || len(s.config.Database) == 0 {
|
2021-10-12 13:44:38 +02:00
|
|
|
return errors.New("`host`, `port` and `database` config options required for TCP sink")
|
|
|
|
}
|
2022-02-08 18:06:07 +01:00
|
|
|
if s.config.MaxIdleConns > 0 {
|
|
|
|
s.maxIdleConns = s.config.MaxIdleConns
|
|
|
|
}
|
|
|
|
if len(s.config.IdleConnTimeout) > 0 {
|
|
|
|
t, err := time.ParseDuration(s.config.IdleConnTimeout)
|
|
|
|
if err == nil {
|
|
|
|
s.idleConnTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.config.Timeout) > 0 {
|
|
|
|
t, err := time.ParseDuration(s.config.Timeout)
|
|
|
|
if err == nil {
|
|
|
|
s.timeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr := &http.Transport{
|
|
|
|
MaxIdleConns: s.maxIdleConns,
|
|
|
|
IdleConnTimeout: s.idleConnTimeout,
|
|
|
|
}
|
|
|
|
s.client = &http.Client{Transport: tr, Timeout: s.timeout}
|
2022-02-04 12:39:25 +01:00
|
|
|
proto := "http"
|
2022-02-04 18:12:24 +01:00
|
|
|
if s.config.SSL {
|
2022-02-04 12:39:25 +01:00
|
|
|
proto = "https"
|
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
s.url = fmt.Sprintf("%s://%s:%s/%s", proto, s.config.Host, s.config.Port, s.config.Database)
|
|
|
|
s.jwt = s.config.JWT
|
2021-10-12 13:44:38 +02:00
|
|
|
s.buffer = &bytes.Buffer{}
|
2022-01-25 15:37:43 +01:00
|
|
|
s.encoder = influx.NewEncoder(s.buffer)
|
2021-10-12 13:44:38 +02:00
|
|
|
s.encoder.SetPrecision(time.Second)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:23:19 +01:00
|
|
|
func (s *HttpSink) Write(m lp.CCMetric) error {
|
2022-02-08 18:06:07 +01:00
|
|
|
p := m.ToPoint(s.config.MetaAsTags)
|
|
|
|
_, err := s.encoder.Encode(p)
|
2022-02-09 19:47:49 +01:00
|
|
|
|
|
|
|
// Flush when received more metrics than batch size
|
|
|
|
s.batchCounter++
|
|
|
|
if s.batchCounter > s.config.BatchSize {
|
|
|
|
s.Flush()
|
|
|
|
}
|
2021-10-12 13:44:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Flush() error {
|
2022-02-09 23:22:54 +01:00
|
|
|
// Do not flush empty buffer
|
|
|
|
if s.batchCounter == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset counter
|
|
|
|
s.batchCounter = 0
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Create new request to send buffer
|
2021-10-12 13:44:38 +02:00
|
|
|
req, err := http.NewRequest(http.MethodPost, s.url, s.buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Set authorization header
|
2021-10-12 13:44:38 +02:00
|
|
|
if len(s.jwt) != 0 {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.jwt))
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Send
|
2021-10-12 13:44:38 +02:00
|
|
|
res, err := s.client.Do(req)
|
2022-02-09 19:47:49 +01:00
|
|
|
|
|
|
|
// Clear buffer
|
2021-10-12 13:44:38 +02:00
|
|
|
s.buffer.Reset()
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Handle error code
|
2021-10-12 13:44:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Handle status code
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2021-10-12 13:44:38 +02:00
|
|
|
return errors.New(res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Close() {
|
2022-02-09 19:47:49 +01:00
|
|
|
s.Flush()
|
2021-10-12 13:44:38 +02:00
|
|
|
s.client.CloseIdleConnections()
|
|
|
|
}
|