2021-03-29 14:14:38 +02:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-18 15:16:10 +02:00
|
|
|
"errors"
|
2021-03-29 14:14:38 +02:00
|
|
|
"fmt"
|
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"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:16:10 +02:00
|
|
|
func (s *NatsSink) connect() error {
|
2021-03-29 14:14:38 +02:00
|
|
|
uinfo := nats.UserInfo(s.user, s.password)
|
|
|
|
uri := fmt.Sprintf("nats://%s:%s", s.host, s.port)
|
2021-05-18 15:16:10 +02:00
|
|
|
log.Print("Using URI ", uri)
|
|
|
|
s.client = nil
|
2021-03-29 14:14:38 +02:00
|
|
|
nc, err := nats.Connect(uri, uinfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.client = nc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (s *NatsSink) Init(config sinkConfig) error {
|
|
|
|
s.name = "NatsSink"
|
2021-05-18 15:16:10 +02:00
|
|
|
if len(config.Host) == 0 ||
|
|
|
|
len(config.Port) == 0 ||
|
|
|
|
len(config.Database) == 0 {
|
|
|
|
return errors.New("Not all configuration variables set required by NatsSink")
|
2021-03-29 14:14:38 +02:00
|
|
|
}
|
2021-05-18 15:16:10 +02:00
|
|
|
s.host = config.Host
|
|
|
|
s.port = config.Port
|
|
|
|
s.database = config.Database
|
|
|
|
s.organization = config.Organization
|
|
|
|
s.user = config.User
|
|
|
|
s.password = config.Password
|
|
|
|
// Setup Influx line protocol
|
|
|
|
s.buffer = &bytes.Buffer{}
|
|
|
|
s.buffer.Grow(1025)
|
2022-01-25 15:37:43 +01:00
|
|
|
s.encoder = influx.NewEncoder(s.buffer)
|
2021-05-18 15:16:10 +02:00
|
|
|
s.encoder.SetPrecision(time.Second)
|
|
|
|
s.encoder.SetMaxLineBytes(1024)
|
|
|
|
// Setup infos for connection
|
|
|
|
return s.connect()
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (s *NatsSink) Write(point lp.CCMetric) error {
|
2021-05-18 15:16:10 +02:00
|
|
|
if s.client != nil {
|
2021-10-04 15:23:43 +02:00
|
|
|
_, err := s.encoder.Encode(point)
|
2021-05-18 15:16:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
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 {
|
|
|
|
if err := s.client.Publish(s.database, s.buffer.Bytes()); err != nil {
|
|
|
|
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
|
|
|
log.Print("Closing Nats connection")
|
|
|
|
if s.client != nil {
|
|
|
|
s.client.Close()
|
|
|
|
}
|
2021-03-29 14:14:38 +02:00
|
|
|
}
|