mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-10-14 02:04:31 +02:00
Use message processor in router, all sinks and all receivers
This commit is contained in:
@@ -10,8 +10,9 @@ import (
|
||||
// "time"
|
||||
"os/exec"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
)
|
||||
|
||||
const GMETRIC_EXEC = `gmetric`
|
||||
@@ -35,50 +36,53 @@ type GangliaSink struct {
|
||||
config GangliaSinkConfig
|
||||
}
|
||||
|
||||
func (s *GangliaSink) Write(point lp.CCMessage) error {
|
||||
func (s *GangliaSink) Write(msg lp.CCMessage) error {
|
||||
var err error = nil
|
||||
//var tagsstr []string
|
||||
var argstr []string
|
||||
|
||||
// Get metric config (type, value, ... in suitable format)
|
||||
conf := GetCommonGangliaConfig(point)
|
||||
if len(conf.Type) == 0 {
|
||||
conf = GetGangliaConfig(point)
|
||||
}
|
||||
if len(conf.Type) == 0 {
|
||||
return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name)
|
||||
}
|
||||
point, err := s.mp.ProcessMessage(msg)
|
||||
if err == nil && point != nil {
|
||||
// Get metric config (type, value, ... in suitable format)
|
||||
conf := GetCommonGangliaConfig(point)
|
||||
if len(conf.Type) == 0 {
|
||||
conf = GetGangliaConfig(point)
|
||||
}
|
||||
if len(conf.Type) == 0 {
|
||||
return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name)
|
||||
}
|
||||
|
||||
if s.config.AddGangliaGroup {
|
||||
argstr = append(argstr, fmt.Sprintf("--group=%s", conf.Group))
|
||||
}
|
||||
if s.config.AddUnits && len(conf.Unit) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--units=%s", conf.Unit))
|
||||
}
|
||||
if s.config.AddGangliaGroup {
|
||||
argstr = append(argstr, fmt.Sprintf("--group=%s", conf.Group))
|
||||
}
|
||||
if s.config.AddUnits && len(conf.Unit) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--units=%s", conf.Unit))
|
||||
}
|
||||
|
||||
if len(s.config.ClusterName) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--cluster=%s", s.config.ClusterName))
|
||||
}
|
||||
// if s.config.AddTagsAsDesc && len(tagsstr) > 0 {
|
||||
// argstr = append(argstr, fmt.Sprintf("--desc=%q", strings.Join(tagsstr, ",")))
|
||||
// }
|
||||
if len(s.gmetric_config) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--conf=%s", s.gmetric_config))
|
||||
}
|
||||
if s.config.AddTypeToName {
|
||||
argstr = append(argstr, fmt.Sprintf("--name=%s", GangliaMetricName(point)))
|
||||
} else {
|
||||
argstr = append(argstr, fmt.Sprintf("--name=%s", conf.Name))
|
||||
}
|
||||
argstr = append(argstr, fmt.Sprintf("--slope=%s", conf.Slope))
|
||||
argstr = append(argstr, fmt.Sprintf("--value=%s", conf.Value))
|
||||
argstr = append(argstr, fmt.Sprintf("--type=%s", conf.Type))
|
||||
argstr = append(argstr, fmt.Sprintf("--tmax=%d", conf.Tmax))
|
||||
if len(s.config.ClusterName) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--cluster=%s", s.config.ClusterName))
|
||||
}
|
||||
// if s.config.AddTagsAsDesc && len(tagsstr) > 0 {
|
||||
// argstr = append(argstr, fmt.Sprintf("--desc=%q", strings.Join(tagsstr, ",")))
|
||||
// }
|
||||
if len(s.gmetric_config) > 0 {
|
||||
argstr = append(argstr, fmt.Sprintf("--conf=%s", s.gmetric_config))
|
||||
}
|
||||
if s.config.AddTypeToName {
|
||||
argstr = append(argstr, fmt.Sprintf("--name=%s", GangliaMetricName(point)))
|
||||
} else {
|
||||
argstr = append(argstr, fmt.Sprintf("--name=%s", conf.Name))
|
||||
}
|
||||
argstr = append(argstr, fmt.Sprintf("--slope=%s", conf.Slope))
|
||||
argstr = append(argstr, fmt.Sprintf("--value=%s", conf.Value))
|
||||
argstr = append(argstr, fmt.Sprintf("--type=%s", conf.Type))
|
||||
argstr = append(argstr, fmt.Sprintf("--tmax=%d", conf.Tmax))
|
||||
|
||||
cclog.ComponentDebug(s.name, s.gmetric_path, strings.Join(argstr, " "))
|
||||
command := exec.Command(s.gmetric_path, argstr...)
|
||||
command.Wait()
|
||||
_, err = command.Output()
|
||||
cclog.ComponentDebug(s.name, s.gmetric_path, strings.Join(argstr, " "))
|
||||
command := exec.Command(s.gmetric_path, argstr...)
|
||||
command.Wait()
|
||||
_, err = command.Output()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -104,6 +108,13 @@ func NewGangliaSink(name string, config json.RawMessage) (Sink, error) {
|
||||
}
|
||||
s.gmetric_path = ""
|
||||
s.gmetric_config = ""
|
||||
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
|
||||
if len(s.config.GmetricPath) > 0 {
|
||||
p, err := exec.LookPath(s.config.GmetricPath)
|
||||
if err == nil {
|
||||
@@ -122,5 +133,15 @@ func NewGangliaSink(name string, config json.RawMessage) (Sink, error) {
|
||||
if len(s.config.GmetricConfig) > 0 {
|
||||
s.gmetric_config = s.config.GmetricConfig
|
||||
}
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = s.mp.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
@@ -9,8 +9,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
influx "github.com/influxdata/line-protocol/v2/lineprotocol"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
@@ -75,28 +76,20 @@ type HttpSink struct {
|
||||
}
|
||||
|
||||
// Write sends metric m as http message
|
||||
func (s *HttpSink) Write(m lp.CCMessage) error {
|
||||
func (s *HttpSink) Write(msg lp.CCMessage) error {
|
||||
|
||||
// Lock for encoder usage
|
||||
s.encoderLock.Lock()
|
||||
// submit m only after applying processing/dropping rules
|
||||
m, err := s.mp.ProcessMessage(msg)
|
||||
if err == nil && m != nil {
|
||||
// Lock for encoder usage
|
||||
s.encoderLock.Lock()
|
||||
|
||||
// Encode measurement name
|
||||
s.encoder.StartLine(m.Name())
|
||||
// Encode measurement name
|
||||
s.encoder.StartLine(m.Name())
|
||||
|
||||
// copy tags and meta data which should be used as tags
|
||||
s.extended_tag_list = s.extended_tag_list[:0]
|
||||
for key, value := range m.Tags() {
|
||||
s.extended_tag_list =
|
||||
append(
|
||||
s.extended_tag_list,
|
||||
key_value_pair{
|
||||
key: key,
|
||||
value: value,
|
||||
},
|
||||
)
|
||||
}
|
||||
for _, key := range s.config.MetaAsTags {
|
||||
if value, ok := m.GetMeta(key); ok {
|
||||
// copy tags and meta data which should be used as tags
|
||||
s.extended_tag_list = s.extended_tag_list[:0]
|
||||
for key, value := range m.Tags() {
|
||||
s.extended_tag_list =
|
||||
append(
|
||||
s.extended_tag_list,
|
||||
@@ -106,45 +99,57 @@ func (s *HttpSink) Write(m lp.CCMessage) error {
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
// for _, key := range s.config.MetaAsTags {
|
||||
// if value, ok := m.GetMeta(key); ok {
|
||||
// s.extended_tag_list =
|
||||
// append(
|
||||
// s.extended_tag_list,
|
||||
// key_value_pair{
|
||||
// key: key,
|
||||
// value: value,
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
// Encode tags (they musts be in lexical order)
|
||||
slices.SortFunc(
|
||||
s.extended_tag_list,
|
||||
func(a key_value_pair, b key_value_pair) int {
|
||||
if a.key < b.key {
|
||||
return -1
|
||||
}
|
||||
if a.key > b.key {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
},
|
||||
)
|
||||
for i := range s.extended_tag_list {
|
||||
s.encoder.AddTag(
|
||||
s.extended_tag_list[i].key,
|
||||
s.extended_tag_list[i].value,
|
||||
// Encode tags (they musts be in lexical order)
|
||||
slices.SortFunc(
|
||||
s.extended_tag_list,
|
||||
func(a key_value_pair, b key_value_pair) int {
|
||||
if a.key < b.key {
|
||||
return -1
|
||||
}
|
||||
if a.key > b.key {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
},
|
||||
)
|
||||
}
|
||||
for i := range s.extended_tag_list {
|
||||
s.encoder.AddTag(
|
||||
s.extended_tag_list[i].key,
|
||||
s.extended_tag_list[i].value,
|
||||
)
|
||||
}
|
||||
|
||||
// Encode fields
|
||||
for key, value := range m.Fields() {
|
||||
s.encoder.AddField(key, influx.MustNewValue(value))
|
||||
}
|
||||
// Encode fields
|
||||
for key, value := range m.Fields() {
|
||||
s.encoder.AddField(key, influx.MustNewValue(value))
|
||||
}
|
||||
|
||||
// Encode time stamp
|
||||
s.encoder.EndLine(m.Time())
|
||||
// Encode time stamp
|
||||
s.encoder.EndLine(m.Time())
|
||||
|
||||
// Check for encoder errors
|
||||
err := s.encoder.Err()
|
||||
// Check for encoder errors
|
||||
err := s.encoder.Err()
|
||||
|
||||
// Unlock encoder usage
|
||||
s.encoderLock.Unlock()
|
||||
// Unlock encoder usage
|
||||
s.encoderLock.Unlock()
|
||||
|
||||
// Check that encoding worked
|
||||
if err != nil {
|
||||
return fmt.Errorf("encoding failed: %v", err)
|
||||
// Check that encoding worked
|
||||
if err != nil {
|
||||
return fmt.Errorf("encoding failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.config.flushDelay == 0 {
|
||||
@@ -297,6 +302,11 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) {
|
||||
if s.config.useBasicAuth && len(s.config.Password) == 0 {
|
||||
return nil, errors.New("basic authentication requires password")
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
|
||||
if len(s.config.IdleConnTimeout) > 0 {
|
||||
t, err := time.ParseDuration(s.config.IdleConnTimeout)
|
||||
@@ -319,6 +329,16 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) {
|
||||
cclog.ComponentDebug(s.name, "Init(): flushDelay", t)
|
||||
}
|
||||
}
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = p.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
precision := influx.Nanosecond
|
||||
if len(s.config.Precision) > 0 {
|
||||
switch s.config.Precision {
|
||||
|
@@ -10,8 +10,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
||||
influxdb2ApiHttp "github.com/influxdata/influxdb-client-go/v2/api/http"
|
||||
@@ -121,9 +122,10 @@ func (s *InfluxAsyncSink) Write(m lp.CCMessage) error {
|
||||
}
|
||||
})
|
||||
}
|
||||
s.writeApi.WritePoint(
|
||||
m.ToPoint(s.meta_as_tags),
|
||||
)
|
||||
msg, err := s.mp.ProcessMessage(m)
|
||||
if err == nil && msg != nil {
|
||||
s.writeApi.WritePoint(msg.ToPoint(nil))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -200,10 +202,24 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) {
|
||||
if len(s.config.Password) == 0 {
|
||||
return nil, errors.New("missing password configuration required by InfluxSink")
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = s.mp.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
s.meta_as_tags = make(map[string]bool)
|
||||
// s.meta_as_tags = make(map[string]bool)
|
||||
// for _, k := range s.config.MetaAsTags {
|
||||
// s.meta_as_tags[k] = true
|
||||
// }
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.meta_as_tags[k] = true
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
toUint := func(duration string, def uint) uint {
|
||||
|
@@ -10,8 +10,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
||||
influx "github.com/influxdata/line-protocol/v2/lineprotocol"
|
||||
@@ -224,28 +225,19 @@ func (s *InfluxSink) connect() error {
|
||||
}
|
||||
|
||||
// Write sends metric m in influxDB line protocol
|
||||
func (s *InfluxSink) Write(m lp.CCMessage) error {
|
||||
func (s *InfluxSink) Write(msg lp.CCMessage) error {
|
||||
|
||||
// Lock for encoder usage
|
||||
s.encoderLock.Lock()
|
||||
m, err := s.mp.ProcessMessage(msg)
|
||||
if err == nil && m != nil {
|
||||
// Lock for encoder usage
|
||||
s.encoderLock.Lock()
|
||||
|
||||
// Encode measurement name
|
||||
s.encoder.StartLine(m.Name())
|
||||
// Encode measurement name
|
||||
s.encoder.StartLine(m.Name())
|
||||
|
||||
// copy tags and meta data which should be used as tags
|
||||
s.extended_tag_list = s.extended_tag_list[:0]
|
||||
for key, value := range m.Tags() {
|
||||
s.extended_tag_list =
|
||||
append(
|
||||
s.extended_tag_list,
|
||||
key_value_pair{
|
||||
key: key,
|
||||
value: value,
|
||||
},
|
||||
)
|
||||
}
|
||||
for _, key := range s.config.MetaAsTags {
|
||||
if value, ok := m.GetMeta(key); ok {
|
||||
// copy tags and meta data which should be used as tags
|
||||
s.extended_tag_list = s.extended_tag_list[:0]
|
||||
for key, value := range m.Tags() {
|
||||
s.extended_tag_list =
|
||||
append(
|
||||
s.extended_tag_list,
|
||||
@@ -255,45 +247,57 @@ func (s *InfluxSink) Write(m lp.CCMessage) error {
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
// for _, key := range s.config.MetaAsTags {
|
||||
// if value, ok := m.GetMeta(key); ok {
|
||||
// s.extended_tag_list =
|
||||
// append(
|
||||
// s.extended_tag_list,
|
||||
// key_value_pair{
|
||||
// key: key,
|
||||
// value: value,
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
// Encode tags (they musts be in lexical order)
|
||||
slices.SortFunc(
|
||||
s.extended_tag_list,
|
||||
func(a key_value_pair, b key_value_pair) int {
|
||||
if a.key < b.key {
|
||||
return -1
|
||||
}
|
||||
if a.key > b.key {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
},
|
||||
)
|
||||
for i := range s.extended_tag_list {
|
||||
s.encoder.AddTag(
|
||||
s.extended_tag_list[i].key,
|
||||
s.extended_tag_list[i].value,
|
||||
// Encode tags (they musts be in lexical order)
|
||||
slices.SortFunc(
|
||||
s.extended_tag_list,
|
||||
func(a key_value_pair, b key_value_pair) int {
|
||||
if a.key < b.key {
|
||||
return -1
|
||||
}
|
||||
if a.key > b.key {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
},
|
||||
)
|
||||
for i := range s.extended_tag_list {
|
||||
s.encoder.AddTag(
|
||||
s.extended_tag_list[i].key,
|
||||
s.extended_tag_list[i].value,
|
||||
)
|
||||
}
|
||||
|
||||
// Encode fields
|
||||
for key, value := range m.Fields() {
|
||||
s.encoder.AddField(key, influx.MustNewValue(value))
|
||||
}
|
||||
|
||||
// Encode time stamp
|
||||
s.encoder.EndLine(m.Time())
|
||||
|
||||
// Check for encoder errors
|
||||
if err := s.encoder.Err(); err != nil {
|
||||
// Unlock encoder usage
|
||||
s.encoderLock.Unlock()
|
||||
|
||||
return fmt.Errorf("encoding failed: %v", err)
|
||||
}
|
||||
s.numRecordsInEncoder++
|
||||
}
|
||||
|
||||
// Encode fields
|
||||
for key, value := range m.Fields() {
|
||||
s.encoder.AddField(key, influx.MustNewValue(value))
|
||||
}
|
||||
|
||||
// Encode time stamp
|
||||
s.encoder.EndLine(m.Time())
|
||||
|
||||
// Check for encoder errors
|
||||
if err := s.encoder.Err(); err != nil {
|
||||
// Unlock encoder usage
|
||||
s.encoderLock.Unlock()
|
||||
|
||||
return fmt.Errorf("Encoding failed: %v", err)
|
||||
}
|
||||
s.numRecordsInEncoder++
|
||||
|
||||
if s.config.flushDelay == 0 {
|
||||
// Unlock encoder usage
|
||||
s.encoderLock.Unlock()
|
||||
@@ -443,11 +447,20 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) {
|
||||
if len(s.config.Password) == 0 {
|
||||
return s, errors.New("missing password configuration required by InfluxSink")
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
s.meta_as_tags = make(map[string]bool)
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = p.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.meta_as_tags[k] = true
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
// Configure flush delay duration
|
||||
|
@@ -72,8 +72,9 @@ import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
"github.com/NVIDIA/go-nvml/pkg/dl"
|
||||
)
|
||||
|
||||
@@ -110,99 +111,102 @@ type LibgangliaSink struct {
|
||||
cstrCache map[string]*C.char
|
||||
}
|
||||
|
||||
func (s *LibgangliaSink) Write(point lp.CCMessage) error {
|
||||
func (s *LibgangliaSink) Write(msg lp.CCMessage) error {
|
||||
var err error = nil
|
||||
var c_name *C.char
|
||||
var c_value *C.char
|
||||
var c_type *C.char
|
||||
var c_unit *C.char
|
||||
|
||||
// helper function for looking up C strings in the cache
|
||||
lookup := func(key string) *C.char {
|
||||
if _, exist := s.cstrCache[key]; !exist {
|
||||
s.cstrCache[key] = C.CString(key)
|
||||
point, err := s.mp.ProcessMessage(msg)
|
||||
if err == nil && point != nil {
|
||||
// helper function for looking up C strings in the cache
|
||||
lookup := func(key string) *C.char {
|
||||
if _, exist := s.cstrCache[key]; !exist {
|
||||
s.cstrCache[key] = C.CString(key)
|
||||
}
|
||||
return s.cstrCache[key]
|
||||
}
|
||||
return s.cstrCache[key]
|
||||
}
|
||||
|
||||
conf := GetCommonGangliaConfig(point)
|
||||
if len(conf.Type) == 0 {
|
||||
conf = GetGangliaConfig(point)
|
||||
}
|
||||
if len(conf.Type) == 0 {
|
||||
return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name)
|
||||
}
|
||||
conf := GetCommonGangliaConfig(point)
|
||||
if len(conf.Type) == 0 {
|
||||
conf = GetGangliaConfig(point)
|
||||
}
|
||||
if len(conf.Type) == 0 {
|
||||
return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name)
|
||||
}
|
||||
|
||||
if s.config.AddTypeToName {
|
||||
conf.Name = GangliaMetricName(point)
|
||||
}
|
||||
if s.config.AddTypeToName {
|
||||
conf.Name = GangliaMetricName(point)
|
||||
}
|
||||
|
||||
c_value = C.CString(conf.Value)
|
||||
c_type = lookup(conf.Type)
|
||||
c_name = lookup(conf.Name)
|
||||
c_value = C.CString(conf.Value)
|
||||
c_type = lookup(conf.Type)
|
||||
c_name = lookup(conf.Name)
|
||||
|
||||
// Add unit
|
||||
unit := ""
|
||||
if s.config.AddUnits {
|
||||
unit = conf.Unit
|
||||
}
|
||||
c_unit = lookup(unit)
|
||||
// Add unit
|
||||
unit := ""
|
||||
if s.config.AddUnits {
|
||||
unit = conf.Unit
|
||||
}
|
||||
c_unit = lookup(unit)
|
||||
|
||||
// Determine the slope of the metric. Ganglia's own collector mostly use
|
||||
// 'both' but the mem and swap total uses 'zero'.
|
||||
slope_type := C.GANGLIA_SLOPE_BOTH
|
||||
switch conf.Slope {
|
||||
case "zero":
|
||||
slope_type = C.GANGLIA_SLOPE_ZERO
|
||||
case "both":
|
||||
slope_type = C.GANGLIA_SLOPE_BOTH
|
||||
}
|
||||
// Determine the slope of the metric. Ganglia's own collector mostly use
|
||||
// 'both' but the mem and swap total uses 'zero'.
|
||||
slope_type := C.GANGLIA_SLOPE_BOTH
|
||||
switch conf.Slope {
|
||||
case "zero":
|
||||
slope_type = C.GANGLIA_SLOPE_ZERO
|
||||
case "both":
|
||||
slope_type = C.GANGLIA_SLOPE_BOTH
|
||||
}
|
||||
|
||||
// Create a new Ganglia metric
|
||||
gmetric := C.Ganglia_metric_create(s.global_context)
|
||||
// Set name, value, type and unit in the Ganglia metric
|
||||
// The default slope_type is both directions, so up and down. Some metrics want 'zero' slope, probably constant.
|
||||
// The 'tmax' value is by default 300.
|
||||
rval := C.int(0)
|
||||
rval = C.Ganglia_metric_set(gmetric, c_name, c_value, c_type, c_unit, C.uint(slope_type), C.uint(conf.Tmax), 0)
|
||||
switch rval {
|
||||
case 1:
|
||||
// Create a new Ganglia metric
|
||||
gmetric := C.Ganglia_metric_create(s.global_context)
|
||||
// Set name, value, type and unit in the Ganglia metric
|
||||
// The default slope_type is both directions, so up and down. Some metrics want 'zero' slope, probably constant.
|
||||
// The 'tmax' value is by default 300.
|
||||
rval := C.int(0)
|
||||
rval = C.Ganglia_metric_set(gmetric, c_name, c_value, c_type, c_unit, C.uint(slope_type), C.uint(conf.Tmax), 0)
|
||||
switch rval {
|
||||
case 1:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return errors.New("invalid parameters")
|
||||
case 2:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return errors.New("one of your parameters has an invalid character '\"'")
|
||||
case 3:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return fmt.Errorf("the type parameter \"%s\" is not a valid type", conf.Type)
|
||||
case 4:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return fmt.Errorf("the value parameter \"%s\" does not represent a number", conf.Value)
|
||||
default:
|
||||
}
|
||||
|
||||
// Set the cluster name, otherwise it takes it from the configuration file
|
||||
if len(s.config.ClusterName) > 0 {
|
||||
C.Ganglia_metadata_add(gmetric, lookup("CLUSTER"), lookup(s.config.ClusterName))
|
||||
}
|
||||
// Set the group metadata in the Ganglia metric if configured
|
||||
if s.config.AddGangliaGroup {
|
||||
c_group := lookup(conf.Group)
|
||||
C.Ganglia_metadata_add(gmetric, lookup("GROUP"), c_group)
|
||||
}
|
||||
|
||||
// Now we send the metric
|
||||
// gmetric does provide some more options like description and other options
|
||||
// but they are not provided by the collectors
|
||||
rval = C.Ganglia_metric_send(gmetric, s.send_channels)
|
||||
if rval != 0 {
|
||||
err = fmt.Errorf("there was an error sending metric %s to %d of the send channels ", point.Name(), rval)
|
||||
// fall throuph to use Ganglia_metric_destroy from common cleanup
|
||||
}
|
||||
// Cleanup Ganglia metric
|
||||
C.Ganglia_metric_destroy(gmetric)
|
||||
// Free the value C string, the only one not stored in the cache
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return errors.New("invalid parameters")
|
||||
case 2:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return errors.New("one of your parameters has an invalid character '\"'")
|
||||
case 3:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return fmt.Errorf("the type parameter \"%s\" is not a valid type", conf.Type)
|
||||
case 4:
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return fmt.Errorf("the value parameter \"%s\" does not represent a number", conf.Value)
|
||||
default:
|
||||
}
|
||||
|
||||
// Set the cluster name, otherwise it takes it from the configuration file
|
||||
if len(s.config.ClusterName) > 0 {
|
||||
C.Ganglia_metadata_add(gmetric, lookup("CLUSTER"), lookup(s.config.ClusterName))
|
||||
}
|
||||
// Set the group metadata in the Ganglia metric if configured
|
||||
if s.config.AddGangliaGroup {
|
||||
c_group := lookup(conf.Group)
|
||||
C.Ganglia_metadata_add(gmetric, lookup("GROUP"), c_group)
|
||||
}
|
||||
|
||||
// Now we send the metric
|
||||
// gmetric does provide some more options like description and other options
|
||||
// but they are not provided by the collectors
|
||||
rval = C.Ganglia_metric_send(gmetric, s.send_channels)
|
||||
if rval != 0 {
|
||||
err = fmt.Errorf("there was an error sending metric %s to %d of the send channels ", point.Name(), rval)
|
||||
// fall throuph to use Ganglia_metric_destroy from common cleanup
|
||||
}
|
||||
// Cleanup Ganglia metric
|
||||
C.Ganglia_metric_destroy(gmetric)
|
||||
// Free the value C string, the only one not stored in the cache
|
||||
C.free(unsafe.Pointer(c_value))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -241,6 +245,20 @@ func NewLibgangliaSink(name string, config json.RawMessage) (Sink, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = s.mp.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
lib := dl.New(s.config.GangliaLib, GANGLIA_LIB_DL_FLAGS)
|
||||
if lib == nil {
|
||||
return nil, fmt.Errorf("error instantiating DynamicLibrary for %s", s.config.GangliaLib)
|
||||
|
@@ -1,24 +1,29 @@
|
||||
package sinks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
)
|
||||
|
||||
type defaultSinkConfig struct {
|
||||
MetaAsTags []string `json:"meta_as_tags,omitempty"`
|
||||
Type string `json:"type"`
|
||||
MetaAsTags []string `json:"meta_as_tags,omitempty"`
|
||||
MessageProcessor json.RawMessage `json:"process_messages,omitempty"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type sink struct {
|
||||
meta_as_tags map[string]bool // Use meta data tags as tags
|
||||
name string // Name of the sink
|
||||
meta_as_tags map[string]bool // Use meta data tags as tags
|
||||
mp mp.MessageProcessor // message processor for the sink
|
||||
name string // Name of the sink
|
||||
}
|
||||
|
||||
type Sink interface {
|
||||
Write(point lp.CCMessage) error // Write metric to the sink
|
||||
Flush() error // Flush buffered metrics
|
||||
Close() // Close / finish metric sink
|
||||
Name() string // Name of the metric sink
|
||||
Flush() error // Flush buffered metrics
|
||||
Close() // Close / finish metric sink
|
||||
Name() string // Name of the metric sink
|
||||
}
|
||||
|
||||
// Name returns the name of the metric sink
|
||||
|
@@ -8,8 +8,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
influx "github.com/influxdata/line-protocol"
|
||||
nats "github.com/nats-io/nats.go"
|
||||
)
|
||||
@@ -60,12 +61,15 @@ func (s *NatsSink) connect() error {
|
||||
}
|
||||
|
||||
func (s *NatsSink) Write(m lp.CCMessage) error {
|
||||
s.lock.Lock()
|
||||
_, err := s.encoder.Encode(m.ToPoint(s.meta_as_tags))
|
||||
s.lock.Unlock()
|
||||
if err != nil {
|
||||
cclog.ComponentError(s.name, "Write:", err.Error())
|
||||
return err
|
||||
msg, err := s.mp.ProcessMessage(m)
|
||||
if err == nil && msg != nil {
|
||||
s.lock.Lock()
|
||||
_, err := s.encoder.Encode(msg.ToPoint(nil))
|
||||
s.lock.Unlock()
|
||||
if err != nil {
|
||||
cclog.ComponentError(s.name, "Write:", err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if s.flushDelay == 0 {
|
||||
@@ -120,11 +124,25 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) {
|
||||
len(s.config.Subject) == 0 {
|
||||
return nil, errors.New("not all configuration variables set required by NatsSink")
|
||||
}
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
s.meta_as_tags = make(map[string]bool)
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.meta_as_tags[k] = true
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = s.mp.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
// s.meta_as_tags = make(map[string]bool)
|
||||
// for _, k := range s.config.MetaAsTags {
|
||||
// s.meta_as_tags[k] = true
|
||||
// }
|
||||
// Setup Influx line protocol
|
||||
s.buffer = &bytes.Buffer{}
|
||||
s.buffer.Grow(1025)
|
||||
|
@@ -10,8 +10,9 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
@@ -153,7 +154,11 @@ func (s *PrometheusSink) updateMetric(metric lp.CCMessage) error {
|
||||
}
|
||||
|
||||
func (s *PrometheusSink) Write(m lp.CCMessage) error {
|
||||
return s.updateMetric(m)
|
||||
msg, err := s.mp.ProcessMessage(m)
|
||||
if err == nil && msg != nil {
|
||||
err = s.updateMetric(m)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PrometheusSink) Flush() error {
|
||||
@@ -182,6 +187,20 @@ func NewPrometheusSink(name string, config json.RawMessage) (Sink, error) {
|
||||
cclog.ComponentError(s.name, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = p.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
s.labelMetrics = make(map[string]*prometheus.GaugeVec)
|
||||
s.nodeMetrics = make(map[string]prometheus.Gauge)
|
||||
s.promWg.Add(1)
|
||||
|
@@ -6,8 +6,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
)
|
||||
|
||||
type SampleSinkConfig struct {
|
||||
@@ -30,7 +31,12 @@ type SampleSink struct {
|
||||
// Code to submit a single CCMetric to the sink
|
||||
func (s *SampleSink) Write(point lp.CCMessage) error {
|
||||
// based on s.meta_as_tags use meta infos as tags
|
||||
log.Print(point)
|
||||
// moreover, submit the point to the message processor
|
||||
// to apply drop/modify rules
|
||||
msg, err := s.mp.ProcessMessage(point)
|
||||
if err == nil && msg != nil {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -66,10 +72,24 @@ func NewSampleSink(name string, config json.RawMessage) (Sink, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
s.meta_as_tags = make(map[string]bool)
|
||||
// Initialize and configure the message processor
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
|
||||
// Add message processor configuration
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = p.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
// Add rules to move meta information to tag space
|
||||
// Replacing the legacy 'meta_as_tags' configuration
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.meta_as_tags[k] = true
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
// Check if all required fields in the config are set
|
||||
|
@@ -8,8 +8,9 @@ import (
|
||||
"strings"
|
||||
|
||||
// "time"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
||||
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
||||
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
||||
)
|
||||
|
||||
type StdoutSink struct {
|
||||
@@ -22,10 +23,13 @@ type StdoutSink struct {
|
||||
}
|
||||
|
||||
func (s *StdoutSink) Write(m lp.CCMessage) error {
|
||||
fmt.Fprint(
|
||||
s.output,
|
||||
m.ToLineProtocol(s.meta_as_tags),
|
||||
)
|
||||
msg, err := s.mp.ProcessMessage(m)
|
||||
if err == nil && msg != nil {
|
||||
fmt.Fprint(
|
||||
s.output,
|
||||
msg.ToLineProtocol(s.meta_as_tags),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,6 +45,7 @@ func (s *StdoutSink) Close() {
|
||||
}
|
||||
|
||||
func NewStdoutSink(name string, config json.RawMessage) (Sink, error) {
|
||||
|
||||
s := new(StdoutSink)
|
||||
s.name = fmt.Sprintf("StdoutSink(%s)", name)
|
||||
if len(config) > 0 {
|
||||
@@ -51,6 +56,11 @@ func NewStdoutSink(name string, config json.RawMessage) (Sink, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
p, err := mp.NewMessageProcessor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
||||
}
|
||||
s.mp = p
|
||||
|
||||
s.output = os.Stdout
|
||||
if len(s.config.Output) > 0 {
|
||||
@@ -67,10 +77,21 @@ func NewStdoutSink(name string, config json.RawMessage) (Sink, error) {
|
||||
s.output = f
|
||||
}
|
||||
}
|
||||
|
||||
// Add message processor configuration
|
||||
if len(s.config.MessageProcessor) > 0 {
|
||||
err = s.mp.FromConfigJSON(s.config.MessageProcessor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
||||
}
|
||||
}
|
||||
// Create lookup map to use meta infos as tags in the output metric
|
||||
s.meta_as_tags = make(map[string]bool)
|
||||
// s.meta_as_tags = make(map[string]bool)
|
||||
// for _, k := range s.config.MetaAsTags {
|
||||
// s.meta_as_tags[k] = true
|
||||
// }
|
||||
for _, k := range s.config.MetaAsTags {
|
||||
s.meta_as_tags[k] = true
|
||||
s.mp.AddMoveMetaToTags("true", k, k)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
@@ -10,7 +10,10 @@ The `stdout` sink is the most simple sink provided by cc-metric-collector. It wr
|
||||
"<name>": {
|
||||
"type": "stdout",
|
||||
"meta_as_tags" : [],
|
||||
"output_file" : "mylogfile.log"
|
||||
"output_file" : "mylogfile.log",
|
||||
"process_messages" : {
|
||||
"see" : "docs of message processor for valid fields"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -18,5 +21,6 @@ The `stdout` sink is the most simple sink provided by cc-metric-collector. It wr
|
||||
- `type`: makes the sink an `stdout` sink
|
||||
- `meta_as_tags`: print meta information as tags in the output (optional)
|
||||
- `output_file`: Write all data to the selected file (optional). There are two 'special' files: `stdout` and `stderr`. If this option is not provided, the default value is `stdout`
|
||||
- `process_messages`: Process messages with given rules before progressing or dropping
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user