2016-01-27 21:21:36 +00:00
|
|
|
package telegraf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/client/v2"
|
2016-09-08 14:22:10 +00:00
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-01-27 21:21:36 +00:00
|
|
|
)
|
|
|
|
|
2016-08-30 17:09:48 +00:00
|
|
|
// ValueType is an enumeration of metric types that represent a simple value.
|
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
// Possible values for the ValueType enum.
|
|
|
|
const (
|
|
|
|
_ ValueType = iota
|
|
|
|
Counter
|
|
|
|
Gauge
|
|
|
|
Untyped
|
|
|
|
)
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
type Metric interface {
|
|
|
|
// Name returns the measurement name of the metric
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Name returns the tags associated with the metric
|
|
|
|
Tags() map[string]string
|
|
|
|
|
|
|
|
// Time return the timestamp for the metric
|
|
|
|
Time() time.Time
|
|
|
|
|
2016-08-30 17:09:48 +00:00
|
|
|
// Type returns the metric type. Can be either telegraf.Gauge or telegraf.Counter
|
|
|
|
Type() ValueType
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
// UnixNano returns the unix nano time of the metric
|
|
|
|
UnixNano() int64
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
// HashID returns a non-cryptographic hash of the metric (name + tags)
|
2016-10-07 15:43:44 +00:00
|
|
|
// NOTE: do not persist & depend on this value to disk.
|
2016-09-08 14:22:10 +00:00
|
|
|
HashID() uint64
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
// Fields returns the fields for the metric
|
|
|
|
Fields() map[string]interface{}
|
|
|
|
|
|
|
|
// String returns a line-protocol string of the metric
|
|
|
|
String() string
|
|
|
|
|
|
|
|
// PrecisionString returns a line-protocol string of the metric, at precision
|
|
|
|
PrecisionString(precison string) string
|
|
|
|
|
|
|
|
// Point returns a influxdb client.Point object
|
|
|
|
Point() *client.Point
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
// SetAggregate sets the metric's aggregate status
|
|
|
|
// This is so that aggregate metrics don't get re-sent to aggregator plugins
|
|
|
|
SetAggregate(bool)
|
|
|
|
// IsAggregate returns true if the metric is an aggregate
|
|
|
|
IsAggregate() bool
|
2016-11-23 12:30:31 +00:00
|
|
|
|
|
|
|
// Copy copies the metric
|
|
|
|
Copy() Metric
|
2016-01-27 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// metric is a wrapper of the influxdb client.Point struct
|
|
|
|
type metric struct {
|
2016-09-08 14:22:10 +00:00
|
|
|
pt models.Point
|
2016-08-30 17:09:48 +00:00
|
|
|
|
|
|
|
mType ValueType
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
isaggregate bool
|
2016-01-27 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 12:29:46 +00:00
|
|
|
func NewMetricFromPoint(pt models.Point) Metric {
|
|
|
|
return &metric{
|
|
|
|
pt: pt,
|
|
|
|
mType: Untyped,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:09:48 +00:00
|
|
|
// NewMetric returns an untyped metric.
|
2016-01-27 21:21:36 +00:00
|
|
|
func NewMetric(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
2016-06-13 14:21:11 +00:00
|
|
|
t time.Time,
|
2016-01-27 21:21:36 +00:00
|
|
|
) (Metric, error) {
|
2016-10-06 12:29:46 +00:00
|
|
|
pt, err := models.NewPoint(name, models.NewTags(tags), fields, t)
|
2016-01-27 21:21:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &metric{
|
2016-08-30 17:09:48 +00:00
|
|
|
pt: pt,
|
|
|
|
mType: Untyped,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGaugeMetric returns a gauge metric.
|
|
|
|
// Gauge metrics should be used when the metric is can arbitrarily go up and
|
|
|
|
// down. ie, temperature, memory usage, cpu usage, etc.
|
|
|
|
func NewGaugeMetric(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
t time.Time,
|
|
|
|
) (Metric, error) {
|
2016-10-06 12:29:46 +00:00
|
|
|
pt, err := models.NewPoint(name, models.NewTags(tags), fields, t)
|
2016-08-30 17:09:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &metric{
|
|
|
|
pt: pt,
|
|
|
|
mType: Gauge,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCounterMetric returns a Counter metric.
|
|
|
|
// Counter metrics should be used when the metric being created is an
|
|
|
|
// always-increasing counter. ie, net bytes received, requests served, errors, etc.
|
|
|
|
func NewCounterMetric(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
t time.Time,
|
|
|
|
) (Metric, error) {
|
2016-10-06 12:29:46 +00:00
|
|
|
pt, err := models.NewPoint(name, models.NewTags(tags), fields, t)
|
2016-08-30 17:09:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &metric{
|
|
|
|
pt: pt,
|
|
|
|
mType: Counter,
|
2016-01-27 21:21:36 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Name() string {
|
|
|
|
return m.pt.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Tags() map[string]string {
|
2016-10-06 12:29:46 +00:00
|
|
|
return m.pt.Tags().Map()
|
2016-01-27 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Time() time.Time {
|
|
|
|
return m.pt.Time()
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:09:48 +00:00
|
|
|
func (m *metric) Type() ValueType {
|
|
|
|
return m.mType
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func (m *metric) HashID() uint64 {
|
|
|
|
return m.pt.HashID()
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (m *metric) UnixNano() int64 {
|
|
|
|
return m.pt.UnixNano()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Fields() map[string]interface{} {
|
|
|
|
return m.pt.Fields()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) String() string {
|
|
|
|
return m.pt.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) PrecisionString(precison string) string {
|
|
|
|
return m.pt.PrecisionString(precison)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) Point() *client.Point {
|
2016-09-08 14:22:10 +00:00
|
|
|
return client.NewPointFrom(m.pt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) IsAggregate() bool {
|
|
|
|
return m.isaggregate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metric) SetAggregate(b bool) {
|
|
|
|
m.isaggregate = b
|
2016-01-27 21:21:36 +00:00
|
|
|
}
|
2016-11-23 12:30:31 +00:00
|
|
|
|
|
|
|
func (m *metric) Copy() Metric {
|
|
|
|
t := time.Time(m.Time())
|
|
|
|
|
|
|
|
tags := make(map[string]string)
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
for k, v := range m.Tags() {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range m.Fields() {
|
|
|
|
fields[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _ := NewMetric(m.Name(), tags, fields, t)
|
|
|
|
return out
|
|
|
|
}
|