2018-03-28 00:30:51 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-26 18:48:41 +00:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2018-03-28 00:30:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/metric"
|
|
|
|
)
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
// MetricHandler implements the Handler interface and produces telegraf.Metric.
|
2018-03-28 00:30:51 +00:00
|
|
|
type MetricHandler struct {
|
2020-03-04 18:13:44 +00:00
|
|
|
err error
|
|
|
|
timePrecision time.Duration
|
|
|
|
timeFunc TimeFunc
|
|
|
|
metric telegraf.Metric
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetricHandler() *MetricHandler {
|
|
|
|
return &MetricHandler{
|
2020-03-04 18:13:44 +00:00
|
|
|
timePrecision: time.Nanosecond,
|
|
|
|
timeFunc: time.Now,
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
func (h *MetricHandler) SetTimePrecision(p time.Duration) {
|
|
|
|
h.timePrecision = p
|
|
|
|
// When the timestamp is omitted from the metric, the timestamp
|
|
|
|
// comes from the server clock, truncated to the nearest unit of
|
|
|
|
// measurement provided in precision.
|
|
|
|
//
|
2020-05-14 07:41:58 +00:00
|
|
|
// When a timestamp is provided in the metric, precision is
|
2020-03-04 18:13:44 +00:00
|
|
|
// overloaded to hold the unit of measurement of the timestamp.
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
func (h *MetricHandler) SetTimeFunc(f TimeFunc) {
|
|
|
|
h.timeFunc = f
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MetricHandler) Metric() (telegraf.Metric, error) {
|
2020-03-04 18:13:44 +00:00
|
|
|
if h.metric.Time().IsZero() {
|
|
|
|
h.metric.SetTime(h.timeFunc().Truncate(h.timePrecision))
|
|
|
|
}
|
|
|
|
return h.metric, nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) SetMeasurement(name []byte) error {
|
2020-03-04 18:13:44 +00:00
|
|
|
var err error
|
|
|
|
h.metric, err = metric.New(nameUnescape(name),
|
|
|
|
nil, nil, time.Time{})
|
|
|
|
return err
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddTag(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
tk := unescape(key)
|
|
|
|
tv := unescape(value)
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddTag(tk, tv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddInt(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
fk := unescape(key)
|
|
|
|
fv, err := parseIntBytes(bytes.TrimSuffix(value, []byte("i")), 10, 64)
|
|
|
|
if err != nil {
|
2019-02-26 18:48:41 +00:00
|
|
|
if numerr, ok := err.(*strconv.NumError); ok {
|
|
|
|
return numerr.Err
|
|
|
|
}
|
|
|
|
return err
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddField(fk, fv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddUint(key []byte, value []byte) error {
|
2018-03-28 23:43:25 +00:00
|
|
|
fk := unescape(key)
|
|
|
|
fv, err := parseUintBytes(bytes.TrimSuffix(value, []byte("u")), 10, 64)
|
|
|
|
if err != nil {
|
2019-02-26 18:48:41 +00:00
|
|
|
if numerr, ok := err.(*strconv.NumError); ok {
|
|
|
|
return numerr.Err
|
|
|
|
}
|
|
|
|
return err
|
2018-03-28 23:43:25 +00:00
|
|
|
}
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddField(fk, fv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 23:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddFloat(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
fk := unescape(key)
|
|
|
|
fv, err := parseFloatBytes(value, 64)
|
|
|
|
if err != nil {
|
2019-02-26 18:48:41 +00:00
|
|
|
if numerr, ok := err.(*strconv.NumError); ok {
|
|
|
|
return numerr.Err
|
|
|
|
}
|
|
|
|
return err
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddField(fk, fv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddString(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
fk := unescape(key)
|
|
|
|
fv := stringFieldUnescape(value)
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddField(fk, fv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) AddBool(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
fk := unescape(key)
|
|
|
|
fv, err := parseBoolBytes(value)
|
|
|
|
if err != nil {
|
2019-02-26 18:48:41 +00:00
|
|
|
return errors.New("unparseable bool")
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2020-03-04 18:13:44 +00:00
|
|
|
h.metric.AddField(fk, fv)
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) SetTimestamp(tm []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
v, err := parseIntBytes(tm, 10, 64)
|
|
|
|
if err != nil {
|
2019-02-26 18:48:41 +00:00
|
|
|
if numerr, ok := err.(*strconv.NumError); ok {
|
|
|
|
return numerr.Err
|
|
|
|
}
|
|
|
|
return err
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
2019-04-02 18:14:56 +00:00
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
//time precision is overloaded to mean time unit here
|
|
|
|
ns := v * int64(h.timePrecision)
|
|
|
|
h.metric.SetTime(time.Unix(0, ns))
|
|
|
|
return nil
|
2019-04-02 18:14:56 +00:00
|
|
|
}
|