Skip to content

Commit

Permalink
serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
DStrand1 committed Jun 6, 2024
1 parent ed9905d commit 5b6106a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,6 @@ issues:
- path: plugins/outputs/*
linters:
- errcheck
- path: plugins/serializers/*
linters:
- errcheck

- path: _test\.go
text: "Potential hardcoded credentials" #gosec:G101
Expand Down
12 changes: 8 additions & 4 deletions plugins/serializers/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,8 @@ func TestCleanWithTagsSupport(t *testing.T) {
require.NoError(t, s.Init())

m := metric.New(tt.metricName, tt.tags, tt.fields, now)
actual, _ := s.Serialize(m)
actual, err := s.Serialize(m)
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
Expand Down Expand Up @@ -1149,7 +1150,8 @@ func TestCleanWithTagsSupportCompatibleSanitize(t *testing.T) {
require.NoError(t, s.Init())

m := metric.New(tt.metricName, tt.tags, tt.fields, now)
actual, _ := s.Serialize(m)
actual, err := s.Serialize(m)
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
Expand Down Expand Up @@ -1179,7 +1181,8 @@ func TestSerializeBatch(t *testing.T) {
require.NoError(t, s.Init())

m := metric.New(tt.metricName, tt.tags, tt.fields, now)
actual, _ := s.SerializeBatch([]telegraf.Metric{m, m})
actual, err := s.SerializeBatch([]telegraf.Metric{m, m})
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
Expand Down Expand Up @@ -1212,7 +1215,8 @@ func TestSerializeBatchWithTagsSupport(t *testing.T) {
require.NoError(t, s.Init())

m := metric.New(tt.metricName, tt.tags, tt.fields, now)
actual, _ := s.SerializeBatch([]telegraf.Metric{m, m})
actual, err := s.SerializeBatch([]telegraf.Metric{m, m})
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
Expand Down
45 changes: 30 additions & 15 deletions plugins/serializers/msgpack/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,57 +81,71 @@ func TestMsgPackTimeEdgeCases(t *testing.T) {

// Unix epoch. Begin of 4bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x0000000000000000
ts, _ := time.Parse(time.RFC3339, "1970-01-01T00:00:00Z")
bs, _ := hex.DecodeString("d6ff00000000")
ts, err := time.Parse(time.RFC3339, "1970-01-01T00:00:00Z")
require.NoError(t, err)
bs, err := hex.DecodeString("d6ff00000000")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// End of 4bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x00000000ffffffff
ts, _ = time.Parse(time.RFC3339, "2106-02-07T06:28:15Z")
bs, _ = hex.DecodeString("d6ffffffffff")
ts, err = time.Parse(time.RFC3339, "2106-02-07T06:28:15Z")
require.NoError(t, err)
bs, err = hex.DecodeString("d6ffffffffff")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// Begin of 8bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x0000000100000000
ts, _ = time.Parse(time.RFC3339, "2106-02-07T06:28:16Z")
bs, _ = hex.DecodeString("d7ff0000000100000000")
ts, err = time.Parse(time.RFC3339, "2106-02-07T06:28:16Z")
require.NoError(t, err)
bs, err = hex.DecodeString("d7ff0000000100000000")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// Just after Unix epoch. Non zero nanoseconds
// Nanoseconds: 0x00000001, Seconds: 0x0000000000000000
ts, _ = time.Parse(time.RFC3339Nano, "1970-01-01T00:00:00.000000001Z")
bs, _ = hex.DecodeString("d7ff0000000400000000")
ts, err = time.Parse(time.RFC3339Nano, "1970-01-01T00:00:00.000000001Z")
require.NoError(t, err)
bs, err = hex.DecodeString("d7ff0000000400000000")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// End of 8bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x00000003ffffffff
ts, _ = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:03.000000000Z")
bs, _ = hex.DecodeString("d7ff00000003ffffffff")
ts, err = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:03.000000000Z")
require.NoError(t, err)
bs, err = hex.DecodeString("d7ff00000003ffffffff")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// Begin of 12bytes date
// Nanoseconds: 0x00000000, Seconds: 0x0000000400000000
ts, _ = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:04.000000000Z")
bs, _ = hex.DecodeString("c70cff000000000000000400000000")
ts, err = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:04.000000000Z")
require.NoError(t, err)
bs, err = hex.DecodeString("c70cff000000000000000400000000")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// Zero value, 0001-01-01T00:00:00Z
// Nanoseconds: 0x00000000, Seconds: 0xfffffff1886e0900
ts = time.Time{}
bs, _ = hex.DecodeString("c70cff00000000fffffff1886e0900")
bs, err = hex.DecodeString("c70cff00000000fffffff1886e0900")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

// Max value
// Nanoseconds: 0x3b9ac9ff, Seconds: 0x7fffffffffffffff
ts = time.Unix(math.MaxInt64, 999_999_999).UTC()
bs, _ = hex.DecodeString("c70cff3b9ac9ff7fffffffffffffff")
bs, err = hex.DecodeString("c70cff3b9ac9ff7fffffffffffffff")
require.NoError(t, err)
times = append(times, ts)
expected = append(expected, bs)

Expand All @@ -141,7 +155,8 @@ func TestMsgPackTimeEdgeCases(t *testing.T) {
m := Metric{Time: t1}

buf = buf[:0]
buf, _ = m.MarshalMsg(buf)
buf, err = m.MarshalMsg(buf)
require.NoError(t, err)
require.Equal(t, expected[i], buf[12:len(buf)-14])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func BenchmarkRemoteWrite(b *testing.B) {
}
s := &Serializer{}
for n := 0; n < b.N; n++ {
//nolint:errcheck // Benchmarking so skip the error check to avoid the unnecessary operations
_, _ = s.SerializeBatch(batch)
}
}
Expand Down
15 changes: 9 additions & 6 deletions plugins/serializers/splunkmetric/splunkmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ type HECTimeSeries struct {
}

func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
return s.createObject(metric), nil
return s.createObject(metric)
}

func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
var serialized []byte

for _, metric := range metrics {
m := s.createObject(metric)
m, err := s.createObject(metric)
if err != nil {
return nil, err
}
if m != nil {
serialized = append(serialized, m...)
}
Expand Down Expand Up @@ -148,7 +151,7 @@ func (s *Serializer) createSingle(metric telegraf.Metric, dataGroup HECTimeSerie
return metricGroup, nil
}

func (s *Serializer) createObject(metric telegraf.Metric) (metricGroup []byte) {
func (s *Serializer) createObject(metric telegraf.Metric) (metricGroup []byte, err error) {
/* Splunk supports one metric json object, and does _not_ support an array of JSON objects.
** Splunk has the following required names for the metric store:
** metric_name: The name of the metric
Expand Down Expand Up @@ -179,13 +182,13 @@ func (s *Serializer) createObject(metric telegraf.Metric) (metricGroup []byte) {
commonTags.Time = float64(metric.Time().UnixNano()) / float64(1000000000)
switch s.MultiMetric {
case true:
metricGroup, _ = s.createMulti(metric, dataGroup, commonTags)
metricGroup, err = s.createMulti(metric, dataGroup, commonTags)
default:
metricGroup, _ = s.createSingle(metric, dataGroup, commonTags)
metricGroup, err = s.createSingle(metric, dataGroup, commonTags)
}

// Return the metric group regardless of if it's multimetric or single metric.
return metricGroup
return metricGroup, err
}

func verifyValue(v interface{}) (value interface{}, valid bool) {
Expand Down
12 changes: 8 additions & 4 deletions plugins/serializers/wavefront/wavefront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func TestSerializeMetricFloat(t *testing.T) {
m := metric.New("cpu", tags, fields, now)

s := &Serializer{}
buf, _ := s.Serialize(m)
buf, err := s.Serialize(m)
require.NoError(t, err)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")

expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 91.500000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
Expand All @@ -200,7 +201,8 @@ func TestSerializeMetricInt(t *testing.T) {
m := metric.New("cpu", tags, fields, now)

s := &Serializer{}
buf, _ := s.Serialize(m)
buf, err := s.Serialize(m)
require.NoError(t, err)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")

expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
Expand All @@ -219,7 +221,8 @@ func TestSerializeMetricBoolTrue(t *testing.T) {
m := metric.New("cpu", tags, fields, now)

s := &Serializer{}
buf, _ := s.Serialize(m)
buf, err := s.Serialize(m)
require.NoError(t, err)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")

expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 1.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
Expand All @@ -238,7 +241,8 @@ func TestSerializeMetricBoolFalse(t *testing.T) {
m := metric.New("cpu", tags, fields, now)

s := &Serializer{}
buf, _ := s.Serialize(m)
buf, err := s.Serialize(m)
require.NoError(t, err)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")

expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 0.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
Expand Down

0 comments on commit 5b6106a

Please sign in to comment.