mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-25 15:09:05 +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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||||
@ -25,29 +24,28 @@ type ccMetric struct {
|
|||||||
|
|
||||||
// ccmetric access functions
|
// ccmetric access functions
|
||||||
type CCMetric interface {
|
type CCMetric interface {
|
||||||
lp.Metric // Time(), Name(), TagList(), FieldList()
|
|
||||||
ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric
|
ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric
|
||||||
ToPoint(metaAsTags bool) *write.Point // Generate influxDB point 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
|
SetName(name string) // Set metric name
|
||||||
|
|
||||||
|
Time() time.Time // Get timestamp
|
||||||
SetTime(t time.Time) // Set timestamp
|
SetTime(t time.Time) // Set timestamp
|
||||||
|
|
||||||
Tags() map[string]string // Map of tags
|
Tags() map[string]string // Map of tags
|
||||||
TagList() []*lp.Tag // Ordered list of tags
|
|
||||||
AddTag(key, value string) // Add a tag
|
AddTag(key, value string) // Add a tag
|
||||||
GetTag(key string) (value string, ok bool) // Get a tag by its key
|
GetTag(key string) (value string, ok bool) // Get a tag by its key
|
||||||
HasTag(key string) (ok bool) // Check a tag
|
HasTag(key string) (ok bool) // Check a tag
|
||||||
RemoveTag(key string) // Remove a tag by its key
|
RemoveTag(key string) // Remove a tag by its key
|
||||||
|
|
||||||
Meta() map[string]string // Map of meta data tags
|
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
|
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
|
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
|
HasMeta(key string) (ok bool) // Check a meta data tag
|
||||||
RemoveMeta(key string) // Remove a meta data tag by its key
|
RemoveMeta(key string) // Remove a meta data tag by its key
|
||||||
|
|
||||||
Fields() map[string]interface{} // Map of fields
|
Fields() map[string]interface{} // Map of fields
|
||||||
FieldList() []*lp.Field // Ordered list of fields
|
|
||||||
AddField(key string, value interface{}) // Add a field
|
AddField(key string, value interface{}) // Add a field
|
||||||
GetField(key string) (value interface{}, ok bool) // Get a field addressed by its key
|
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
|
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
|
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
|
// String implements the stringer interface for data type ccMetric
|
||||||
func (m *ccMetric) String() string {
|
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())
|
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
|
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
|
// Fields returns the list of fields as key-value-mapping
|
||||||
func (m *ccMetric) Fields() map[string]interface{} {
|
func (m *ccMetric) Fields() map[string]interface{} {
|
||||||
return m.fields
|
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
|
// Time returns timestamp
|
||||||
func (m *ccMetric) Time() time.Time {
|
func (m *ccMetric) Time() time.Time {
|
||||||
return m.tm
|
return m.tm
|
||||||
|
@ -57,8 +57,8 @@ func (s *HttpSink) Init(config json.RawMessage) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HttpSink) Write(point lp.CCMetric) error {
|
func (s *HttpSink) Write(m lp.CCMetric) error {
|
||||||
_, err := s.encoder.Encode(point)
|
_, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,9 +77,9 @@ func (s *NatsSink) Init(config json.RawMessage) error {
|
|||||||
return s.connect()
|
return s.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *NatsSink) Write(point lp.CCMetric) error {
|
func (s *NatsSink) Write(m lp.CCMetric) error {
|
||||||
if s.client != nil {
|
if s.client != nil {
|
||||||
_, err := s.encoder.Encode(point)
|
_, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cclog.ComponentError(s.name, "Write:", err.Error())
|
cclog.ComponentError(s.name, "Write:", err.Error())
|
||||||
return err
|
return err
|
||||||
|
@ -51,34 +51,34 @@ func (s *StdoutSink) Init(config json.RawMessage) error {
|
|||||||
func (s *StdoutSink) Write(point lp.CCMetric) error {
|
func (s *StdoutSink) Write(point lp.CCMetric) error {
|
||||||
var tagsstr []string
|
var tagsstr []string
|
||||||
var fieldstr []string
|
var fieldstr []string
|
||||||
for _, t := range point.TagList() {
|
for key, value := range point.Tags() {
|
||||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", t.Key, t.Value))
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
||||||
}
|
}
|
||||||
if s.meta_as_tags {
|
if s.meta_as_tags {
|
||||||
for _, m := range point.MetaList() {
|
for key, value := range point.Meta() {
|
||||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", m.Key, m.Value))
|
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", key, value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for key, value := range point.Fields() {
|
for key, v := range point.Fields() {
|
||||||
switch value.(type) {
|
switch value := v.(type) {
|
||||||
case float64:
|
case float64:
|
||||||
if !math.IsNaN(value.(float64)) {
|
if !math.IsNaN(value) {
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(float64)))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, v))
|
||||||
} else {
|
} else {
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
||||||
}
|
}
|
||||||
case float32:
|
case float32:
|
||||||
if !math.IsNaN(float64(value.(float32))) {
|
if !math.IsNaN(float64(value)) {
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value.(float32)))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, v))
|
||||||
} else {
|
} else {
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=0.0", key))
|
||||||
}
|
}
|
||||||
case int:
|
case int:
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, value.(int)))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v))
|
||||||
case int64:
|
case int64:
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, value.(int64)))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%d", key, v))
|
||||||
case string:
|
case string:
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, value.(string)))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", key, v))
|
||||||
default:
|
default:
|
||||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value))
|
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", key, value))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user