diff --git a/sinks/stdoutSink.go b/sinks/stdoutSink.go index c085c13..5d0761a 100644 --- a/sinks/stdoutSink.go +++ b/sinks/stdoutSink.go @@ -3,7 +3,6 @@ package sinks import ( "encoding/json" "fmt" - "math" "os" "strings" @@ -11,15 +10,13 @@ import ( lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) -type StdoutSinkConfig struct { - defaultSinkConfig - Output string `json:"output_file,omitempty"` -} - type StdoutSink struct { - sink + sink // meta_as_tags, name output *os.File - config StdoutSinkConfig + config struct { + defaultSinkConfig + Output string `json:"output_file,omitempty"` + } } func (s *StdoutSink) Init(config json.RawMessage) error { @@ -30,13 +27,15 @@ func (s *StdoutSink) Init(config json.RawMessage) error { return err } } + s.output = os.Stdout if len(s.config.Output) > 0 { - if strings.ToLower(s.config.Output) == "stdout" { + switch strings.ToLower(s.config.Output) { + case "stdout": s.output = os.Stdout - } else if strings.ToLower(s.config.Output) == "stderr" { + case "stderr": s.output = os.Stderr - } else { + default: f, err := os.OpenFile(s.config.Output, os.O_CREATE|os.O_WRONLY, os.FileMode(0600)) if err != nil { return err @@ -48,46 +47,11 @@ func (s *StdoutSink) Init(config json.RawMessage) error { return nil } -func (s *StdoutSink) Write(point lp.CCMetric) error { - var tagsstr []string - var fieldstr []string - for key, value := range point.Tags() { - tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value)) - } - if s.meta_as_tags { - for key, value := range point.Meta() { - tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value)) - } - } - for key, v := range point.Fields() { - switch value := v.(type) { - case float64: - if !math.IsNaN(value) { - fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, v)) - } else { - fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key)) - } - case float32: - if !math.IsNaN(float64(value)) { - fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, v)) - } else { - fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key)) - } - case int: - fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v)) - case int64: - fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v)) - case string: - fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, v)) - default: - fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value)) - } - } - if len(tagsstr) > 0 { - fmt.Printf("%s,%s %s %d\n", point.Name(), strings.Join(tagsstr, ","), strings.Join(fieldstr, ","), point.Time().Unix()) - } else { - fmt.Printf("%s %s %d\n", point.Name(), strings.Join(fieldstr, ","), point.Time().Unix()) - } +func (s *StdoutSink) Write(m lp.CCMetric) error { + fmt.Fprint( + s.output, + m.ToLineProtocol(s.meta_as_tags), + ) return nil }