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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MetricHandler struct {
|
|
|
|
builder *metric.Builder
|
2019-02-26 18:48:41 +00:00
|
|
|
err error
|
2018-03-28 00:30:51 +00:00
|
|
|
precision time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetricHandler() *MetricHandler {
|
|
|
|
return &MetricHandler{
|
|
|
|
builder: metric.NewBuilder(),
|
|
|
|
precision: time.Nanosecond,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MetricHandler) SetTimeFunc(f metric.TimeFunc) {
|
|
|
|
h.builder.TimeFunc = f
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:32:33 +00:00
|
|
|
func (h *MetricHandler) SetTimePrecision(precision time.Duration) {
|
|
|
|
h.builder.TimePrecision = precision
|
|
|
|
h.precision = precision
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MetricHandler) Metric() (telegraf.Metric, error) {
|
2019-02-26 18:48:41 +00:00
|
|
|
m, err := h.builder.Metric()
|
|
|
|
h.builder.Reset()
|
|
|
|
return m, err
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
func (h *MetricHandler) SetMeasurement(name []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
h.builder.SetName(nameUnescape(name))
|
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) AddTag(key []byte, value []byte) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
tk := unescape(key)
|
|
|
|
tv := unescape(value)
|
|
|
|
h.builder.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
|
|
|
}
|
|
|
|
h.builder.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
|
|
|
}
|
|
|
|
h.builder.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
|
|
|
}
|
|
|
|
h.builder.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)
|
|
|
|
h.builder.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
|
|
|
}
|
|
|
|
h.builder.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
|
|
|
}
|
|
|
|
ns := v * int64(h.precision)
|
|
|
|
h.builder.SetTime(time.Unix(0, ns))
|
2019-02-26 18:48:41 +00:00
|
|
|
return nil
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|