Skip to content

Commit

Permalink
fix(serializers.json): Append newline for batch-serialization (#14150)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8d1bf60)
  • Loading branch information
srebhan authored and powersj committed Oct 23, 2023
1 parent 4504069 commit 8f5aeeb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions plugins/serializers/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
if err != nil {
return []byte{}, err
}
serialized = append(serialized, '\n')

return serialized, nil
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/serializers/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestSerializeBatch(t *testing.T) {
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}]}`),
[]byte(`{"metrics":[{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0},{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"),
buf,
)
}
Expand All @@ -219,7 +219,7 @@ func TestSerializeBatchSkipInf(t *testing.T) {
require.NoError(t, s.Init())
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)
require.Equal(t, []byte(`{"metrics":[{"fields":{"time_idle":42},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
require.Equal(t, []byte(`{"metrics":[{"fields":{"time_idle":42},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"), buf)
}

func TestSerializeBatchSkipInfAllFields(t *testing.T) {
Expand All @@ -238,7 +238,7 @@ func TestSerializeBatchSkipInfAllFields(t *testing.T) {
require.NoError(t, s.Init())
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)
require.Equal(t, []byte(`{"metrics":[{"fields":{},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
require.Equal(t, []byte(`{"metrics":[{"fields":{},"name":"cpu","tags":{},"timestamp":0}]}`+"\n"), buf)
}

func TestSerializeTransformationNonBatch(t *testing.T) {
Expand Down

0 comments on commit 8f5aeeb

Please sign in to comment.