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-01-25 17:43:10 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-26 16:54:51 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2022-01-25 15:37:43 +01:00
|
|
|
)
|
|
|
|
|
2022-01-28 15:16:58 +01:00
|
|
|
// Map of all available sinks
|
2022-01-25 15:37:43 +01:00
|
|
|
var AvailableSinks = map[string]Sink{
|
2022-02-07 16:51:46 +01:00
|
|
|
"influxdb": new(InfluxSink),
|
|
|
|
"stdout": new(StdoutSink),
|
|
|
|
"nats": new(NatsSink),
|
|
|
|
"http": new(HttpSink),
|
|
|
|
"ganglia": new(GangliaSink),
|
|
|
|
"influxasync": new(InfluxAsyncSink),
|
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-04 18:12:24 +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
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-28 15:16:58 +01:00
|
|
|
|
|
|
|
// Read sink config file
|
2022-01-25 15:37:43 +01:00
|
|
|
if len(sinkConfigFile) > 0 {
|
|
|
|
configFile, err := os.Open(sinkConfigFile)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("SinkManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
jsonParser := json.NewDecoder(configFile)
|
2022-02-04 18:12:24 +01:00
|
|
|
var rawConfigs map[string]json.RawMessage
|
2022-01-25 15:37:43 +01:00
|
|
|
err = jsonParser.Decode(&rawConfigs)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("SinkManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
for name, raw := range rawConfigs {
|
|
|
|
err = sm.AddOutput(name, raw)
|
2022-01-25 15:37:43 +01:00
|
|
|
if err != nil {
|
2022-02-04 18:12:24 +01:00
|
|
|
cclog.ComponentError("SinkManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *sinkManager) Start() {
|
|
|
|
batchcount := 20
|
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-27 17:43:00 +01:00
|
|
|
s.Flush()
|
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-01-25 15:37:43 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sm.done:
|
2022-01-26 16:54:51 +01:00
|
|
|
done()
|
|
|
|
return
|
2022-01-28 15:16:58 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
case p := <-sm.input:
|
2022-01-28 15:16:58 +01:00
|
|
|
// Send received metric to all outputs
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("SinkManager", "WRITE", p)
|
2022-02-04 18:12:24 +01:00
|
|
|
for _, s := range sm.sinks {
|
2022-01-25 15:37:43 +01:00
|
|
|
s.Write(p)
|
|
|
|
}
|
2022-01-28 15:16:58 +01:00
|
|
|
|
|
|
|
// Flush all outputs
|
2022-01-25 15:37:43 +01:00
|
|
|
if batchcount == 0 {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("SinkManager", "FLUSH")
|
2022-02-04 18:12:24 +01:00
|
|
|
for _, s := range sm.sinks {
|
2022-01-25 15:37:43 +01:00
|
|
|
s.Flush()
|
|
|
|
}
|
|
|
|
batchcount = 20
|
|
|
|
}
|
|
|
|
batchcount--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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 {
|
|
|
|
cclog.ComponentError("SinkManager", "SKIP", name, "unknown sink:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-04 18:12:24 +01:00
|
|
|
s := AvailableSinks[sinkConfig.Type]
|
|
|
|
err = s.Init(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) {
|
|
|
|
sm := &sinkManager{}
|
|
|
|
err := sm.Init(wg, sinkConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sm, err
|
|
|
|
}
|