2016-02-10 22:50:07 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
2018-03-28 00:30:51 +00:00
|
|
|
"bytes"
|
2018-04-19 23:24:31 +00:00
|
|
|
"fmt"
|
2018-03-28 00:30:51 +00:00
|
|
|
"io"
|
2018-04-19 23:24:31 +00:00
|
|
|
"log"
|
2018-03-28 00:30:51 +00:00
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-04-20 21:56:28 +00:00
|
|
|
const MaxInt64 = int64(^uint64(0) >> 1)
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
type FieldSortOrder int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoSortFields FieldSortOrder = iota
|
|
|
|
SortFields
|
|
|
|
)
|
|
|
|
|
2018-03-29 20:31:43 +00:00
|
|
|
type FieldTypeSupport int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UintSupport FieldTypeSupport = 1 << iota
|
|
|
|
)
|
|
|
|
|
2018-08-14 20:36:29 +00:00
|
|
|
var (
|
|
|
|
NeedMoreSpace = "need more space"
|
|
|
|
InvalidName = "invalid name"
|
|
|
|
NoFields = "no serializable fields"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MetricError is an error causing an entire metric to be unserializable.
|
2018-04-19 23:24:31 +00:00
|
|
|
type MetricError struct {
|
2018-08-14 20:36:29 +00:00
|
|
|
series string
|
|
|
|
reason string
|
2018-04-19 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e MetricError) Error() string {
|
2018-08-14 20:36:29 +00:00
|
|
|
if e.series != "" {
|
|
|
|
return fmt.Sprintf("%q: %s", e.series, e.reason)
|
|
|
|
}
|
|
|
|
return e.reason
|
2018-04-19 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FieldError is an error causing a field to be unserializable.
|
|
|
|
type FieldError struct {
|
2018-08-14 20:36:29 +00:00
|
|
|
reason string
|
2018-04-19 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e FieldError) Error() string {
|
2018-08-14 20:36:29 +00:00
|
|
|
return e.reason
|
2018-04-19 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
// Serializer is a serializer for line protocol.
|
|
|
|
type Serializer struct {
|
2018-03-29 20:31:43 +00:00
|
|
|
maxLineBytes int
|
|
|
|
bytesWritten int
|
|
|
|
fieldSortOrder FieldSortOrder
|
|
|
|
fieldTypeSupport FieldTypeSupport
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
buf bytes.Buffer
|
|
|
|
header []byte
|
|
|
|
footer []byte
|
|
|
|
pair []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSerializer() *Serializer {
|
|
|
|
serializer := &Serializer{
|
|
|
|
fieldSortOrder: NoSortFields,
|
|
|
|
|
|
|
|
header: make([]byte, 0, 50),
|
|
|
|
footer: make([]byte, 0, 21),
|
|
|
|
pair: make([]byte, 0, 50),
|
|
|
|
}
|
|
|
|
return serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) SetMaxLineBytes(bytes int) {
|
|
|
|
s.maxLineBytes = bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) SetFieldSortOrder(order FieldSortOrder) {
|
|
|
|
s.fieldSortOrder = order
|
|
|
|
}
|
|
|
|
|
2018-03-29 20:31:43 +00:00
|
|
|
func (s *Serializer) SetFieldTypeSupport(typeSupport FieldTypeSupport) {
|
|
|
|
s.fieldTypeSupport = typeSupport
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
// Serialize writes the telegraf.Metric to a byte slice. May produce multiple
|
|
|
|
// lines of output if longer than maximum line length. Lines are terminated
|
|
|
|
// with a newline (LF) char.
|
|
|
|
func (s *Serializer) Serialize(m telegraf.Metric) ([]byte, error) {
|
|
|
|
s.buf.Reset()
|
|
|
|
err := s.writeMetric(&s.buf, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]byte, s.buf.Len())
|
|
|
|
copy(out, s.buf.Bytes())
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 20:36:29 +00:00
|
|
|
// SerializeBatch writes the slice of metrics and returns a byte slice of the
|
|
|
|
// results. The returned byte slice may contain multiple lines of data.
|
2018-05-05 01:27:31 +00:00
|
|
|
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
2018-08-14 20:36:29 +00:00
|
|
|
s.buf.Reset()
|
2018-05-05 01:27:31 +00:00
|
|
|
for _, m := range metrics {
|
2018-08-14 20:36:29 +00:00
|
|
|
_, err := s.Write(&s.buf, m)
|
2018-05-05 01:27:31 +00:00
|
|
|
if err != nil {
|
2019-06-04 00:34:48 +00:00
|
|
|
if _, ok := err.(*MetricError); ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-05 01:27:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 20:36:29 +00:00
|
|
|
out := make([]byte, s.buf.Len())
|
|
|
|
copy(out, s.buf.Bytes())
|
|
|
|
return out, nil
|
2018-05-05 01:27:31 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
func (s *Serializer) Write(w io.Writer, m telegraf.Metric) (int, error) {
|
|
|
|
err := s.writeMetric(w, m)
|
|
|
|
return s.bytesWritten, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) writeString(w io.Writer, str string) error {
|
|
|
|
n, err := io.WriteString(w, str)
|
|
|
|
s.bytesWritten += n
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) write(w io.Writer, b []byte) error {
|
|
|
|
n, err := w.Write(b)
|
|
|
|
s.bytesWritten += n
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) buildHeader(m telegraf.Metric) error {
|
|
|
|
s.header = s.header[:0]
|
|
|
|
|
|
|
|
name := nameEscape(m.Name())
|
|
|
|
if name == "" {
|
2018-08-14 20:36:29 +00:00
|
|
|
return s.newMetricError(InvalidName)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.header = append(s.header, name...)
|
|
|
|
|
|
|
|
for _, tag := range m.TagList() {
|
|
|
|
key := escape(tag.Key)
|
|
|
|
value := escape(tag.Value)
|
|
|
|
|
|
|
|
// Some keys and values are not encodeable as line protocol, such as
|
|
|
|
// those with a trailing '\' or empty strings.
|
|
|
|
if key == "" || value == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.header = append(s.header, ',')
|
|
|
|
s.header = append(s.header, key...)
|
|
|
|
s.header = append(s.header, '=')
|
|
|
|
s.header = append(s.header, value...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.header = append(s.header, ' ')
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) buildFooter(m telegraf.Metric) {
|
|
|
|
s.footer = s.footer[:0]
|
|
|
|
s.footer = append(s.footer, ' ')
|
|
|
|
s.footer = strconv.AppendInt(s.footer, m.Time().UnixNano(), 10)
|
|
|
|
s.footer = append(s.footer, '\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) buildFieldPair(key string, value interface{}) error {
|
|
|
|
s.pair = s.pair[:0]
|
|
|
|
key = escape(key)
|
|
|
|
|
|
|
|
// Some keys are not encodeable as line protocol, such as those with a
|
|
|
|
// trailing '\' or empty strings.
|
|
|
|
if key == "" {
|
2018-04-19 23:24:31 +00:00
|
|
|
return &FieldError{"invalid field key"}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.pair = append(s.pair, key...)
|
|
|
|
s.pair = append(s.pair, '=')
|
2018-03-29 20:31:43 +00:00
|
|
|
pair, err := s.appendFieldValue(s.pair, value)
|
2018-03-28 00:30:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.pair = pair
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = s.buildHeader(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.buildFooter(m)
|
|
|
|
|
|
|
|
if s.fieldSortOrder == SortFields {
|
|
|
|
sort.Slice(m.FieldList(), func(i, j int) bool {
|
|
|
|
return m.FieldList()[i].Key < m.FieldList()[j].Key
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pairsLen := 0
|
|
|
|
firstField := true
|
|
|
|
for _, field := range m.FieldList() {
|
|
|
|
err = s.buildFieldPair(field.Key, field.Value)
|
|
|
|
if err != nil {
|
2018-04-19 23:24:31 +00:00
|
|
|
log.Printf(
|
|
|
|
"D! [serializers.influx] could not serialize field %q: %v; discarding field",
|
|
|
|
field.Key, err)
|
2018-03-28 00:30:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesNeeded := len(s.header) + pairsLen + len(s.pair) + len(s.footer)
|
|
|
|
|
|
|
|
// Additional length needed for field separator `,`
|
|
|
|
if !firstField {
|
|
|
|
bytesNeeded += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.maxLineBytes > 0 && bytesNeeded > s.maxLineBytes {
|
2018-08-14 20:36:29 +00:00
|
|
|
// Need at least one field per line, this metric cannot be fit
|
|
|
|
// into the max line bytes.
|
2018-03-28 00:30:51 +00:00
|
|
|
if firstField {
|
2018-08-14 20:36:29 +00:00
|
|
|
return s.newMetricError(NeedMoreSpace)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.write(w, s.footer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:08:54 +00:00
|
|
|
pairsLen = 0
|
2018-08-14 20:36:29 +00:00
|
|
|
firstField = true
|
2018-03-28 00:30:51 +00:00
|
|
|
bytesNeeded = len(s.header) + len(s.pair) + len(s.footer)
|
|
|
|
|
2018-08-14 20:36:29 +00:00
|
|
|
if bytesNeeded > s.maxLineBytes {
|
|
|
|
return s.newMetricError(NeedMoreSpace)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if firstField {
|
|
|
|
err = s.write(w, s.header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = s.writeString(w, ",")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 20:36:29 +00:00
|
|
|
err = s.write(w, s.pair)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
pairsLen += len(s.pair)
|
|
|
|
firstField = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if firstField {
|
2018-08-14 20:36:29 +00:00
|
|
|
return s.newMetricError(NoFields)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.write(w, s.footer)
|
2018-08-14 20:36:29 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
2018-08-14 20:36:29 +00:00
|
|
|
func (s *Serializer) newMetricError(reason string) *MetricError {
|
|
|
|
if len(s.header) != 0 {
|
|
|
|
series := bytes.TrimRight(s.header, " ")
|
|
|
|
return &MetricError{series: string(series), reason: reason}
|
|
|
|
}
|
|
|
|
return &MetricError{reason: reason}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:31:43 +00:00
|
|
|
func (s *Serializer) appendFieldValue(buf []byte, value interface{}) ([]byte, error) {
|
2018-03-28 00:30:51 +00:00
|
|
|
switch v := value.(type) {
|
2018-03-28 23:43:25 +00:00
|
|
|
case uint64:
|
2018-03-29 20:31:43 +00:00
|
|
|
if s.fieldTypeSupport&UintSupport != 0 {
|
|
|
|
return appendUintField(buf, v), nil
|
|
|
|
} else {
|
2018-04-20 21:56:28 +00:00
|
|
|
if v <= uint64(MaxInt64) {
|
2018-03-29 20:31:43 +00:00
|
|
|
return appendIntField(buf, int64(v)), nil
|
|
|
|
} else {
|
2018-04-20 21:56:28 +00:00
|
|
|
return appendIntField(buf, int64(MaxInt64)), nil
|
2018-03-29 20:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
case int64:
|
|
|
|
return appendIntField(buf, v), nil
|
|
|
|
case float64:
|
|
|
|
if math.IsNaN(v) {
|
2018-04-19 23:24:31 +00:00
|
|
|
return nil, &FieldError{"is NaN"}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if math.IsInf(v, 0) {
|
2018-04-19 23:24:31 +00:00
|
|
|
return nil, &FieldError{"is Inf"}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return appendFloatField(buf, v), nil
|
|
|
|
case string:
|
|
|
|
return appendStringField(buf, v), nil
|
|
|
|
case bool:
|
|
|
|
return appendBoolField(buf, v), nil
|
2018-04-19 23:24:31 +00:00
|
|
|
default:
|
|
|
|
return buf, &FieldError{fmt.Sprintf("invalid value type: %T", v)}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 23:43:25 +00:00
|
|
|
func appendUintField(buf []byte, value uint64) []byte {
|
|
|
|
return append(strconv.AppendUint(buf, value, 10), 'u')
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func appendIntField(buf []byte, value int64) []byte {
|
|
|
|
return append(strconv.AppendInt(buf, value, 10), 'i')
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendFloatField(buf []byte, value float64) []byte {
|
2018-03-28 21:32:40 +00:00
|
|
|
return strconv.AppendFloat(buf, value, 'f', -1, 64)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendBoolField(buf []byte, value bool) []byte {
|
|
|
|
return strconv.AppendBool(buf, value)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func appendStringField(buf []byte, value string) []byte {
|
|
|
|
buf = append(buf, '"')
|
|
|
|
buf = append(buf, stringFieldEscape(value)...)
|
|
|
|
buf = append(buf, '"')
|
|
|
|
return buf
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|