mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-25 15:09:05 +01:00
Refactoring: Use ccmetric function ToLineProtocol() in stdout sink
This commit is contained in:
parent
e1cf682989
commit
377f85111a
@ -3,7 +3,6 @@ package sinks
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -11,15 +10,13 @@ import (
|
|||||||
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StdoutSinkConfig struct {
|
|
||||||
defaultSinkConfig
|
|
||||||
Output string `json:"output_file,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StdoutSink struct {
|
type StdoutSink struct {
|
||||||
sink
|
sink // meta_as_tags, name
|
||||||
output *os.File
|
output *os.File
|
||||||
config StdoutSinkConfig
|
config struct {
|
||||||
|
defaultSinkConfig
|
||||||
|
Output string `json:"output_file,omitempty"`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StdoutSink) Init(config json.RawMessage) error {
|
func (s *StdoutSink) Init(config json.RawMessage) error {
|
||||||
@ -30,13 +27,15 @@ func (s *StdoutSink) Init(config json.RawMessage) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.output = os.Stdout
|
s.output = os.Stdout
|
||||||
if len(s.config.Output) > 0 {
|
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
|
s.output = os.Stdout
|
||||||
} else if strings.ToLower(s.config.Output) == "stderr" {
|
case "stderr":
|
||||||
s.output = os.Stderr
|
s.output = os.Stderr
|
||||||
} else {
|
default:
|
||||||
f, err := os.OpenFile(s.config.Output, os.O_CREATE|os.O_WRONLY, os.FileMode(0600))
|
f, err := os.OpenFile(s.config.Output, os.O_CREATE|os.O_WRONLY, os.FileMode(0600))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -48,46 +47,11 @@ func (s *StdoutSink) Init(config json.RawMessage) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StdoutSink) Write(point lp.CCMetric) error {
|
func (s *StdoutSink) Write(m lp.CCMetric) error {
|
||||||
var tagsstr []string
|
fmt.Fprint(
|
||||||
var fieldstr []string
|
s.output,
|
||||||
for key, value := range point.Tags() {
|
m.ToLineProtocol(s.meta_as_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())
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user