cc-metric-collector/sinks/stdoutSink.go

60 lines
1.5 KiB
Go
Raw Normal View History

2021-03-26 17:03:46 +01:00
package sinks
import (
2021-03-26 17:08:00 +01:00
"fmt"
"math"
"strings"
2021-10-04 15:23:43 +02:00
// "time"
lp "github.com/influxdata/line-protocol"
2021-03-26 17:03:46 +01:00
)
type StdoutSink struct {
Sink
}
func (s *StdoutSink) Init(config SinkConfig) error {
2021-03-26 17:03:46 +01:00
return nil
}
2021-10-04 15:23:43 +02:00
func (s *StdoutSink) Write(point lp.MutableMetric) 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
}
2021-10-04 15:23:43 +02:00
for _, f := range point.FieldList() {
switch f.Value.(type) {
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-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)))
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
}
func (s *StdoutSink) Close() {
2021-03-26 17:08:00 +01:00
return
2021-03-26 17:03:46 +01:00
}