|
|
|
@@ -2,26 +2,28 @@ package sinks
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
|
|
|
|
"github.com/go-mqtt/mqtt"
|
|
|
|
|
influx "github.com/influxdata/line-protocol/v2/lineprotocol"
|
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AmqpSinkConfig struct {
|
|
|
|
|
type MqttSinkConfig struct {
|
|
|
|
|
// defines JSON tags for 'type' and 'meta_as_tags' (string list)
|
|
|
|
|
// See: metricSink.go
|
|
|
|
|
defaultSinkConfig
|
|
|
|
|
// Additional config options, for AmqpSink
|
|
|
|
|
QueueName string `json:"queue_name"`
|
|
|
|
|
// Additional config options, for MqttSink
|
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
|
PersistenceDirectory string `json:"persistence_directory,omitempty"`
|
|
|
|
|
// Maximum number of points sent to server in single request.
|
|
|
|
|
// Default: 1000
|
|
|
|
|
BatchSize int `json:"batch_size,omitempty"`
|
|
|
|
@@ -33,18 +35,20 @@ type AmqpSinkConfig struct {
|
|
|
|
|
FlushInterval string `json:"flush_delay,omitempty"`
|
|
|
|
|
flushDelay time.Duration
|
|
|
|
|
|
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
|
Port int `json:"port"`
|
|
|
|
|
PublishTimeout string `json:"publish_timeout,omitempty"`
|
|
|
|
|
publishTimeout time.Duration
|
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
|
DialProtocol string `json:"dial_protocol"`
|
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
|
Port int `json:"port"`
|
|
|
|
|
PauseTimeout string `json:"pause_timeout"`
|
|
|
|
|
pauseTimeout time.Duration
|
|
|
|
|
KeepAlive uint16 `json:"keep_alive_seconds"`
|
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AmqpSink struct {
|
|
|
|
|
type MqttSink struct {
|
|
|
|
|
// declares elements 'name' and 'meta_as_tags' (string to bool map!)
|
|
|
|
|
sink
|
|
|
|
|
config AmqpSinkConfig // entry point to the AmqpSinkConfig
|
|
|
|
|
config MqttSinkConfig // entry point to the MqttSinkConfig
|
|
|
|
|
// influx line protocol encoder
|
|
|
|
|
encoder influx.Encoder
|
|
|
|
|
// number of records stored in the encoder
|
|
|
|
@@ -63,9 +67,8 @@ type AmqpSink struct {
|
|
|
|
|
// WaitGroup to ensure only one send operation is running at a time
|
|
|
|
|
sendWaitGroup sync.WaitGroup
|
|
|
|
|
|
|
|
|
|
client *amqp.Connection
|
|
|
|
|
channel *amqp.Channel
|
|
|
|
|
queue amqp.Queue
|
|
|
|
|
client *mqtt.Client
|
|
|
|
|
mqttconfig mqtt.Config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement functions required for Sink interface
|
|
|
|
@@ -73,7 +76,7 @@ type AmqpSink struct {
|
|
|
|
|
// See: metricSink.go
|
|
|
|
|
|
|
|
|
|
// Code to submit a single CCMetric to the sink
|
|
|
|
|
func (s *AmqpSink) Write(m lp.CCMetric) error {
|
|
|
|
|
func (s *MqttSink) Write(m lp.CCMetric) error {
|
|
|
|
|
|
|
|
|
|
// Lock for encoder usage
|
|
|
|
|
s.encoderLock.Lock()
|
|
|
|
@@ -194,7 +197,7 @@ func (s *AmqpSink) Write(m lp.CCMetric) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the sink uses batched sends internally, you can tell to flush its buffers
|
|
|
|
|
func (s *AmqpSink) Flush() error {
|
|
|
|
|
func (s *MqttSink) Flush() error {
|
|
|
|
|
|
|
|
|
|
// Lock for encoder usage
|
|
|
|
|
// Own lock for as short as possible: the time it takes to clone the buffer.
|
|
|
|
@@ -219,21 +222,37 @@ func (s *AmqpSink) Flush() error {
|
|
|
|
|
go func() {
|
|
|
|
|
defer s.sendWaitGroup.Done()
|
|
|
|
|
//startTime := time.Now()
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), s.config.flushDelay)
|
|
|
|
|
defer cancel()
|
|
|
|
|
err := s.channel.PublishWithContext(ctx, "", s.queue.Name, false, false, amqp.Publishing{
|
|
|
|
|
ContentType: "text/plain",
|
|
|
|
|
Body: buf,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
cclog.ComponentError(s.name, err.Error())
|
|
|
|
|
for {
|
|
|
|
|
exchange, err := s.client.PublishAtLeastOnce(buf, s.config.ClientID)
|
|
|
|
|
switch {
|
|
|
|
|
case err == nil:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case mqtt.IsDeny(err), errors.Is(err, mqtt.ErrClosed):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case errors.Is(err, mqtt.ErrMax):
|
|
|
|
|
time.Sleep(s.config.pauseTimeout)
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
time.Sleep(s.config.pauseTimeout)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for err := range exchange {
|
|
|
|
|
if errors.Is(err, mqtt.ErrClosed) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close sink: close network connection, close files, close libraries, ...
|
|
|
|
|
func (s *AmqpSink) Close() {
|
|
|
|
|
func (s *MqttSink) Close() {
|
|
|
|
|
|
|
|
|
|
cclog.ComponentDebug(s.name, "CLOSE")
|
|
|
|
|
|
|
|
|
@@ -258,18 +277,19 @@ func (s *AmqpSink) Close() {
|
|
|
|
|
|
|
|
|
|
// New function to create a new instance of the sink
|
|
|
|
|
// Initialize the sink by giving it a name and reading in the config JSON
|
|
|
|
|
func NewAmqpSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
|
s := new(AmqpSink)
|
|
|
|
|
func NewMqttSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
|
s := new(MqttSink)
|
|
|
|
|
|
|
|
|
|
// Set name of sampleSink
|
|
|
|
|
// The name should be chosen in such a way that different instances of AmqpSink can be distinguished
|
|
|
|
|
s.name = fmt.Sprintf("AmqpSink(%s)", name) // Always specify a name here
|
|
|
|
|
// The name should be chosen in such a way that different instances of MqttSink can be distinguished
|
|
|
|
|
s.name = fmt.Sprintf("MqttSink(%s)", name) // Always specify a name here
|
|
|
|
|
|
|
|
|
|
// Set defaults in s.config
|
|
|
|
|
// Allow overwriting these defaults by reading config JSON
|
|
|
|
|
|
|
|
|
|
s.config.PublishTimeout = "4s"
|
|
|
|
|
s.config.publishTimeout = time.Duration(4) * time.Second
|
|
|
|
|
s.config.PauseTimeout = "4s"
|
|
|
|
|
s.config.pauseTimeout = time.Duration(4) * time.Second
|
|
|
|
|
s.config.DialProtocol = "tcp"
|
|
|
|
|
s.config.Hostname = "localhost"
|
|
|
|
|
s.config.Port = 1883
|
|
|
|
|
|
|
|
|
@@ -291,10 +311,10 @@ func NewAmqpSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
|
|
|
|
|
|
// Check if all required fields in the config are set
|
|
|
|
|
// E.g. use 'len(s.config.Option) > 0' for string settings
|
|
|
|
|
if t, err := time.ParseDuration(s.config.PublishTimeout); err == nil {
|
|
|
|
|
s.config.publishTimeout = t
|
|
|
|
|
if t, err := time.ParseDuration(s.config.PauseTimeout); err == nil {
|
|
|
|
|
s.config.pauseTimeout = t
|
|
|
|
|
} else {
|
|
|
|
|
err := fmt.Errorf("to parse duration for PublishTimeout: %s", s.config.PublishTimeout)
|
|
|
|
|
err := fmt.Errorf("to parse duration for PauseTimeout: %s", s.config.PauseTimeout)
|
|
|
|
|
cclog.ComponentError(s.name, err.Error())
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@@ -306,46 +326,46 @@ func NewAmqpSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url := net.JoinHostPort(s.config.Hostname, fmt.Sprintf("%d", s.config.Port))
|
|
|
|
|
userpart := ""
|
|
|
|
|
if len(s.config.Username) > 0 {
|
|
|
|
|
userpart = s.config.Username
|
|
|
|
|
if len(s.config.Password) > 0 {
|
|
|
|
|
userpart += ":" + s.config.Password
|
|
|
|
|
}
|
|
|
|
|
userpart += "@"
|
|
|
|
|
switch s.config.DialProtocol {
|
|
|
|
|
case "tcp", "udp":
|
|
|
|
|
default:
|
|
|
|
|
err := errors.New("failed to parse dial protocol, allowed: tcp, udp")
|
|
|
|
|
cclog.ComponentError(s.name, err.Error())
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var persistence mqtt.Persistence
|
|
|
|
|
if len(s.config.PersistenceDirectory) > 0 {
|
|
|
|
|
persistence = mqtt.FileSystem(s.config.PersistenceDirectory)
|
|
|
|
|
} else {
|
|
|
|
|
tmpdir, err := os.MkdirTemp("", "mqtt")
|
|
|
|
|
if err == nil {
|
|
|
|
|
persistence = mqtt.FileSystem(tmpdir)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
url = fmt.Sprintf("amqp://%s%s", userpart, url)
|
|
|
|
|
|
|
|
|
|
// Establish connection to the server, library, ...
|
|
|
|
|
// Check required files exist and lookup path(s) of executable(s)
|
|
|
|
|
c, err := amqp.Dial(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.client = c
|
|
|
|
|
|
|
|
|
|
ch, err := c.Channel()
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.client.Close()
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.channel = ch
|
|
|
|
|
dialer := mqtt.NewDialer(s.config.DialProtocol, net.JoinHostPort(s.config.Hostname, fmt.Sprintf("%d", s.config.Port)))
|
|
|
|
|
|
|
|
|
|
q, err := ch.QueueDeclare(
|
|
|
|
|
s.config.QueueName, // name
|
|
|
|
|
false, // durable
|
|
|
|
|
false, // delete when unused
|
|
|
|
|
false, // exclusive
|
|
|
|
|
false, // no-wait
|
|
|
|
|
nil, // arguments
|
|
|
|
|
)
|
|
|
|
|
s.mqttconfig = mqtt.Config{
|
|
|
|
|
Dialer: dialer,
|
|
|
|
|
PauseTimeout: s.config.pauseTimeout,
|
|
|
|
|
KeepAlive: uint16(s.config.KeepAlive),
|
|
|
|
|
}
|
|
|
|
|
if len(s.config.Username) > 0 {
|
|
|
|
|
s.mqttconfig.UserName = s.config.Username
|
|
|
|
|
}
|
|
|
|
|
if len(s.config.Password) > 0 {
|
|
|
|
|
s.mqttconfig.Password = []byte(s.config.Password)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := mqtt.InitSession(s.config.ClientID, persistence, &s.mqttconfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.channel.Close()
|
|
|
|
|
s.client.Close()
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.queue = q
|
|
|
|
|
s.client = client
|
|
|
|
|
|
|
|
|
|
// Return (nil, meaningful error message) in case of errors
|
|
|
|
|
return s, nil
|