2016-11-22 12:51:57 +00:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
type metric struct {
|
|
|
|
name string
|
|
|
|
tags []*telegraf.Tag
|
|
|
|
fields []*telegraf.Field
|
|
|
|
tm time.Time
|
|
|
|
|
|
|
|
tp telegraf.ValueType
|
|
|
|
aggregate bool
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
func New(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
2018-03-28 00:30:51 +00:00
|
|
|
tm time.Time,
|
|
|
|
tp ...telegraf.ValueType,
|
2016-11-22 12:51:57 +00:00
|
|
|
) (telegraf.Metric, error) {
|
2018-03-28 00:30:51 +00:00
|
|
|
var vtype telegraf.ValueType
|
|
|
|
if len(tp) > 0 {
|
|
|
|
vtype = tp[0]
|
2016-11-22 12:51:57 +00:00
|
|
|
} else {
|
2018-03-28 00:30:51 +00:00
|
|
|
vtype = telegraf.Untyped
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := &metric{
|
2018-03-28 00:30:51 +00:00
|
|
|
name: name,
|
|
|
|
tags: nil,
|
|
|
|
fields: nil,
|
|
|
|
tm: tm,
|
|
|
|
tp: vtype,
|
2016-11-29 18:06:14 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
if len(tags) > 0 {
|
|
|
|
m.tags = make([]*telegraf.Tag, 0, len(tags))
|
|
|
|
for k, v := range tags {
|
|
|
|
m.tags = append(m.tags,
|
|
|
|
&telegraf.Tag{Key: k, Value: v})
|
2017-07-11 22:54:38 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
sort.Slice(m.tags, func(i, j int) bool { return m.tags[i].Key < m.tags[j].Key })
|
2016-11-29 18:06:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
if len(fields) > 0 {
|
|
|
|
m.fields = make([]*telegraf.Field, 0, len(fields))
|
|
|
|
for k, v := range fields {
|
|
|
|
v := convertField(v)
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.AddField(k, v)
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2019-03-01 19:21:31 +00:00
|
|
|
// FromMetric returns a deep copy of the metric with any tracking information
|
|
|
|
// removed.
|
|
|
|
func FromMetric(other telegraf.Metric) telegraf.Metric {
|
|
|
|
m := &metric{
|
|
|
|
name: other.Name(),
|
|
|
|
tags: make([]*telegraf.Tag, len(other.TagList())),
|
|
|
|
fields: make([]*telegraf.Field, len(other.FieldList())),
|
|
|
|
tm: other.Time(),
|
|
|
|
tp: other.Type(),
|
|
|
|
aggregate: other.IsAggregate(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tag := range other.TagList() {
|
|
|
|
m.tags[i] = &telegraf.Tag{Key: tag.Key, Value: tag.Value}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, field := range other.FieldList() {
|
|
|
|
m.fields[i] = &telegraf.Field{Key: field.Key, Value: field.Value}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) String() string {
|
|
|
|
return fmt.Sprintf("%s %v %v %d", m.name, m.Tags(), m.Fields(), m.tm.UnixNano())
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) Name() string {
|
|
|
|
return m.name
|
2017-08-31 04:16:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) Tags() map[string]string {
|
|
|
|
tags := make(map[string]string, len(m.tags))
|
|
|
|
for _, tag := range m.tags {
|
|
|
|
tags[tag.Key] = tag.Value
|
2017-08-31 04:16:37 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return tags
|
2017-08-31 04:16:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) TagList() []*telegraf.Tag {
|
|
|
|
return m.tags
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) Fields() map[string]interface{} {
|
|
|
|
fields := make(map[string]interface{}, len(m.fields))
|
|
|
|
for _, field := range m.fields {
|
|
|
|
fields[field.Key] = field.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) FieldList() []*telegraf.Field {
|
|
|
|
return m.fields
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) Time() time.Time {
|
|
|
|
return m.tm
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Type() telegraf.ValueType {
|
2018-03-28 00:30:51 +00:00
|
|
|
return m.tp
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) SetName(name string) {
|
|
|
|
m.name = name
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) AddPrefix(prefix string) {
|
|
|
|
m.name = prefix + m.name
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) AddSuffix(suffix string) {
|
|
|
|
m.name = m.name + suffix
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) AddTag(key, value string) {
|
|
|
|
for i, tag := range m.tags {
|
|
|
|
if key > tag.Key {
|
|
|
|
continue
|
2016-12-05 13:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
if key == tag.Key {
|
|
|
|
tag.Value = value
|
2018-04-21 01:39:31 +00:00
|
|
|
return
|
2016-12-05 13:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
m.tags = append(m.tags, nil)
|
|
|
|
copy(m.tags[i+1:], m.tags[i:])
|
|
|
|
m.tags[i] = &telegraf.Tag{Key: key, Value: value}
|
|
|
|
return
|
2016-12-05 13:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
m.tags = append(m.tags, &telegraf.Tag{Key: key, Value: value})
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) HasTag(key string) bool {
|
|
|
|
for _, tag := range m.tags {
|
|
|
|
if tag.Key == key {
|
|
|
|
return true
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return false
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) GetTag(key string) (string, bool) {
|
|
|
|
for _, tag := range m.tags {
|
|
|
|
if tag.Key == key {
|
|
|
|
return tag.Value, true
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return "", false
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) RemoveTag(key string) {
|
|
|
|
for i, tag := range m.tags {
|
|
|
|
if tag.Key == key {
|
|
|
|
copy(m.tags[i:], m.tags[i+1:])
|
|
|
|
m.tags[len(m.tags)-1] = nil
|
|
|
|
m.tags = m.tags[:len(m.tags)-1]
|
|
|
|
return
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) AddField(key string, value interface{}) {
|
|
|
|
for i, field := range m.fields {
|
|
|
|
if key == field.Key {
|
|
|
|
m.fields[i] = &telegraf.Field{Key: key, Value: convertField(value)}
|
2018-07-14 05:54:34 +00:00
|
|
|
return
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
m.fields = append(m.fields, &telegraf.Field{Key: key, Value: convertField(value)})
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2016-12-01 15:18:46 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) HasField(key string) bool {
|
|
|
|
for _, field := range m.fields {
|
|
|
|
if field.Key == key {
|
|
|
|
return true
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return false
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) GetField(key string) (interface{}, bool) {
|
|
|
|
for _, field := range m.fields {
|
|
|
|
if field.Key == key {
|
|
|
|
return field.Value, true
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return nil, false
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) 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
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:00:03 +00:00
|
|
|
func (m *metric) SetTime(t time.Time) {
|
|
|
|
m.tm = t
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) Copy() telegraf.Metric {
|
|
|
|
m2 := &metric{
|
|
|
|
name: m.name,
|
|
|
|
tags: make([]*telegraf.Tag, len(m.tags)),
|
|
|
|
fields: make([]*telegraf.Field, len(m.fields)),
|
|
|
|
tm: m.tm,
|
|
|
|
tp: m.tp,
|
|
|
|
aggregate: m.aggregate,
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
for i, tag := range m.tags {
|
2019-06-14 22:26:56 +00:00
|
|
|
m2.tags[i] = &telegraf.Tag{Key: tag.Key, Value: tag.Value}
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2016-12-01 15:18:46 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
for i, field := range m.fields {
|
2019-06-14 22:26:56 +00:00
|
|
|
m2.fields[i] = &telegraf.Field{Key: field.Key, Value: field.Value}
|
2016-12-01 15:18:46 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return m2
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) SetAggregate(b bool) {
|
|
|
|
m.aggregate = true
|
2016-12-05 13:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (m *metric) IsAggregate() bool {
|
|
|
|
return m.aggregate
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) HashID() uint64 {
|
2018-03-28 00:30:51 +00:00
|
|
|
h := fnv.New64a()
|
|
|
|
h.Write([]byte(m.name))
|
2018-04-13 01:09:31 +00:00
|
|
|
h.Write([]byte("\n"))
|
2018-03-28 00:30:51 +00:00
|
|
|
for _, tag := range m.tags {
|
|
|
|
h.Write([]byte(tag.Key))
|
2018-04-13 01:09:31 +00:00
|
|
|
h.Write([]byte("\n"))
|
2018-03-28 00:30:51 +00:00
|
|
|
h.Write([]byte(tag.Value))
|
2018-04-13 01:09:31 +00:00
|
|
|
h.Write([]byte("\n"))
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
return h.Sum64()
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (m *metric) Accept() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Reject() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Drop() {
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
// Convert field to a supported type or nil if unconvertible
|
|
|
|
func convertField(v interface{}) interface{} {
|
2016-11-22 12:51:57 +00:00
|
|
|
switch v := v.(type) {
|
|
|
|
case float64:
|
2018-03-28 00:30:51 +00:00
|
|
|
return v
|
2016-11-22 12:51:57 +00:00
|
|
|
case int64:
|
2018-03-28 00:30:51 +00:00
|
|
|
return v
|
2016-11-22 12:51:57 +00:00
|
|
|
case string:
|
2018-03-30 23:57:35 +00:00
|
|
|
return v
|
2016-11-22 12:51:57 +00:00
|
|
|
case bool:
|
2018-03-28 00:30:51 +00:00
|
|
|
return v
|
2016-11-22 12:51:57 +00:00
|
|
|
case int:
|
2018-03-28 00:30:51 +00:00
|
|
|
return int64(v)
|
|
|
|
case uint:
|
2018-03-29 20:31:43 +00:00
|
|
|
return uint64(v)
|
2016-11-28 18:19:35 +00:00
|
|
|
case uint64:
|
2018-03-28 23:43:25 +00:00
|
|
|
return uint64(v)
|
2018-03-28 00:30:51 +00:00
|
|
|
case []byte:
|
|
|
|
return string(v)
|
|
|
|
case int32:
|
|
|
|
return int64(v)
|
|
|
|
case int16:
|
|
|
|
return int64(v)
|
|
|
|
case int8:
|
|
|
|
return int64(v)
|
2016-11-22 12:51:57 +00:00
|
|
|
case uint32:
|
2018-03-29 20:31:43 +00:00
|
|
|
return uint64(v)
|
2016-11-22 12:51:57 +00:00
|
|
|
case uint16:
|
2018-03-29 20:31:43 +00:00
|
|
|
return uint64(v)
|
2016-11-22 12:51:57 +00:00
|
|
|
case uint8:
|
2018-03-29 20:31:43 +00:00
|
|
|
return uint64(v)
|
2016-11-22 12:51:57 +00:00
|
|
|
case float32:
|
2018-03-28 00:30:51 +00:00
|
|
|
return float64(v)
|
2019-03-04 21:34:52 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
default:
|
2018-03-28 00:30:51 +00:00
|
|
|
return nil
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|
2019-03-04 21:34:52 +00:00
|
|
|
return nil
|
2016-11-22 12:51:57 +00:00
|
|
|
}
|