Adds a new json_timestamp_units configuration parameter (#2587)
This commit is contained in:
@@ -2,19 +2,27 @@ package json
|
||||
|
||||
import (
|
||||
ejson "encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
)
|
||||
|
||||
type JsonSerializer struct {
|
||||
TimestampUnits time.Duration
|
||||
}
|
||||
|
||||
func (s *JsonSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||
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
|
||||
}
|
||||
m["tags"] = metric.Tags()
|
||||
m["fields"] = metric.Fields()
|
||||
m["name"] = metric.Name()
|
||||
m["timestamp"] = metric.UnixNano() / 1000000000
|
||||
m["timestamp"] = metric.UnixNano() / units_nanoseconds
|
||||
serialized, err := ejson.Marshal(m)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
|
||||
@@ -2,6 +2,7 @@ package serializers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
|
||||
@@ -29,7 +30,7 @@ type Serializer interface {
|
||||
// Config is a struct that covers the data types needed for all serializer types,
|
||||
// and can be used to instantiate _any_ of the serializers.
|
||||
type Config struct {
|
||||
// Dataformat can be one of: influx, graphite
|
||||
// Dataformat can be one of: influx, graphite, or json
|
||||
DataFormat string
|
||||
|
||||
// Prefix to add to all measurements, only supports Graphite
|
||||
@@ -38,6 +39,9 @@ type Config struct {
|
||||
// Template for converting telegraf metrics into Graphite
|
||||
// only supports Graphite
|
||||
Template string
|
||||
|
||||
// Timestamp units to use for JSON formatted output
|
||||
TimestampUnits time.Duration
|
||||
}
|
||||
|
||||
// NewSerializer a Serializer interface based on the given config.
|
||||
@@ -50,15 +54,15 @@ func NewSerializer(config *Config) (Serializer, error) {
|
||||
case "graphite":
|
||||
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template)
|
||||
case "json":
|
||||
serializer, err = NewJsonSerializer()
|
||||
serializer, err = NewJsonSerializer(config.TimestampUnits)
|
||||
default:
|
||||
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
|
||||
}
|
||||
return serializer, err
|
||||
}
|
||||
|
||||
func NewJsonSerializer() (Serializer, error) {
|
||||
return &json.JsonSerializer{}, nil
|
||||
func NewJsonSerializer(timestampUnits time.Duration) (Serializer, error) {
|
||||
return &json.JsonSerializer{TimestampUnits: timestampUnits}, nil
|
||||
}
|
||||
|
||||
func NewInfluxSerializer() (Serializer, error) {
|
||||
|
||||
Reference in New Issue
Block a user