2016-03-17 17:50:39 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2018-05-05 01:27:31 +00:00
|
|
|
"encoding/json"
|
2020-04-28 20:41:25 +00:00
|
|
|
"math"
|
2017-03-30 00:12:29 +00:00
|
|
|
"time"
|
2016-03-17 17:50:39 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-05-05 01:27:31 +00:00
|
|
|
type serializer struct {
|
2017-03-30 00:12:29 +00:00
|
|
|
TimestampUnits time.Duration
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 01:27:31 +00:00
|
|
|
func NewSerializer(timestampUnits time.Duration) (*serializer, error) {
|
|
|
|
s := &serializer{
|
|
|
|
TimestampUnits: truncateDuration(timestampUnits),
|
2017-03-30 00:12:29 +00:00
|
|
|
}
|
2018-05-05 01:27:31 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
|
|
|
m := s.createObject(metric)
|
|
|
|
serialized, err := json.Marshal(m)
|
2016-03-17 17:50:39 +00:00
|
|
|
if err != nil {
|
2016-11-22 12:51:57 +00:00
|
|
|
return []byte{}, err
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
serialized = append(serialized, '\n')
|
2016-03-17 17:50:39 +00:00
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
return serialized, nil
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
2018-05-05 01:27:31 +00:00
|
|
|
|
|
|
|
func (s *serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
|
|
|
objects := make([]interface{}, 0, len(metrics))
|
|
|
|
for _, metric := range metrics {
|
|
|
|
m := s.createObject(metric)
|
|
|
|
objects = append(objects, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := map[string]interface{}{
|
|
|
|
"metrics": objects,
|
|
|
|
}
|
|
|
|
|
|
|
|
serialized, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return serialized, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serializer) createObject(metric telegraf.Metric) map[string]interface{} {
|
|
|
|
m := make(map[string]interface{}, 4)
|
2020-04-28 20:41:25 +00:00
|
|
|
|
|
|
|
tags := make(map[string]string, len(metric.TagList()))
|
|
|
|
for _, tag := range metric.TagList() {
|
|
|
|
tags[tag.Key] = tag.Value
|
|
|
|
}
|
|
|
|
m["tags"] = tags
|
|
|
|
|
|
|
|
fields := make(map[string]interface{}, len(metric.FieldList()))
|
|
|
|
for _, field := range metric.FieldList() {
|
|
|
|
switch fv := field.Value.(type) {
|
|
|
|
case float64:
|
|
|
|
// JSON does not support these special values
|
|
|
|
if math.IsNaN(fv) || math.IsInf(fv, 0) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields[field.Key] = field.Value
|
|
|
|
}
|
|
|
|
m["fields"] = fields
|
|
|
|
|
2018-05-05 01:27:31 +00:00
|
|
|
m["name"] = metric.Name()
|
|
|
|
m["timestamp"] = metric.Time().UnixNano() / int64(s.TimestampUnits)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncateDuration(units time.Duration) time.Duration {
|
|
|
|
// Default precision is 1s
|
|
|
|
if units <= 0 {
|
|
|
|
return time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the power of ten less than the duration
|
|
|
|
d := time.Nanosecond
|
|
|
|
for {
|
|
|
|
if d*10 > units {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
d = d * 10
|
|
|
|
}
|
|
|
|
}
|