telegraf/plugins/serializers/json/json.go

34 lines
789 B
Go
Raw Permalink Normal View History

2016-03-17 17:50:39 +00:00
package json
import (
ejson "encoding/json"
"time"
2016-03-17 17:50:39 +00:00
"github.com/influxdata/telegraf"
)
type JsonSerializer struct {
TimestampUnits time.Duration
2016-03-17 17:50:39 +00:00
}
func (s *JsonSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
2016-03-17 17:50:39 +00:00
m := make(map[string]interface{})
units_nanoseconds := s.TimestampUnits.Nanoseconds()
// if the units passed in were less than or equal to zero,
// then serialize the timestamp in seconds (the default)
if units_nanoseconds <= 0 {
units_nanoseconds = 1000000000
}
2016-03-17 17:50:39 +00:00
m["tags"] = metric.Tags()
m["fields"] = metric.Fields()
m["name"] = metric.Name()
m["timestamp"] = metric.UnixNano() / units_nanoseconds
2016-03-17 17:50:39 +00:00
serialized, err := ejson.Marshal(m)
if err != nil {
return []byte{}, err
2016-03-17 17:50:39 +00:00
}
serialized = append(serialized, '\n')
2016-03-17 17:50:39 +00:00
return serialized, nil
2016-03-17 17:50:39 +00:00
}