Add SerializeBatch method to the Serializer interface (#4107)
This commit is contained in:
@@ -1,29 +1,26 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
ejson "encoding/json"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
)
|
||||
|
||||
type JsonSerializer struct {
|
||||
type serializer 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
|
||||
func NewSerializer(timestampUnits time.Duration) (*serializer, error) {
|
||||
s := &serializer{
|
||||
TimestampUnits: truncateDuration(timestampUnits),
|
||||
}
|
||||
m["tags"] = metric.Tags()
|
||||
m["fields"] = metric.Fields()
|
||||
m["name"] = metric.Name()
|
||||
m["timestamp"] = metric.Time().UnixNano() / units_nanoseconds
|
||||
serialized, err := ejson.Marshal(m)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||
m := s.createObject(metric)
|
||||
serialized, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
@@ -31,3 +28,46 @@ func (s *JsonSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||
|
||||
return serialized, nil
|
||||
}
|
||||
|
||||
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)
|
||||
m["tags"] = metric.Tags()
|
||||
m["fields"] = metric.Fields()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,19 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
)
|
||||
|
||||
func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func TestSerializeMetricFloat(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
@@ -21,7 +30,7 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := JsonSerializer{}
|
||||
s, _ := NewSerializer(0)
|
||||
var buf []byte
|
||||
buf, err = s.Serialize(m)
|
||||
assert.NoError(t, err)
|
||||
@@ -29,6 +38,63 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||
assert.Equal(t, string(expS), string(buf))
|
||||
}
|
||||
|
||||
func TestSerialize_TimestampUnits(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
timestampUnits time.Duration
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "default of 1s",
|
||||
timestampUnits: 0,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":1525478795}`,
|
||||
},
|
||||
{
|
||||
name: "1ns",
|
||||
timestampUnits: 1 * time.Nanosecond,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":1525478795123456789}`,
|
||||
},
|
||||
{
|
||||
name: "1ms",
|
||||
timestampUnits: 1 * time.Millisecond,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":1525478795123}`,
|
||||
},
|
||||
{
|
||||
name: "10ms",
|
||||
timestampUnits: 10 * time.Millisecond,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":152547879512}`,
|
||||
},
|
||||
{
|
||||
name: "15ms is reduced to 10ms",
|
||||
timestampUnits: 15 * time.Millisecond,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":152547879512}`,
|
||||
},
|
||||
{
|
||||
name: "65ms is reduced to 10ms",
|
||||
timestampUnits: 65 * time.Millisecond,
|
||||
expected: `{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":152547879512}`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m := MustMetric(
|
||||
metric.New(
|
||||
"cpu",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"value": 42.0,
|
||||
},
|
||||
time.Unix(1525478795, 123456789),
|
||||
),
|
||||
)
|
||||
s, _ := NewSerializer(tt.timestampUnits)
|
||||
actual, err := s.Serialize(m)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected+"\n", string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializeMetricInt(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
@@ -40,7 +106,7 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := JsonSerializer{}
|
||||
s, _ := NewSerializer(0)
|
||||
var buf []byte
|
||||
buf, err = s.Serialize(m)
|
||||
assert.NoError(t, err)
|
||||
@@ -60,7 +126,7 @@ func TestSerializeMetricString(t *testing.T) {
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := JsonSerializer{}
|
||||
s, _ := NewSerializer(0)
|
||||
var buf []byte
|
||||
buf, err = s.Serialize(m)
|
||||
assert.NoError(t, err)
|
||||
@@ -81,7 +147,7 @@ func TestSerializeMultiFields(t *testing.T) {
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := JsonSerializer{}
|
||||
s, _ := NewSerializer(0)
|
||||
var buf []byte
|
||||
buf, err = s.Serialize(m)
|
||||
assert.NoError(t, err)
|
||||
@@ -101,10 +167,29 @@ func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||
m, err := metric.New("My CPU", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := JsonSerializer{}
|
||||
s, _ := NewSerializer(0)
|
||||
buf, err := s.Serialize(m)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []byte(fmt.Sprintf(`{"fields":{"U,age=Idle":90},"name":"My CPU","tags":{"cpu tag":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
||||
assert.Equal(t, string(expS), string(buf))
|
||||
}
|
||||
|
||||
func TestSerializeBatch(t *testing.T) {
|
||||
m := MustMetric(
|
||||
metric.New(
|
||||
"cpu",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"value": 42.0,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
)
|
||||
|
||||
metrics := []telegraf.Metric{m, m}
|
||||
s, _ := NewSerializer(0)
|
||||
buf, err := s.SerializeBatch(metrics)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte(`{"metrics":[{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0},{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user