2021-03-29 14:14:38 +02:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-04 18:12:24 +01:00
|
|
|
"encoding/json"
|
2021-05-18 15:16:10 +02:00
|
|
|
"errors"
|
2021-03-29 14:14:38 +02:00
|
|
|
"fmt"
|
2022-02-04 18:12:24 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
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-03-29 14:14:38 +02:00
|
|
|
nats "github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
type NatsSinkConfig struct {
|
|
|
|
defaultSinkConfig
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
Port string `json:"port,omitempty"`
|
|
|
|
Database string `json:"database,omitempty"`
|
|
|
|
User string `json:"user,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:14:38 +02:00
|
|
|
type NatsSink struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
sink
|
2021-03-29 14:14:38 +02:00
|
|
|
client *nats.Conn
|
2022-01-25 15:37:43 +01:00
|
|
|
encoder *influx.Encoder
|
2021-03-29 14:14:38 +02:00
|
|
|
buffer *bytes.Buffer
|
2022-02-04 18:12:24 +01:00
|
|
|
config NatsSinkConfig
|
2021-03-29 14:14:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:16:10 +02:00
|
|
|
func (s *NatsSink) connect() error {
|
2022-02-04 18:12:24 +01:00
|
|
|
var err error
|
|
|
|
var uinfo nats.Option = nil
|
|
|
|
var nc *nats.Conn
|
|
|
|
if len(s.config.User) > 0 && len(s.config.Password) > 0 {
|
|
|
|
uinfo = nats.UserInfo(s.config.User, s.config.Password)
|
|
|
|
}
|
|
|
|
uri := fmt.Sprintf("nats://%s:%s", s.config.Host, s.config.Port)
|
|
|
|
cclog.ComponentDebug(s.name, "Connect to", uri)
|
2021-05-18 15:16:10 +02:00
|
|
|
s.client = nil
|
2022-02-04 18:12:24 +01:00
|
|
|
if uinfo != nil {
|
|
|
|
nc, err = nats.Connect(uri, uinfo)
|
|
|
|
} else {
|
|
|
|
nc, err = nats.Connect(uri)
|
|
|
|
}
|
2021-03-29 14:14:38 +02:00
|
|
|
if err != nil {
|
2022-02-04 18:12:24 +01:00
|
|
|
cclog.ComponentError(s.name, "Connect to", uri, "failed:", err.Error())
|
2021-03-29 14:14:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.client = nc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:23:19 +01:00
|
|
|
func (s *NatsSink) Write(m lp.CCMetric) error {
|
2021-05-18 15:16:10 +02:00
|
|
|
if s.client != nil {
|
2022-03-15 16:41:11 +01:00
|
|
|
_, err := s.encoder.Encode(m.ToPoint(s.meta_as_tags))
|
2021-05-18 15:16:10 +02:00
|
|
|
if err != nil {
|
2022-02-04 18:12:24 +01:00
|
|
|
cclog.ComponentError(s.name, "Write:", err.Error())
|
2021-05-18 15:16:10 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-03-29 14:14:38 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:43:58 +02:00
|
|
|
func (s *NatsSink) Flush() error {
|
2022-01-25 15:37:43 +01:00
|
|
|
if s.client != nil {
|
2022-02-04 18:12:24 +01:00
|
|
|
if err := s.client.Publish(s.config.Database, s.buffer.Bytes()); err != nil {
|
|
|
|
cclog.ComponentError(s.name, "Flush:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.buffer.Reset()
|
|
|
|
}
|
2021-10-12 13:43:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:14:38 +02:00
|
|
|
func (s *NatsSink) Close() {
|
2021-05-18 15:16:10 +02:00
|
|
|
if s.client != nil {
|
2022-02-04 18:12:24 +01:00
|
|
|
cclog.ComponentDebug(s.name, "Close")
|
2021-05-18 15:16:10 +02:00
|
|
|
s.client.Close()
|
|
|
|
}
|
2021-03-29 14:14:38 +02:00
|
|
|
}
|
2022-02-25 14:49:49 +01:00
|
|
|
|
|
|
|
func NewNatsSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
s := new(NatsSink)
|
|
|
|
s.name = fmt.Sprintf("NatsSink(%s)", name)
|
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &s.config)
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.config.Host) == 0 ||
|
|
|
|
len(s.config.Port) == 0 ||
|
|
|
|
len(s.config.Database) == 0 {
|
|
|
|
return nil, errors.New("not all configuration variables set required by NatsSink")
|
|
|
|
}
|
2022-03-15 16:41:11 +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-25 14:49:49 +01:00
|
|
|
// Setup Influx line protocol
|
|
|
|
s.buffer = &bytes.Buffer{}
|
|
|
|
s.buffer.Grow(1025)
|
|
|
|
s.encoder = influx.NewEncoder(s.buffer)
|
|
|
|
s.encoder.SetPrecision(time.Second)
|
|
|
|
s.encoder.SetMaxLineBytes(1024)
|
|
|
|
// Setup infos for connection
|
|
|
|
if err := s.connect(); err != nil {
|
2022-03-15 16:41:11 +01:00
|
|
|
return nil, fmt.Errorf("unable to connect: %v", err)
|
2022-02-25 14:49:49 +01:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|