2022-01-25 15:37:43 +01:00
|
|
|
package ccmetric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"time"
|
2022-01-30 14:30:06 +01:00
|
|
|
|
|
|
|
lp "github.com/influxdata/line-protocol" // MIT license
|
2022-01-25 15:37:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Most functions are derived from github.com/influxdata/line-protocol/metric.go
|
|
|
|
// The metric type is extended with an extra meta information list re-using the Tag
|
|
|
|
// type.
|
2022-01-31 14:02:00 +01:00
|
|
|
//
|
|
|
|
// See: https://docs.influxdata.com/influxdb/latest/reference/syntax/line-protocol/
|
2022-01-25 15:37:43 +01:00
|
|
|
type ccMetric struct {
|
2022-02-01 14:54:34 +01:00
|
|
|
name string // Measurement name
|
|
|
|
meta map[string]string // map of meta data tags
|
|
|
|
tags map[string]string // map of of tags
|
|
|
|
fields []*lp.Field // unordered list of of fields
|
|
|
|
tm time.Time // timestamp
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// ccmetric access functions
|
2022-01-25 15:37:43 +01:00
|
|
|
type CCMetric interface {
|
2022-02-01 14:54:34 +01:00
|
|
|
lp.Metric // Time(), Name(), TagList(), FieldList()
|
|
|
|
|
2022-02-01 18:27:16 +01:00
|
|
|
SetName(name string)
|
2022-02-01 14:54:34 +01:00
|
|
|
SetTime(t time.Time)
|
|
|
|
|
|
|
|
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) (string, bool) // Get a meta data tab addressed by its key
|
|
|
|
|
|
|
|
Tags() map[string]string // Map of tags
|
|
|
|
AddTag(key, value string) // Add a tag
|
|
|
|
GetTag(key string) (string, bool) // Get a tag by its key
|
|
|
|
RemoveTag(key string) // Remove a tag by its key
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
GetField(key string) (interface{}, bool) // Get a field addressed by its key
|
|
|
|
HasField(key string) bool // Check if a field key is present
|
|
|
|
RemoveField(key string) // Remove a field addressed by its key
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// Meta returns the meta data tags as key-value mapping
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) Meta() map[string]string {
|
2022-02-01 14:54:34 +01:00
|
|
|
return m.meta
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// MetaList returns the the list of meta data tags as sorted list of key value tags
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) MetaList() []*lp.Tag {
|
2022-02-01 14:54:34 +01:00
|
|
|
|
|
|
|
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
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// String implements the stringer interface for data type ccMetric
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) String() string {
|
2022-02-01 14:54:34 +01:00
|
|
|
return fmt.Sprintf("%s %v %v %v %d", m.name, m.tags, m.meta, m.Fields(), m.tm.UnixNano())
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// Name returns the measurement name
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) Name() string {
|
|
|
|
return m.name
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:27:16 +01:00
|
|
|
func (m *ccMetric) SetName(name string) {
|
|
|
|
m.name = name
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// Tags returns the the list of tags as key-value-mapping
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) Tags() map[string]string {
|
2022-02-01 14:54:34 +01:00
|
|
|
return m.tags
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// TagList returns the the list of tags as sorted list of key value tags
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) TagList() []*lp.Tag {
|
2022-02-01 14:54:34 +01:00
|
|
|
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
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// Fields returns the list of fields as key-value-mapping
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) Fields() map[string]interface{} {
|
|
|
|
fields := make(map[string]interface{}, len(m.fields))
|
|
|
|
for _, field := range m.fields {
|
|
|
|
fields[field.Key] = field.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// FieldList returns the list of fields
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) FieldList() []*lp.Field {
|
|
|
|
return m.fields
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// Time returns timestamp
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) Time() time.Time {
|
|
|
|
return m.tm
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// SetTime sets the timestamp
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) SetTime(t time.Time) {
|
|
|
|
m.tm = t
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// HasTag checks if a tag with key equal to <key> is present in the list of tags
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) HasTag(key string) bool {
|
2022-02-01 14:54:34 +01:00
|
|
|
_, ok := m.tags[key]
|
|
|
|
return ok
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// GetTag returns the tag with tag's key equal to <key>
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) GetTag(key string) (string, bool) {
|
2022-02-01 14:54:34 +01:00
|
|
|
value, ok := m.tags[key]
|
|
|
|
return value, ok
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// RemoveTag removes the tag with tag's key equal to <key>
|
|
|
|
// and keeps the tag list ordered by the keys
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) RemoveTag(key string) {
|
2022-02-01 14:54:34 +01:00
|
|
|
delete(m.tags, key)
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// AddTag adds a tag (consisting of key and value)
|
|
|
|
// and keeps the tag list ordered by the keys
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) AddTag(key, value string) {
|
2022-02-01 14:54:34 +01:00
|
|
|
m.tags[key] = value
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// HasTag checks if a meta data tag with meta data's key equal to <key> is present in the list of meta data tags
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) HasMeta(key string) bool {
|
2022-02-01 14:54:34 +01:00
|
|
|
_, ok := m.meta[key]
|
|
|
|
return ok
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// GetMeta returns the meta data tag with meta data's key equal to <key>
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) GetMeta(key string) (string, bool) {
|
2022-02-01 14:54:34 +01:00
|
|
|
value, ok := m.meta[key]
|
|
|
|
return value, ok
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// RemoveMeta removes the meta data tag with tag's key equal to <key>
|
|
|
|
// and keeps the meta data tag list ordered by the keys
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) RemoveMeta(key string) {
|
2022-02-01 14:54:34 +01:00
|
|
|
delete(m.meta, key)
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// AddMeta adds a meta data tag (consisting of key and value)
|
|
|
|
// and keeps the meta data list ordered by the keys
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) AddMeta(key, value string) {
|
2022-02-01 14:54:34 +01:00
|
|
|
m.meta[key] = value
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// AddField adds a field (consisting of key and value) to the unordered list of fields
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *ccMetric) AddField(key string, value interface{}) {
|
|
|
|
for i, field := range m.fields {
|
|
|
|
if key == field.Key {
|
|
|
|
m.fields[i] = &lp.Field{Key: key, Value: convertField(value)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.fields = append(m.fields, &lp.Field{Key: key, Value: convertField(value)})
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// GetField returns the field with field's key equal to <key>
|
2022-01-30 14:30:06 +01:00
|
|
|
func (m *ccMetric) GetField(key string) (interface{}, bool) {
|
|
|
|
for _, field := range m.fields {
|
|
|
|
if field.Key == key {
|
|
|
|
return field.Value, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// HasField checks if a field with field's key equal to <key> is present in the list of fields
|
2022-01-30 14:30:06 +01:00
|
|
|
func (m *ccMetric) HasField(key string) bool {
|
|
|
|
for _, field := range m.fields {
|
|
|
|
if field.Key == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// RemoveField removes the field with field's key equal to <key>
|
|
|
|
// from the unordered list of fields
|
2022-01-30 14:30:06 +01:00
|
|
|
func (m *ccMetric) RemoveField(key string) {
|
|
|
|
for i, field := range m.fields {
|
|
|
|
if field.Key == key {
|
|
|
|
copy(m.fields[i:], m.fields[i+1:])
|
|
|
|
m.fields[len(m.fields)-1] = nil
|
|
|
|
m.fields = m.fields[:len(m.fields)-1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// New creates a new measurement point
|
2022-01-25 15:37:43 +01:00
|
|
|
func New(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
meta map[string]string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tm time.Time,
|
|
|
|
) (CCMetric, error) {
|
|
|
|
m := &ccMetric{
|
|
|
|
name: name,
|
2022-02-01 14:54:34 +01:00
|
|
|
tags: make(map[string]string, len(tags)),
|
|
|
|
meta: make(map[string]string, len(meta)),
|
|
|
|
fields: make([]*lp.Field, 0, len(fields)),
|
2022-01-25 15:37:43 +01:00
|
|
|
tm: tm,
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// deep copy tags
|
|
|
|
for k, v := range tags {
|
|
|
|
m.tags[k] = v
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
// deep copy meta data tags
|
|
|
|
for k, v := range meta {
|
|
|
|
m.meta[k] = v
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// Unsorted list of fields
|
2022-02-01 14:54:34 +01:00
|
|
|
for k, v := range fields {
|
|
|
|
v := convertField(v)
|
|
|
|
if v == nil {
|
|
|
|
continue
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-02-01 14:54:34 +01:00
|
|
|
m.AddField(k, v)
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// FromMetric copies the metric <other>
|
2022-02-01 14:54:34 +01:00
|
|
|
func FromMetric(other ccMetric) CCMetric {
|
2022-01-25 15:37:43 +01:00
|
|
|
m := &ccMetric{
|
|
|
|
name: other.Name(),
|
2022-02-01 14:54:34 +01:00
|
|
|
tags: make(map[string]string),
|
2022-01-25 15:37:43 +01:00
|
|
|
fields: make([]*lp.Field, len(other.FieldList())),
|
2022-02-01 14:54:34 +01:00
|
|
|
meta: make(map[string]string),
|
2022-01-25 15:37:43 +01:00
|
|
|
tm: other.Time(),
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
for key, value := range other.Tags() {
|
|
|
|
m.tags[key] = value
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-02-01 14:54:34 +01:00
|
|
|
for key, value := range other.Meta() {
|
|
|
|
m.meta[key] = value
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, field := range other.FieldList() {
|
|
|
|
m.fields[i] = &lp.Field{Key: field.Key, Value: field.Value}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// FromInfluxMetric copies the influxDB line protocol metric <other>
|
2022-01-25 15:37:43 +01:00
|
|
|
func FromInfluxMetric(other lp.Metric) CCMetric {
|
|
|
|
m := &ccMetric{
|
|
|
|
name: other.Name(),
|
2022-02-01 14:54:34 +01:00
|
|
|
tags: make(map[string]string),
|
2022-01-25 15:37:43 +01:00
|
|
|
fields: make([]*lp.Field, len(other.FieldList())),
|
2022-02-01 14:54:34 +01:00
|
|
|
meta: make(map[string]string),
|
2022-01-25 15:37:43 +01:00
|
|
|
tm: other.Time(),
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:54:34 +01:00
|
|
|
for _, otherTag := range other.TagList() {
|
|
|
|
m.tags[otherTag.Key] = otherTag.Value
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
for i, otherField := range other.FieldList() {
|
|
|
|
m.fields[i] = &lp.Field{
|
|
|
|
Key: otherField.Key,
|
|
|
|
Value: otherField.Value,
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:02:00 +01:00
|
|
|
// convertField converts data types of fields by the following schemata:
|
|
|
|
// *float32, *float64, float32, float64 -> float64
|
|
|
|
// *int, *int8, *int16, *int32, *int64, int, int8, int16, int32, int64 -> int64
|
|
|
|
// *uint, *uint8, *uint16, *uint32, *uint64, uint, uint8, uint16, uint32, uint64 -> uint64
|
|
|
|
// *[]byte, *string, []byte, string -> string
|
|
|
|
// *bool, bool -> bool
|
2022-01-25 15:37:43 +01:00
|
|
|
func convertField(v interface{}) interface{} {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case float64:
|
|
|
|
return v
|
|
|
|
case int64:
|
|
|
|
return v
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case bool:
|
|
|
|
return v
|
|
|
|
case int:
|
|
|
|
return int64(v)
|
|
|
|
case uint:
|
|
|
|
return uint64(v)
|
|
|
|
case uint64:
|
|
|
|
return uint64(v)
|
|
|
|
case []byte:
|
|
|
|
return string(v)
|
|
|
|
case int32:
|
|
|
|
return int64(v)
|
|
|
|
case int16:
|
|
|
|
return int64(v)
|
|
|
|
case int8:
|
|
|
|
return int64(v)
|
|
|
|
case uint32:
|
|
|
|
return uint64(v)
|
|
|
|
case uint16:
|
|
|
|
return uint64(v)
|
|
|
|
case uint8:
|
|
|
|
return uint64(v)
|
|
|
|
case float32:
|
|
|
|
return float64(v)
|
|
|
|
case *float64:
|
|
|
|
if v != nil {
|
|
|
|
return *v
|
|
|
|
}
|
|
|
|
case *int64:
|
|
|
|
if v != nil {
|
|
|
|
return *v
|
|
|
|
}
|
|
|
|
case *string:
|
|
|
|
if v != nil {
|
|
|
|
return *v
|
|
|
|
}
|
|
|
|
case *bool:
|
|
|
|
if v != nil {
|
|
|
|
return *v
|
|
|
|
}
|
|
|
|
case *int:
|
|
|
|
if v != nil {
|
|
|
|
return int64(*v)
|
|
|
|
}
|
|
|
|
case *uint:
|
|
|
|
if v != nil {
|
|
|
|
return uint64(*v)
|
|
|
|
}
|
|
|
|
case *uint64:
|
|
|
|
if v != nil {
|
|
|
|
return uint64(*v)
|
|
|
|
}
|
|
|
|
case *[]byte:
|
|
|
|
if v != nil {
|
|
|
|
return string(*v)
|
|
|
|
}
|
|
|
|
case *int32:
|
|
|
|
if v != nil {
|
|
|
|
return int64(*v)
|
|
|
|
}
|
|
|
|
case *int16:
|
|
|
|
if v != nil {
|
|
|
|
return int64(*v)
|
|
|
|
}
|
|
|
|
case *int8:
|
|
|
|
if v != nil {
|
|
|
|
return int64(*v)
|
|
|
|
}
|
|
|
|
case *uint32:
|
|
|
|
if v != nil {
|
|
|
|
return uint64(*v)
|
|
|
|
}
|
|
|
|
case *uint16:
|
|
|
|
if v != nil {
|
|
|
|
return uint64(*v)
|
|
|
|
}
|
|
|
|
case *uint8:
|
|
|
|
if v != nil {
|
|
|
|
return uint64(*v)
|
|
|
|
}
|
|
|
|
case *float32:
|
|
|
|
if v != nil {
|
|
|
|
return float64(*v)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|