2022-01-25 15:37:43 +01:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-02-04 18:12:24 +01:00
|
|
|
"fmt"
|
2022-01-25 15:37:43 +01:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2022-10-10 11:53:11 +02:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
2022-01-25 15:37:43 +01:00
|
|
|
)
|
|
|
|
|
2022-02-14 18:12:50 +01:00
|
|
|
const SINK_MAX_FORWARD = 50
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Map of all available sinks
|
2022-02-22 16:15:25 +01:00
|
|
|
var AvailableSinks = map[string]func(name string, config json.RawMessage) (Sink, error){
|
|
|
|
"ganglia": NewGangliaSink,
|
|
|
|
"libganglia": NewLibgangliaSink,
|
|
|
|
"stdout": NewStdoutSink,
|
|
|
|
"nats": NewNatsSink,
|
|
|
|
"influxdb": NewInfluxSink,
|
|
|
|
"influxasync": NewInfluxAsyncSink,
|
|
|
|
"http": NewHttpSink,
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Metric collector manager data structure
|
2022-01-25 15:37:43 +01:00
|
|
|
type sinkManager struct {
|
2022-02-14 18:12:50 +01:00
|
|
|
input chan lp.CCMetric // input channel
|
|
|
|
done chan bool // channel to finish / stop metric sink manager
|
|
|
|
wg *sync.WaitGroup // wait group for all goroutines in cc-metric-collector
|
|
|
|
sinks map[string]Sink // Mapping sink name to sink
|
|
|
|
maxForward int // number of metrics to write maximally in one iteration
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Sink manager access functions
|
2022-01-25 15:37:43 +01:00
|
|
|
type SinkManager interface {
|
|
|
|
Init(wg *sync.WaitGroup, sinkConfigFile string) error
|
|
|
|
AddInput(input chan lp.CCMetric)
|
2022-02-04 18:12:24 +01:00
|
|
|
AddOutput(name string, config json.RawMessage) error
|
2022-01-25 15:37:43 +01:00
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2022-02-09 10:09:03 +01:00
|
|
|
// Init initializes the sink manager by:
|
|
|
|
// * Reading its configuration file
|
|
|
|
// * Adding the configured sinks and providing them with the corresponding config
|
2022-01-25 15:37:43 +01:00
|
|
|
func (sm *sinkManager) Init(wg *sync.WaitGroup, sinkConfigFile string) error {
|
|
|
|
sm.input = nil
|
|
|
|
sm.done = make(chan bool)
|
|
|
|
sm.wg = wg
|
2022-02-04 18:12:24 +01:00
|
|
|
sm.sinks = make(map[string]Sink, 0)
|
2022-02-14 18:12:50 +01:00
|
|
|
sm.maxForward = SINK_MAX_FORWARD
|
2022-01-28 15:16:58 +01:00
|
|
|
|
2022-02-09 10:09:03 +01:00
|
|
|
if len(sinkConfigFile) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Read sink config file
|
2022-02-09 10:09:03 +01:00
|
|
|
configFile, err := os.Open(sinkConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError("SinkManager", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
// Parse config
|
|
|
|
jsonParser := json.NewDecoder(configFile)
|
|
|
|
var rawConfigs map[string]json.RawMessage
|
|
|
|
err = jsonParser.Decode(&rawConfigs)
|
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError("SinkManager", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start sinks
|
|
|
|
for name, raw := range rawConfigs {
|
|
|
|
err = sm.AddOutput(name, raw)
|
2022-01-25 15:37:43 +01:00
|
|
|
if err != nil {
|
2022-06-23 15:44:02 +02:00
|
|
|
cclog.ComponentError("SinkManager", err)
|
2022-02-09 10:09:03 +01:00
|
|
|
continue
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-09 10:09:03 +01:00
|
|
|
|
2022-06-23 15:44:02 +02:00
|
|
|
// Check that at least one sink is running
|
|
|
|
if !(len(sm.sinks) > 0) {
|
|
|
|
cclog.ComponentError("SinkManager", "Found no usable sinks")
|
2023-07-17 15:20:12 +02:00
|
|
|
return fmt.Errorf("found no usable sinks")
|
2022-06-23 15:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-09 10:09:03 +01:00
|
|
|
// Start starts the sink managers background task, which
|
|
|
|
// distributes received metrics to the sinks
|
2022-01-25 15:37:43 +01:00
|
|
|
func (sm *sinkManager) Start() {
|
2022-01-28 15:16:58 +01:00
|
|
|
sm.wg.Add(1)
|
2022-01-25 15:37:43 +01:00
|
|
|
go func() {
|
2022-01-28 15:16:58 +01:00
|
|
|
defer sm.wg.Done()
|
|
|
|
|
|
|
|
// Sink manager is done
|
2022-01-26 16:54:51 +01:00
|
|
|
done := func() {
|
2022-02-04 18:12:24 +01:00
|
|
|
for _, s := range sm.sinks {
|
2022-01-26 16:54:51 +01:00
|
|
|
s.Close()
|
|
|
|
}
|
2022-01-28 15:16:58 +01:00
|
|
|
|
2022-01-30 12:08:33 +01:00
|
|
|
close(sm.done)
|
2022-01-27 17:43:00 +01:00
|
|
|
cclog.ComponentDebug("SinkManager", "DONE")
|
2022-01-26 16:54:51 +01:00
|
|
|
}
|
2022-01-28 15:16:58 +01:00
|
|
|
|
2022-02-14 18:53:37 +01:00
|
|
|
toTheSinks := func(p lp.CCMetric) {
|
|
|
|
// Send received metric to all outputs
|
|
|
|
cclog.ComponentDebug("SinkManager", "WRITE", p)
|
|
|
|
for _, s := range sm.sinks {
|
|
|
|
if err := s.Write(p); err != nil {
|
|
|
|
cclog.ComponentError("SinkManager", "WRITE", s.Name(), "write failed:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 18:53:37 +01:00
|
|
|
}
|
2022-02-14 18:12:50 +01:00
|
|
|
|
2022-02-14 18:53:37 +01:00
|
|
|
for {
|
2022-02-14 18:12:50 +01:00
|
|
|
select {
|
|
|
|
case <-sm.done:
|
|
|
|
done()
|
|
|
|
return
|
|
|
|
|
|
|
|
case p := <-sm.input:
|
|
|
|
toTheSinks(p)
|
|
|
|
for i := 0; len(sm.input) > 0 && i < sm.maxForward; i++ {
|
|
|
|
p := <-sm.input
|
|
|
|
toTheSinks(p)
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
}()
|
2022-01-28 15:16:58 +01:00
|
|
|
|
|
|
|
// Sink manager is started
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("SinkManager", "STARTED")
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// AddInput adds the input channel to the sink manager
|
2022-01-25 15:37:43 +01:00
|
|
|
func (sm *sinkManager) AddInput(input chan lp.CCMetric) {
|
|
|
|
sm.input = input
|
|
|
|
}
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
func (sm *sinkManager) AddOutput(name string, rawConfig json.RawMessage) error {
|
2022-01-25 15:37:43 +01:00
|
|
|
var err error
|
2022-02-04 18:12:24 +01:00
|
|
|
var sinkConfig defaultSinkConfig
|
|
|
|
if len(rawConfig) > 0 {
|
|
|
|
err := json.Unmarshal(rawConfig, &sinkConfig)
|
2022-01-25 15:37:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
if _, found := AvailableSinks[sinkConfig.Type]; !found {
|
2022-02-10 13:12:32 +01:00
|
|
|
cclog.ComponentError("SinkManager", "SKIP", name, "unknown sink:", sinkConfig.Type)
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-22 16:15:25 +01:00
|
|
|
s, err := AvailableSinks[sinkConfig.Type](name, rawConfig)
|
2022-01-25 15:37:43 +01:00
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("SinkManager", "SKIP", s.Name(), "initialization failed:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
sm.sinks[name] = s
|
|
|
|
cclog.ComponentDebug("SinkManager", "ADD SINK", s.Name(), "with name", fmt.Sprintf("'%s'", name))
|
2022-01-25 15:37:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Close finishes / stops the sink manager
|
2022-01-25 15:37:43 +01:00
|
|
|
func (sm *sinkManager) Close() {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("SinkManager", "CLOSE")
|
2022-01-27 17:43:00 +01:00
|
|
|
sm.done <- true
|
2022-01-30 12:08:33 +01:00
|
|
|
// wait for close of channel sm.done
|
|
|
|
<-sm.done
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// New creates a new initialized sink manager
|
2022-01-25 15:37:43 +01:00
|
|
|
func New(wg *sync.WaitGroup, sinkConfigFile string) (SinkManager, error) {
|
2022-02-09 10:09:03 +01:00
|
|
|
sm := new(sinkManager)
|
2022-01-25 15:37:43 +01:00
|
|
|
err := sm.Init(wg, sinkConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sm, err
|
|
|
|
}
|