2021-03-26 17:03:46 +01:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
2022-02-04 18:12:24 +01:00
|
|
|
"encoding/json"
|
2021-03-26 17:08:00 +01:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2022-02-04 18:12:24 +01:00
|
|
|
"os"
|
2021-03-26 17:08:00 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
type StdoutSinkConfig struct {
|
|
|
|
defaultSinkConfig
|
|
|
|
Output string `json:"output_file,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-03-26 17:03:46 +01:00
|
|
|
type StdoutSink struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
sink
|
2022-02-04 18:12:24 +01:00
|
|
|
output *os.File
|
|
|
|
config StdoutSinkConfig
|
2021-03-26 17:03:46 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
func (s *StdoutSink) Init(config json.RawMessage) error {
|
2022-01-25 15:37:43 +01:00
|
|
|
s.name = "StdoutSink"
|
2022-02-04 18:12:24 +01:00
|
|
|
if len(config) > 0 {
|
|
|
|
err := json.Unmarshal(config, &s.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.output = os.Stdout
|
|
|
|
if len(s.config.Output) > 0 {
|
|
|
|
if strings.ToLower(s.config.Output) == "stdout" {
|
|
|
|
s.output = os.Stdout
|
|
|
|
} else if strings.ToLower(s.config.Output) == "stderr" {
|
|
|
|
s.output = os.Stderr
|
|
|
|
} else {
|
|
|
|
f, err := os.OpenFile(s.config.Output, os.O_CREATE|os.O_WRONLY, os.FileMode(0600))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.output = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.meta_as_tags = s.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))
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 22:52:39 +01:00
|
|
|
for key, value := range point.Fields() {
|
|
|
|
switch value.(type) {
|
2021-05-18 15:16:10 +02:00
|
|
|
case float64:
|
2022-02-07 22:52:39 +01:00
|
|
|
if !math.IsNaN(value.(float64)) {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(float64)))
|
2021-10-04 15:23:43 +02:00
|
|
|
} else {
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
2021-05-18 15:16:10 +02:00
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
case float32:
|
2022-02-07 22:52:39 +01:00
|
|
|
if !math.IsNaN(float64(value.(float32))) {
|
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(float32)))
|
2021-10-04 15:23:43 +02:00
|
|
|
} else {
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
|
|
|
case int:
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, value.(int)))
|
2021-10-04 15:23:43 +02:00
|
|
|
case int64:
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, value.(int64)))
|
2021-05-12 17:46:16 +02:00
|
|
|
case string:
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, value.(string)))
|
2021-10-04 15:23:43 +02:00
|
|
|
default:
|
2022-02-07 22:52:39 +01:00
|
|
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, 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 {
|
2022-02-04 18:12:24 +01:00
|
|
|
s.output.Sync()
|
2021-10-12 13:43:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-04 18:12:24 +01:00
|
|
|
func (s *StdoutSink) Close() {
|
|
|
|
if s.output != os.Stdout && s.output != os.Stderr {
|
|
|
|
s.output.Close()
|
|
|
|
}
|
|
|
|
}
|