2021-03-26 17:03:46 +01:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
2021-03-26 17:08:00 +01:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
2021-10-12 13:43:58 +02:00
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
// "time"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-03-26 17:03:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type StdoutSink struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
sink
|
2021-03-26 17:03:46 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (s *StdoutSink) Init(config sinkConfig) error {
|
|
|
|
s.name = "StdoutSink"
|
|
|
|
s.meta_as_tags = config.MetaAsTags
|
2021-03-26 17:03:46 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (s *StdoutSink) Write(point lp.CCMetric) error {
|
2021-03-26 17:08:00 +01:00
|
|
|
var tagsstr []string
|
|
|
|
var fieldstr []string
|
2021-10-04 15:23:43 +02:00
|
|
|
for _, t := range point.TagList() {
|
|
|
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", t.Key, t.Value))
|
2021-03-26 17:08:00 +01:00
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
if s.meta_as_tags {
|
|
|
|
for _, m := range point.MetaList() {
|
|
|
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", m.Key, m.Value))
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
for _, f := range point.FieldList() {
|
|
|
|
switch f.Value.(type) {
|
2021-05-18 15:16:10 +02:00
|
|
|
case float64:
|
2021-10-04 15:23:43 +02:00
|
|
|
if !math.IsNaN(f.Value.(float64)) {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", f.Key, f.Value.(float64)))
|
|
|
|
} else {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", f.Key))
|
2021-05-18 15:16:10 +02:00
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
case float32:
|
|
|
|
if !math.IsNaN(float64(f.Value.(float32))) {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", f.Key, f.Value.(float32)))
|
|
|
|
} else {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", f.Key))
|
|
|
|
}
|
|
|
|
case int:
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", f.Key, f.Value.(int)))
|
|
|
|
case int64:
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", f.Key, f.Value.(int64)))
|
2021-05-12 17:46:16 +02:00
|
|
|
case string:
|
2021-10-04 15:23:43 +02:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", f.Key, f.Value.(string)))
|
|
|
|
default:
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", f.Key, f.Value))
|
2021-03-26 17:08:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tagsstr) > 0 {
|
2021-10-04 15:23:43 +02:00
|
|
|
fmt.Printf("%s,%s %s %d\n", point.Name(), strings.Join(tagsstr, ","), strings.Join(fieldstr, ","), point.Time().Unix())
|
2021-03-26 17:08:00 +01:00
|
|
|
} else {
|
2021-10-04 15:23:43 +02:00
|
|
|
fmt.Printf("%s %s %d\n", point.Name(), strings.Join(fieldstr, ","), point.Time().Unix())
|
2021-03-26 17:08:00 +01:00
|
|
|
}
|
|
|
|
return nil
|
2021-03-26 17:03:46 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:43:58 +02:00
|
|
|
func (s *StdoutSink) Flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 14:35:52 +01:00
|
|
|
func (s *StdoutSink) Close() {}
|