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
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:44:38 +02:00
|
|
|
type HttpSink struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
sink
|
2021-10-12 13:44:38 +02:00
|
|
|
client *http.Client
|
|
|
|
url, jwt string
|
2022-01-25 15:37:43 +01:00
|
|
|
encoder *influx.Encoder
|
2021-10-12 13:44:38 +02:00
|
|
|
buffer *bytes.Buffer
|
2022-02-04 18:12:24 +01:00
|
|
|
config HttpSinkConfig
|
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-01-25 15:37:43 +01:00
|
|
|
s.name = "HttpSink"
|
2022-02-04 18:12:24 +01:00
|
|
|
s.config.SSL = false
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.client = &http.Client{}
|
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 {
|
|
|
|
_, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags))
|
2021-10-12 13:44:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Flush() error {
|
|
|
|
req, err := http.NewRequest(http.MethodPost, s.url, s.buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.jwt) != 0 {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.jwt))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.client.Do(req)
|
|
|
|
s.buffer.Reset()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return errors.New(res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HttpSink) Close() {
|
|
|
|
s.client.CloseIdleConnections()
|
|
|
|
}
|