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"
|
2022-02-10 13:12:32 +01:00
|
|
|
"sync"
|
2021-10-12 13:44:38 +02:00
|
|
|
"time"
|
|
|
|
|
2022-02-10 13:12:32 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
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-10 13:12:32 +01:00
|
|
|
URL string `json:"url,omitempty"`
|
2022-02-08 18:06:07 +01:00
|
|
|
JWT string `json:"jwt,omitempty"`
|
|
|
|
Timeout string `json:"timeout,omitempty"`
|
|
|
|
MaxIdleConns int `json:"max_idle_connections,omitempty"`
|
|
|
|
IdleConnTimeout string `json:"idle_connection_timeout,omitempty"`
|
2022-02-10 13:12:32 +01:00
|
|
|
FlushDelay string `json:"flush_delay,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
|
|
|
|
encoder *influx.Encoder
|
2022-02-10 13:12:32 +01:00
|
|
|
lock sync.Mutex // Flush() runs in another goroutine, so this lock has to protect the buffer
|
2022-02-08 18:06:07 +01:00
|
|
|
buffer *bytes.Buffer
|
2022-02-10 13:12:32 +01:00
|
|
|
flushTimer *time.Timer
|
2022-02-08 18:06:07 +01:00
|
|
|
config HttpSinkConfig
|
|
|
|
maxIdleConns int
|
|
|
|
idleConnTimeout time.Duration
|
|
|
|
timeout time.Duration
|
2022-02-10 13:12:32 +01:00
|
|
|
flushDelay time.Duration
|
2021-10-12 13:44:38 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 11:23:19 +01:00
|
|
|
func (s *HttpSink) Write(m lp.CCMetric) error {
|
2022-02-10 13:12:32 +01:00
|
|
|
if s.buffer.Len() == 0 && s.flushDelay != 0 {
|
|
|
|
// This is the first write since the last flush, start the flushTimer!
|
|
|
|
if s.flushTimer != nil && s.flushTimer.Stop() {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentDebug(s.name, "unexpected: the flushTimer was already running?")
|
2022-02-10 13:12:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a batched flush for all lines that have arrived in the last second
|
|
|
|
s.flushTimer = time.AfterFunc(s.flushDelay, func() {
|
|
|
|
if err := s.Flush(); err != nil {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentError(s.name, "flush failed:", err.Error())
|
2022-02-10 13:12:32 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-15 16:16:26 +01:00
|
|
|
p := m.ToPoint(s.meta_as_tags)
|
2022-02-10 13:12:32 +01:00
|
|
|
|
|
|
|
s.lock.Lock()
|
2022-02-08 18:06:07 +01:00
|
|
|
_, err := s.encoder.Encode(p)
|
2022-02-10 13:12:32 +01:00
|
|
|
s.lock.Unlock() // defer does not work here as Flush() takes the lock as well
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentError(s.name, "encoding failed:", err.Error())
|
2022-02-10 13:12:32 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-09 19:47:49 +01:00
|
|
|
|
2022-02-10 13:12:32 +01:00
|
|
|
// Flush synchronously if "flush_delay" is zero
|
|
|
|
if s.flushDelay == 0 {
|
|
|
|
return s.Flush()
|
2022-02-09 19:47:49 +01:00
|
|
|
}
|
2022-02-10 13:12:32 +01:00
|
|
|
|
2021-10-12 13:44:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Flush() error {
|
2022-02-10 13:12:32 +01:00
|
|
|
// buffer is read by client.Do, prevent concurrent modifications
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2022-02-09 23:22:54 +01:00
|
|
|
// Do not flush empty buffer
|
2022-02-10 13:12:32 +01:00
|
|
|
if s.buffer.Len() == 0 {
|
2022-02-09 23:22:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Create new request to send buffer
|
2022-02-10 13:12:32 +01:00
|
|
|
req, err := http.NewRequest(http.MethodPost, s.config.URL, s.buffer)
|
2021-10-12 13:44:38 +02:00
|
|
|
if err != nil {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentError(s.name, "failed to create request:", err.Error())
|
2021-10-12 13:44:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:49 +01:00
|
|
|
// Set authorization header
|
2022-02-10 13:12:32 +01:00
|
|
|
if len(s.config.JWT) != 0 {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.config.JWT))
|
2021-10-12 13:44:38 +02:00
|
|
|
}
|
|
|
|
|
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-10 13:12:32 +01:00
|
|
|
// Handle transport/tcp errors
|
2021-10-12 13:44:38 +02:00
|
|
|
if err != nil {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentError(s.name, "transport/tcp error:", err.Error())
|
2021-10-12 13:44:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:12:32 +01:00
|
|
|
// Handle application errors
|
2022-02-09 19:47:49 +01:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2022-04-01 18:36:54 +02:00
|
|
|
err = errors.New(res.Status)
|
|
|
|
cclog.ComponentError(s.name, "application error:", err.Error())
|
|
|
|
return err
|
2021-10-12 13:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Close() {
|
2022-02-10 13:12:32 +01:00
|
|
|
s.flushTimer.Stop()
|
|
|
|
if err := s.Flush(); err != nil {
|
2022-04-01 18:36:54 +02:00
|
|
|
cclog.ComponentError(s.name, "flush failed:", err.Error())
|
2022-02-10 13:12:32 +01:00
|
|
|
}
|
2021-10-12 13:44:38 +02:00
|
|
|
s.client.CloseIdleConnections()
|
|
|
|
}
|
2022-02-22 16:15:25 +01:00
|
|
|
|
|
|
|
func NewHttpSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
s := new(HttpSink)
|
2022-02-23 14:56:29 +01:00
|
|
|
// Set default values
|
|
|
|
s.name = fmt.Sprintf("HttpSink(%s)", name)
|
|
|
|
s.config.MaxIdleConns = 10
|
|
|
|
s.config.IdleConnTimeout = "5s"
|
|
|
|
s.config.Timeout = "5s"
|
|
|
|
s.config.FlushDelay = "1s"
|
|
|
|
|
|
|
|
// Read config
|
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &s.config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.config.URL) == 0 {
|
|
|
|
return nil, errors.New("`url` config option is required for HTTP sink")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.config.FlushDelay) > 0 {
|
|
|
|
t, err := time.ParseDuration(s.config.FlushDelay)
|
|
|
|
if err == nil {
|
|
|
|
s.flushDelay = t
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 16:16:26 +01:00
|
|
|
// Create lookup map to use meta infos as tags in the output metric
|
|
|
|
s.meta_as_tags = make(map[string]bool)
|
|
|
|
for _, k := range s.config.MetaAsTags {
|
|
|
|
s.meta_as_tags[k] = true
|
|
|
|
}
|
2022-02-23 14:56:29 +01:00
|
|
|
tr := &http.Transport{
|
|
|
|
MaxIdleConns: s.maxIdleConns,
|
|
|
|
IdleConnTimeout: s.idleConnTimeout,
|
|
|
|
}
|
|
|
|
s.client = &http.Client{Transport: tr, Timeout: s.timeout}
|
|
|
|
s.buffer = &bytes.Buffer{}
|
|
|
|
s.encoder = influx.NewEncoder(s.buffer)
|
|
|
|
s.encoder.SetPrecision(time.Second)
|
2022-02-22 16:15:25 +01:00
|
|
|
return s, nil
|
|
|
|
}
|