mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 04:27:25 +01:00
Refactoring: Remove all *List() functions from CCMetric
This commit is contained in:
parent
d98b678399
commit
6d55c376bd
@ -2,7 +2,6 @@ package ccmetric
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
@ -25,29 +24,28 @@ type ccMetric struct {
|
||||
|
||||
// ccmetric access functions
|
||||
type CCMetric interface {
|
||||
lp.Metric // Time(), Name(), TagList(), FieldList()
|
||||
ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric
|
||||
ToPoint(metaAsTags bool) *write.Point // Generate influxDB point for data type ccMetric
|
||||
|
||||
Name() string // Get metric name
|
||||
SetName(name string) // Set metric name
|
||||
|
||||
Time() time.Time // Get timestamp
|
||||
SetTime(t time.Time) // Set timestamp
|
||||
|
||||
Tags() map[string]string // Map of tags
|
||||
TagList() []*lp.Tag // Ordered list of tags
|
||||
AddTag(key, value string) // Add a tag
|
||||
GetTag(key string) (value string, ok bool) // Get a tag by its key
|
||||
HasTag(key string) (ok bool) // Check a tag
|
||||
RemoveTag(key string) // Remove a tag by its key
|
||||
|
||||
Meta() map[string]string // Map of meta data tags
|
||||
MetaList() []*lp.Tag // Ordered list of meta data
|
||||
AddMeta(key, value string) // Add a meta data tag
|
||||
GetMeta(key string) (value string, ok bool) // Get a meta data tab addressed by its key
|
||||
HasMeta(key string) (ok bool) // Check a meta data tag
|
||||
RemoveMeta(key string) // Remove a meta data tag by its key
|
||||
|
||||
Fields() map[string]interface{} // Map of fields
|
||||
FieldList() []*lp.Field // Ordered list of fields
|
||||
AddField(key string, value interface{}) // Add a field
|
||||
GetField(key string) (value interface{}, ok bool) // Get a field addressed by its key
|
||||
HasField(key string) (ok bool) // Check if a field key is present
|
||||
@ -59,17 +57,6 @@ func (m *ccMetric) Meta() map[string]string {
|
||||
return m.meta
|
||||
}
|
||||
|
||||
// MetaList returns the the list of meta data tags as sorted list of key value tags
|
||||
func (m *ccMetric) MetaList() []*lp.Tag {
|
||||
|
||||
ml := make([]*lp.Tag, 0, len(m.meta))
|
||||
for key, value := range m.meta {
|
||||
ml = append(ml, &lp.Tag{Key: key, Value: value})
|
||||
}
|
||||
sort.Slice(ml, func(i, j int) bool { return ml[i].Key < ml[j].Key })
|
||||
return ml
|
||||
}
|
||||
|
||||
// String implements the stringer interface for data type ccMetric
|
||||
func (m *ccMetric) String() string {
|
||||
return fmt.Sprintf("Name: %s, Tags: %+v, Meta: %+v, fields: %+v, Timestamp: %d", m.name, m.tags, m.meta, m.fields, m.tm.UnixNano())
|
||||
@ -116,30 +103,11 @@ func (m *ccMetric) Tags() map[string]string {
|
||||
return m.tags
|
||||
}
|
||||
|
||||
// TagList returns the the list of tags as sorted list of key value tags
|
||||
func (m *ccMetric) TagList() []*lp.Tag {
|
||||
tl := make([]*lp.Tag, 0, len(m.tags))
|
||||
for key, value := range m.tags {
|
||||
tl = append(tl, &lp.Tag{Key: key, Value: value})
|
||||
}
|
||||
sort.Slice(tl, func(i, j int) bool { return tl[i].Key < tl[j].Key })
|
||||
return tl
|
||||
}
|
||||
|
||||
// Fields returns the list of fields as key-value-mapping
|
||||
func (m *ccMetric) Fields() map[string]interface{} {
|
||||
return m.fields
|
||||
}
|
||||
|
||||
// FieldList returns the list of fields
|
||||
func (m *ccMetric) FieldList() []*lp.Field {
|
||||
fieldList := make([]*lp.Field, 0, len(m.fields))
|
||||
for key, value := range m.fields {
|
||||
fieldList = append(fieldList, &lp.Field{Key: key, Value: value})
|
||||
}
|
||||
return fieldList
|
||||
}
|
||||
|
||||
// Time returns timestamp
|
||||
func (m *ccMetric) Time() time.Time {
|
||||
return m.tm
|
||||
|
@ -57,8 +57,8 @@ func (s *HttpSink) Init(config json.RawMessage) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HttpSink) Write(point lp.CCMetric) error {
|
||||
_, err := s.encoder.Encode(point)
|
||||
func (s *HttpSink) Write(m lp.CCMetric) error {
|
||||
_, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags))
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -77,9 +77,9 @@ func (s *NatsSink) Init(config json.RawMessage) error {
|
||||
return s.connect()
|
||||
}
|
||||
|
||||
func (s *NatsSink) Write(point lp.CCMetric) error {
|
||||
func (s *NatsSink) Write(m lp.CCMetric) error {
|
||||
if s.client != nil {
|
||||
_, err := s.encoder.Encode(point)
|
||||
_, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags))
|
||||
if err != nil {
|
||||
cclog.ComponentError(s.name, "Write:", err.Error())
|
||||
return err
|
||||
|
@ -51,34 +51,34 @@ func (s *StdoutSink) Init(config json.RawMessage) error {
|
||||
func (s *StdoutSink) Write(point lp.CCMetric) error {
|
||||
var tagsstr []string
|
||||
var fieldstr []string
|
||||
for _, t := range point.TagList() {
|
||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", t.Key, t.Value))
|
||||
for key, value := range point.Tags() {
|
||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
if s.meta_as_tags {
|
||||
for _, m := range point.MetaList() {
|
||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", m.Key, m.Value))
|
||||
for key, value := range point.Meta() {
|
||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
}
|
||||
for key, value := range point.Fields() {
|
||||
switch value.(type) {
|
||||
for key, v := range point.Fields() {
|
||||
switch value := v.(type) {
|
||||
case float64:
|
||||
if !math.IsNaN(value.(float64)) {
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(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.(float32))) {
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(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, value.(int)))
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v))
|
||||
case int64:
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, value.(int64)))
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v))
|
||||
case string:
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, value.(string)))
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, v))
|
||||
default:
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user