2022-01-25 15:37:43 +01:00
|
|
|
package receivers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-25 15:37:43 +01:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var AvailableReceivers = map[string]Receiver{
|
|
|
|
"nats": &NatsReceiver{},
|
|
|
|
}
|
|
|
|
|
|
|
|
type receiveManager struct {
|
|
|
|
inputs []Receiver
|
|
|
|
output chan lp.CCMetric
|
|
|
|
done chan bool
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
config []ReceiverConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReceiveManager interface {
|
|
|
|
Init(wg *sync.WaitGroup, receiverConfigFile string) error
|
|
|
|
AddInput(rawConfig json.RawMessage) error
|
|
|
|
AddOutput(output chan lp.CCMetric)
|
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Init(wg *sync.WaitGroup, receiverConfigFile string) error {
|
|
|
|
rm.inputs = make([]Receiver, 0)
|
|
|
|
rm.output = nil
|
|
|
|
rm.done = make(chan bool)
|
|
|
|
rm.wg = wg
|
|
|
|
rm.config = make([]ReceiverConfig, 0)
|
|
|
|
configFile, err := os.Open(receiverConfigFile)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
jsonParser := json.NewDecoder(configFile)
|
|
|
|
var rawConfigs []json.RawMessage
|
|
|
|
err = jsonParser.Decode(&rawConfigs)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, raw := range rawConfigs {
|
|
|
|
rm.AddInput(raw)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Start() {
|
|
|
|
rm.wg.Add(1)
|
|
|
|
|
|
|
|
for _, r := range rm.inputs {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "START", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
r.Start()
|
|
|
|
}
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "STARTED")
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) AddInput(rawConfig json.RawMessage) error {
|
|
|
|
var config ReceiverConfig
|
|
|
|
err := json.Unmarshal(rawConfig, &config)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", config.Type, "JSON config error:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, found := AvailableReceivers[config.Type]; !found {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", config.Type, "unknown receiver:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r := AvailableReceivers[config.Type]
|
|
|
|
err = r.Init(config)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", r.Name(), "initialization failed:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
rm.inputs = append(rm.inputs, r)
|
|
|
|
rm.config = append(rm.config, config)
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "ADD RECEIVER", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) AddOutput(output chan lp.CCMetric) {
|
|
|
|
rm.output = output
|
|
|
|
for _, r := range rm.inputs {
|
|
|
|
r.SetSink(rm.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Close() {
|
|
|
|
for _, r := range rm.inputs {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "CLOSE", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
rm.wg.Done()
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "CLOSE")
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(wg *sync.WaitGroup, receiverConfigFile string) (ReceiveManager, error) {
|
|
|
|
r := &receiveManager{}
|
|
|
|
err := r.Init(wg, receiverConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|