fix: Linter fixes for plugins/serializers/[a-z]* (#10181)
Co-authored-by: Pawel Zak <Pawel Zak>
This commit is contained in:
parent
7aa6b533bd
commit
59eeddb41e
|
|
@ -65,7 +65,7 @@ func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||||
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
||||||
var batch bytes.Buffer
|
var batch bytes.Buffer
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
batch.Write(s.createObject(metric))
|
batch.Write(s.createObject(metric)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
}
|
}
|
||||||
return batch.Bytes(), nil
|
return batch.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
@ -83,31 +83,27 @@ func (s *Serializer) createObject(metric telegraf.Metric) []byte {
|
||||||
|
|
||||||
switch metricsFormat {
|
switch metricsFormat {
|
||||||
case Carbon2FormatFieldSeparate:
|
case Carbon2FormatFieldSeparate:
|
||||||
m.WriteString(serializeMetricFieldSeparate(
|
m.WriteString(serializeMetricFieldSeparate(name, fieldName)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
name, fieldName,
|
|
||||||
))
|
|
||||||
|
|
||||||
case Carbon2FormatMetricIncludesField:
|
case Carbon2FormatMetricIncludesField:
|
||||||
m.WriteString(serializeMetricIncludeField(
|
m.WriteString(serializeMetricIncludeField(name, fieldName)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
name, fieldName,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tag := range metric.TagList() {
|
for _, tag := range metric.TagList() {
|
||||||
m.WriteString(strings.Replace(tag.Key, " ", "_", -1))
|
m.WriteString(strings.Replace(tag.Key, " ", "_", -1)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString("=")
|
m.WriteString("=") //nolint:revive // from buffer.go: "err is always nil"
|
||||||
value := tag.Value
|
value := tag.Value
|
||||||
if len(value) == 0 {
|
if len(value) == 0 {
|
||||||
value = "null"
|
value = "null"
|
||||||
}
|
}
|
||||||
m.WriteString(strings.Replace(value, " ", "_", -1))
|
m.WriteString(strings.Replace(value, " ", "_", -1)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString(" ")
|
m.WriteString(" ") //nolint:revive // from buffer.go: "err is always nil"
|
||||||
}
|
}
|
||||||
m.WriteString(" ")
|
m.WriteString(" ") //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString(formatValue(fieldValue))
|
m.WriteString(formatValue(fieldValue)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString(" ")
|
m.WriteString(" ") //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString(strconv.FormatInt(metric.Time().Unix(), 10))
|
m.WriteString(strconv.FormatInt(metric.Time().Unix(), 10)) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
m.WriteString("\n")
|
m.WriteString("\n") //nolint:revive // from buffer.go: "err is always nil"
|
||||||
}
|
}
|
||||||
return m.Bytes()
|
return m.Bytes()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
|
@ -44,7 +43,7 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +80,7 @@ func TestSerializeMetricWithEmptyStringTag(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +117,7 @@ func TestSerializeWithSpaces(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -155,7 +154,7 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -192,7 +191,7 @@ func TestSerializeMetricString(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -248,7 +247,7 @@ func TestSerializeMetricBool(t *testing.T) {
|
||||||
buf, err := s.Serialize(tc.metric)
|
buf, err := s.Serialize(tc.metric)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -291,7 +290,7 @@ metric=cpu_value 42 0
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -400,7 +399,7 @@ func TestSerializeMetricIsProperlySanitized(t *testing.T) {
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, tc.expected, string(buf))
|
require.Equal(t, tc.expected, string(buf))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
|
@ -55,9 +54,9 @@ func TestGraphiteTags(t *testing.T) {
|
||||||
tags2 := buildTags(m2.Tags())
|
tags2 := buildTags(m2.Tags())
|
||||||
tags3 := buildTags(m3.Tags())
|
tags3 := buildTags(m3.Tags())
|
||||||
|
|
||||||
assert.Equal(t, "192_168_0_1", tags1)
|
require.Equal(t, "192_168_0_1", tags1)
|
||||||
assert.Equal(t, "first.second.192_168_0_1", tags2)
|
require.Equal(t, "first.second.192_168_0_1", tags2)
|
||||||
assert.Equal(t, "first.second", tags3)
|
require.Equal(t, "first.second", tags3)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricNoHost(t *testing.T) {
|
func TestSerializeMetricNoHost(t *testing.T) {
|
||||||
|
|
@ -82,7 +81,7 @@ func TestSerializeMetricNoHost(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricNoHostWithTagSupport(t *testing.T) {
|
func TestSerializeMetricNoHostWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -110,7 +109,7 @@ func TestSerializeMetricNoHostWithTagSupport(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricHost(t *testing.T) {
|
func TestSerializeMetricHost(t *testing.T) {
|
||||||
|
|
@ -136,7 +135,7 @@ func TestSerializeMetricHost(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricHostWithMultipleTemplates(t *testing.T) {
|
func TestSerializeMetricHostWithMultipleTemplates(t *testing.T) {
|
||||||
|
|
@ -157,8 +156,8 @@ func TestSerializeMetricHostWithMultipleTemplates(t *testing.T) {
|
||||||
"cp* tags.measurement.host.field",
|
"cp* tags.measurement.host.field",
|
||||||
"new_cpu tags.host.measurement.field",
|
"new_cpu tags.host.measurement.field",
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, defaultTemplate, "")
|
require.Equal(t, defaultTemplate, "")
|
||||||
|
|
||||||
s := GraphiteSerializer{
|
s := GraphiteSerializer{
|
||||||
Templates: templates,
|
Templates: templates,
|
||||||
|
|
@ -170,7 +169,7 @@ func TestSerializeMetricHostWithMultipleTemplates(t *testing.T) {
|
||||||
buf = append(buf, buf2...)
|
buf = append(buf, buf2...)
|
||||||
|
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
||||||
|
|
@ -180,7 +179,7 @@ func TestSerializeMetricHostWithMultipleTemplates(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricHostWithMultipleTemplatesWithDefault(t *testing.T) {
|
func TestSerializeMetricHostWithMultipleTemplatesWithDefault(t *testing.T) {
|
||||||
|
|
@ -201,8 +200,8 @@ func TestSerializeMetricHostWithMultipleTemplatesWithDefault(t *testing.T) {
|
||||||
"cp* tags.measurement.host.field",
|
"cp* tags.measurement.host.field",
|
||||||
"tags.host.measurement.field",
|
"tags.host.measurement.field",
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, defaultTemplate, "tags.host.measurement.field")
|
require.Equal(t, defaultTemplate, "tags.host.measurement.field")
|
||||||
|
|
||||||
s := GraphiteSerializer{
|
s := GraphiteSerializer{
|
||||||
Templates: templates,
|
Templates: templates,
|
||||||
|
|
@ -215,7 +214,7 @@ func TestSerializeMetricHostWithMultipleTemplatesWithDefault(t *testing.T) {
|
||||||
buf = append(buf, buf2...)
|
buf = append(buf, buf2...)
|
||||||
|
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
||||||
|
|
@ -225,7 +224,7 @@ func TestSerializeMetricHostWithMultipleTemplatesWithDefault(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricHostWithTagSupport(t *testing.T) {
|
func TestSerializeMetricHostWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -254,7 +253,7 @@ func TestSerializeMetricHostWithTagSupport(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that a field named "value" gets ignored.
|
// test that a field named "value" gets ignored.
|
||||||
|
|
@ -277,7 +276,7 @@ func TestSerializeValueField(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueFieldWithTagSupport(t *testing.T) {
|
func TestSerializeValueFieldWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -302,7 +301,7 @@ func TestSerializeValueFieldWithTagSupport(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that a field named "value" gets ignored in middle of template.
|
// test that a field named "value" gets ignored in middle of template.
|
||||||
|
|
@ -327,7 +326,7 @@ func TestSerializeValueField2(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueString(t *testing.T) {
|
func TestSerializeValueString(t *testing.T) {
|
||||||
|
|
@ -347,7 +346,7 @@ func TestSerializeValueString(t *testing.T) {
|
||||||
}
|
}
|
||||||
buf, _ := s.Serialize(m)
|
buf, _ := s.Serialize(m)
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
assert.Equal(t, "", mS[0])
|
require.Equal(t, "", mS[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueStringWithTagSupport(t *testing.T) {
|
func TestSerializeValueStringWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -368,7 +367,7 @@ func TestSerializeValueStringWithTagSupport(t *testing.T) {
|
||||||
}
|
}
|
||||||
buf, _ := s.Serialize(m)
|
buf, _ := s.Serialize(m)
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
assert.Equal(t, "", mS[0])
|
require.Equal(t, "", mS[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueBoolean(t *testing.T) {
|
func TestSerializeValueBoolean(t *testing.T) {
|
||||||
|
|
@ -396,7 +395,7 @@ func TestSerializeValueBoolean(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueBooleanWithTagSupport(t *testing.T) {
|
func TestSerializeValueBooleanWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -425,7 +424,7 @@ func TestSerializeValueBooleanWithTagSupport(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeValueUnsigned(t *testing.T) {
|
func TestSerializeValueUnsigned(t *testing.T) {
|
||||||
|
|
@ -465,7 +464,7 @@ func TestSerializeFieldWithSpaces(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.cpu0.us-west-2.cpu.field_with_spaces 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.cpu0.us-west-2.cpu.field_with_spaces 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeFieldWithSpacesWithTagSupport(t *testing.T) {
|
func TestSerializeFieldWithSpacesWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -490,7 +489,7 @@ func TestSerializeFieldWithSpacesWithTagSupport(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that tags with spaces get fixed.
|
// test that tags with spaces get fixed.
|
||||||
|
|
@ -515,7 +514,7 @@ func TestSerializeTagWithSpaces(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.cpu_0.us-west-2.cpu.field_with_spaces 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.cpu_0.us-west-2.cpu.field_with_spaces 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeTagWithSpacesWithTagSupport(t *testing.T) {
|
func TestSerializeTagWithSpacesWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -540,7 +539,7 @@ func TestSerializeTagWithSpacesWithTagSupport(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu_0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu_0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeTagWithSpacesWithTagSupportCompatibleSanitize(t *testing.T) {
|
func TestSerializeTagWithSpacesWithTagSupportCompatibleSanitize(t *testing.T) {
|
||||||
|
|
@ -566,7 +565,7 @@ func TestSerializeTagWithSpacesWithTagSupportCompatibleSanitize(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu\\ 0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu\\ 0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that a field named "value" gets ignored at beginning of template.
|
// test that a field named "value" gets ignored at beginning of template.
|
||||||
|
|
@ -591,7 +590,7 @@ func TestSerializeValueField3(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.cpu0.us-west-2.cpu 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that a field named "value" gets ignored at beginning of template.
|
// test that a field named "value" gets ignored at beginning of template.
|
||||||
|
|
@ -616,7 +615,7 @@ func TestSerializeValueField5(t *testing.T) {
|
||||||
expS := []string{
|
expS := []string{
|
||||||
fmt.Sprintf("localhost.us-west-2.cpu0.cpu 91.5 %d", now.Unix()),
|
fmt.Sprintf("localhost.us-west-2.cpu0.cpu 91.5 %d", now.Unix()),
|
||||||
}
|
}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricPrefix(t *testing.T) {
|
func TestSerializeMetricPrefix(t *testing.T) {
|
||||||
|
|
@ -642,7 +641,7 @@ func TestSerializeMetricPrefix(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricPrefixWithTagSupport(t *testing.T) {
|
func TestSerializeMetricPrefixWithTagSupport(t *testing.T) {
|
||||||
|
|
@ -672,7 +671,7 @@ func TestSerializeMetricPrefixWithTagSupport(t *testing.T) {
|
||||||
}
|
}
|
||||||
sort.Strings(mS)
|
sort.Strings(mS)
|
||||||
sort.Strings(expS)
|
sort.Strings(expS)
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBucketNameNoHost(t *testing.T) {
|
func TestSerializeBucketNameNoHost(t *testing.T) {
|
||||||
|
|
@ -689,7 +688,7 @@ func TestSerializeBucketNameNoHost(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
||||||
|
|
||||||
expS := "cpu0.us-west-2.cpu.FIELDNAME"
|
expS := "cpu0.us-west-2.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBucketNameHost(t *testing.T) {
|
func TestSerializeBucketNameHost(t *testing.T) {
|
||||||
|
|
@ -702,7 +701,7 @@ func TestSerializeBucketNameHost(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
||||||
|
|
||||||
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBucketNamePrefix(t *testing.T) {
|
func TestSerializeBucketNamePrefix(t *testing.T) {
|
||||||
|
|
@ -715,7 +714,7 @@ func TestSerializeBucketNamePrefix(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "prefix")
|
mS := SerializeBucketName(m.Name(), m.Tags(), "", "prefix")
|
||||||
|
|
||||||
expS := "prefix.localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
expS := "prefix.localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplate1(t *testing.T) {
|
func TestTemplate1(t *testing.T) {
|
||||||
|
|
@ -728,7 +727,7 @@ func TestTemplate1(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), template1, "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), template1, "")
|
||||||
|
|
||||||
expS := "cpu0.us-west-2.localhost.cpu.FIELDNAME"
|
expS := "cpu0.us-west-2.localhost.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplate2(t *testing.T) {
|
func TestTemplate2(t *testing.T) {
|
||||||
|
|
@ -741,7 +740,7 @@ func TestTemplate2(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), template2, "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), template2, "")
|
||||||
|
|
||||||
expS := "localhost.cpu.FIELDNAME"
|
expS := "localhost.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplate3(t *testing.T) {
|
func TestTemplate3(t *testing.T) {
|
||||||
|
|
@ -754,7 +753,7 @@ func TestTemplate3(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), template3, "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), template3, "")
|
||||||
|
|
||||||
expS := "localhost.cpu0.us-west-2.FIELDNAME"
|
expS := "localhost.cpu0.us-west-2.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplate4(t *testing.T) {
|
func TestTemplate4(t *testing.T) {
|
||||||
|
|
@ -767,7 +766,7 @@ func TestTemplate4(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), template4, "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), template4, "")
|
||||||
|
|
||||||
expS := "localhost.cpu0.us-west-2.cpu"
|
expS := "localhost.cpu0.us-west-2.cpu"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTemplate6(t *testing.T) {
|
func TestTemplate6(t *testing.T) {
|
||||||
|
|
@ -780,7 +779,7 @@ func TestTemplate6(t *testing.T) {
|
||||||
mS := SerializeBucketName(m.Name(), m.Tags(), template6, "")
|
mS := SerializeBucketName(m.Name(), m.Tags(), template6, "")
|
||||||
|
|
||||||
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClean(t *testing.T) {
|
func TestClean(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -80,8 +80,8 @@ func NewSerializer() *Serializer {
|
||||||
return serializer
|
return serializer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) SetMaxLineBytes(bytes int) {
|
func (s *Serializer) SetMaxLineBytes(maxLineBytes int) {
|
||||||
s.maxLineBytes = bytes
|
s.maxLineBytes = maxLineBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) SetFieldSortOrder(order FieldSortOrder) {
|
func (s *Serializer) SetFieldSortOrder(order FieldSortOrder) {
|
||||||
|
|
@ -135,7 +135,7 @@ func (s *Serializer) writeString(w io.Writer, str string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) write(w io.Writer, b []byte) error {
|
func (s *Serializer) writeBytes(w io.Writer, b []byte) error {
|
||||||
n, err := w.Write(b)
|
n, err := w.Write(b)
|
||||||
s.bytesWritten += n
|
s.bytesWritten += n
|
||||||
return err
|
return err
|
||||||
|
|
@ -247,7 +247,7 @@ func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
|
||||||
return s.newMetricError(NeedMoreSpace)
|
return s.newMetricError(NeedMoreSpace)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.write(w, s.footer)
|
err = s.writeBytes(w, s.footer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -262,7 +262,7 @@ func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if firstField {
|
if firstField {
|
||||||
err = s.write(w, s.header)
|
err = s.writeBytes(w, s.header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -273,7 +273,7 @@ func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.write(w, s.pair)
|
err = s.writeBytes(w, s.pair)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -286,7 +286,7 @@ func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
|
||||||
return s.newMetricError(NoFields)
|
return s.newMetricError(NoFields)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.write(w, s.footer)
|
return s.writeBytes(w, s.footer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) newMetricError(reason string) *MetricError {
|
func (s *Serializer) newMetricError(reason string) *MetricError {
|
||||||
|
|
|
||||||
|
|
@ -61,8 +61,7 @@ func (s *Serializer) createObject(metric telegraf.Metric) map[string]interface{}
|
||||||
|
|
||||||
fields := make(map[string]interface{}, len(metric.FieldList()))
|
fields := make(map[string]interface{}, len(metric.FieldList()))
|
||||||
for _, field := range metric.FieldList() {
|
for _, field := range metric.FieldList() {
|
||||||
switch fv := field.Value.(type) {
|
if fv, ok := field.Value.(float64); ok {
|
||||||
case float64:
|
|
||||||
// JSON does not support these special values
|
// JSON does not support these special values
|
||||||
if math.IsNaN(fv) || math.IsInf(fv, 0) {
|
if math.IsNaN(fv) || math.IsInf(fv, 0) {
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,13 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSerializeMetricFloat(t *testing.T) {
|
func TestSerializeMetricFloat(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
|
|
@ -33,9 +26,9 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||||
s, _ := NewSerializer(0, "")
|
s, _ := NewSerializer(0, "")
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":91.5},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":91.5},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerialize_TimestampUnits(t *testing.T) {
|
func TestSerialize_TimestampUnits(t *testing.T) {
|
||||||
|
|
@ -112,10 +105,10 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
s, _ := NewSerializer(0, "")
|
s, _ := NewSerializer(0, "")
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricString(t *testing.T) {
|
func TestSerializeMetricString(t *testing.T) {
|
||||||
|
|
@ -131,10 +124,10 @@ func TestSerializeMetricString(t *testing.T) {
|
||||||
s, _ := NewSerializer(0, "")
|
s, _ := NewSerializer(0, "")
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":"foobar"},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":"foobar"},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMultiFields(t *testing.T) {
|
func TestSerializeMultiFields(t *testing.T) {
|
||||||
|
|
@ -151,10 +144,10 @@ func TestSerializeMultiFields(t *testing.T) {
|
||||||
s, _ := NewSerializer(0, "")
|
s, _ := NewSerializer(0, "")
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90,"usage_total":8559615},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90,"usage_total":8559615},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricWithEscapes(t *testing.T) {
|
func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||||
|
|
@ -169,10 +162,10 @@ func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||||
|
|
||||||
s, _ := NewSerializer(0, "")
|
s, _ := NewSerializer(0, "")
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`{"fields":{"U,age=Idle":90},"name":"My CPU","tags":{"cpu tag":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
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))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBatch(t *testing.T) {
|
func TestSerializeBatch(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,9 @@ func (*MessagePackTime) ExtensionType() int8 {
|
||||||
// 32bits: [1970-01-01 00:00:00 UTC, 2106-02-07 06:28:16 UTC) range. If the nanoseconds part is 0
|
// 32bits: [1970-01-01 00:00:00 UTC, 2106-02-07 06:28:16 UTC) range. If the nanoseconds part is 0
|
||||||
// 64bits: [1970-01-01 00:00:00.000000000 UTC, 2514-05-30 01:53:04.000000000 UTC) range.
|
// 64bits: [1970-01-01 00:00:00.000000000 UTC, 2514-05-30 01:53:04.000000000 UTC) range.
|
||||||
// 96bits: [-584554047284-02-23 16:59:44 UTC, 584554051223-11-09 07:00:16.000000000 UTC) range.
|
// 96bits: [-584554047284-02-23 16:59:44 UTC, 584554051223-11-09 07:00:16.000000000 UTC) range.
|
||||||
func (t *MessagePackTime) Len() int {
|
func (z *MessagePackTime) Len() int {
|
||||||
sec := t.time.Unix()
|
sec := z.time.Unix()
|
||||||
nsec := t.time.Nanosecond()
|
nsec := z.time.Nanosecond()
|
||||||
|
|
||||||
if sec < 0 || sec >= (1<<34) { // 96 bits encoding
|
if sec < 0 || sec >= (1<<34) { // 96 bits encoding
|
||||||
return 12
|
return 12
|
||||||
|
|
@ -56,21 +56,21 @@ func (t *MessagePackTime) Len() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalBinaryTo implements the Extension interface
|
// MarshalBinaryTo implements the Extension interface
|
||||||
func (t *MessagePackTime) MarshalBinaryTo(buf []byte) error {
|
func (z *MessagePackTime) MarshalBinaryTo(buf []byte) error {
|
||||||
len := t.Len()
|
length := z.Len()
|
||||||
|
|
||||||
if len == 4 {
|
if length == 4 {
|
||||||
sec := t.time.Unix()
|
sec := z.time.Unix()
|
||||||
binary.BigEndian.PutUint32(buf, uint32(sec))
|
binary.BigEndian.PutUint32(buf, uint32(sec))
|
||||||
} else if len == 8 {
|
} else if length == 8 {
|
||||||
sec := t.time.Unix()
|
sec := z.time.Unix()
|
||||||
nsec := t.time.Nanosecond()
|
nsec := z.time.Nanosecond()
|
||||||
|
|
||||||
data := uint64(nsec)<<34 | (uint64(sec) & 0x03_ffff_ffff)
|
data := uint64(nsec)<<34 | (uint64(sec) & 0x03_ffff_ffff)
|
||||||
binary.BigEndian.PutUint64(buf, data)
|
binary.BigEndian.PutUint64(buf, data)
|
||||||
} else if len == 12 {
|
} else if length == 12 {
|
||||||
sec := t.time.Unix()
|
sec := z.time.Unix()
|
||||||
nsec := t.time.Nanosecond()
|
nsec := z.time.Nanosecond()
|
||||||
|
|
||||||
binary.BigEndian.PutUint32(buf, uint32(nsec))
|
binary.BigEndian.PutUint32(buf, uint32(nsec))
|
||||||
binary.BigEndian.PutUint64(buf[4:], uint64(sec))
|
binary.BigEndian.PutUint64(buf[4:], uint64(sec))
|
||||||
|
|
@ -80,24 +80,24 @@ func (t *MessagePackTime) MarshalBinaryTo(buf []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalBinary implements the Extension interface
|
// UnmarshalBinary implements the Extension interface
|
||||||
func (t *MessagePackTime) UnmarshalBinary(buf []byte) error {
|
func (z *MessagePackTime) UnmarshalBinary(buf []byte) error {
|
||||||
len := len(buf)
|
length := len(buf)
|
||||||
|
|
||||||
if len == 4 {
|
if length == 4 {
|
||||||
sec := binary.BigEndian.Uint32(buf)
|
sec := binary.BigEndian.Uint32(buf)
|
||||||
t.time = time.Unix(int64(sec), 0)
|
z.time = time.Unix(int64(sec), 0)
|
||||||
} else if len == 8 {
|
} else if length == 8 {
|
||||||
data := binary.BigEndian.Uint64(buf)
|
data := binary.BigEndian.Uint64(buf)
|
||||||
|
|
||||||
nsec := (data & 0xfffffffc_00000000) >> 34
|
nsec := (data & 0xfffffffc_00000000) >> 34
|
||||||
sec := (data & 0x00000003_ffffffff)
|
sec := data & 0x00000003_ffffffff
|
||||||
|
|
||||||
t.time = time.Unix(int64(sec), int64(nsec))
|
z.time = time.Unix(int64(sec), int64(nsec))
|
||||||
} else if len == 12 {
|
} else if length == 12 {
|
||||||
nsec := binary.BigEndian.Uint32(buf)
|
nsec := binary.BigEndian.Uint32(buf)
|
||||||
sec := binary.BigEndian.Uint64(buf[4:])
|
sec := binary.BigEndian.Uint64(buf[4:])
|
||||||
|
|
||||||
t.time = time.Unix(int64(sec), int64(nsec))
|
z.time = time.Unix(int64(sec), int64(nsec))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,25 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMsgPackTime32(t *testing.T) {
|
func TestMsgPackTime32(t *testing.T) {
|
||||||
// Maximum of 4 bytes encodable time
|
// Maximum of 4 bytes encodable time
|
||||||
var sec int64 = 0xFFFFFFFF
|
var sec int64 = 0xFFFFFFFF
|
||||||
var nsec int64 = 0
|
var nsec int64
|
||||||
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
||||||
|
|
||||||
assert.Equal(t, t1.Len(), 4)
|
require.Equal(t, t1.Len(), 4)
|
||||||
|
|
||||||
buf := make([]byte, t1.Len())
|
buf := make([]byte, t1.Len())
|
||||||
assert.NoError(t, t1.MarshalBinaryTo(buf))
|
require.NoError(t, t1.MarshalBinaryTo(buf))
|
||||||
|
|
||||||
t2 := new(MessagePackTime)
|
t2 := new(MessagePackTime)
|
||||||
t2.UnmarshalBinary(buf)
|
err := t2.UnmarshalBinary(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, t1.time, t2.time)
|
require.Equal(t, t1.time, t2.time)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMsgPackTime64(t *testing.T) {
|
func TestMsgPackTime64(t *testing.T) {
|
||||||
|
|
@ -32,15 +33,16 @@ func TestMsgPackTime64(t *testing.T) {
|
||||||
var nsec int64 = 999999999
|
var nsec int64 = 999999999
|
||||||
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
||||||
|
|
||||||
assert.Equal(t, t1.Len(), 8)
|
require.Equal(t, t1.Len(), 8)
|
||||||
|
|
||||||
buf := make([]byte, t1.Len())
|
buf := make([]byte, t1.Len())
|
||||||
assert.NoError(t, t1.MarshalBinaryTo(buf))
|
require.NoError(t, t1.MarshalBinaryTo(buf))
|
||||||
|
|
||||||
t2 := new(MessagePackTime)
|
t2 := new(MessagePackTime)
|
||||||
t2.UnmarshalBinary(buf)
|
err := t2.UnmarshalBinary(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, t1.time, t2.time)
|
require.Equal(t, t1.time, t2.time)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMsgPackTime96(t *testing.T) {
|
func TestMsgPackTime96(t *testing.T) {
|
||||||
|
|
@ -49,26 +51,28 @@ func TestMsgPackTime96(t *testing.T) {
|
||||||
var nsec int64 = 111111111
|
var nsec int64 = 111111111
|
||||||
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
|
||||||
|
|
||||||
assert.Equal(t, t1.Len(), 12)
|
require.Equal(t, t1.Len(), 12)
|
||||||
|
|
||||||
buf := make([]byte, t1.Len())
|
buf := make([]byte, t1.Len())
|
||||||
assert.NoError(t, t1.MarshalBinaryTo(buf))
|
require.NoError(t, t1.MarshalBinaryTo(buf))
|
||||||
|
|
||||||
t2 := new(MessagePackTime)
|
t2 := new(MessagePackTime)
|
||||||
t2.UnmarshalBinary(buf)
|
err := t2.UnmarshalBinary(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.True(t, t1.time.Equal(t2.time))
|
require.True(t, t1.time.Equal(t2.time))
|
||||||
|
|
||||||
// Testing the default value: 0001-01-01T00:00:00Z
|
// Testing the default value: 0001-01-01T00:00:00Z
|
||||||
t1 = MessagePackTime{}
|
t1 = MessagePackTime{}
|
||||||
|
|
||||||
assert.Equal(t, t1.Len(), 12)
|
require.Equal(t, t1.Len(), 12)
|
||||||
assert.NoError(t, t1.MarshalBinaryTo(buf))
|
require.NoError(t, t1.MarshalBinaryTo(buf))
|
||||||
|
|
||||||
t2 = new(MessagePackTime)
|
t2 = new(MessagePackTime)
|
||||||
t2.UnmarshalBinary(buf)
|
err = t2.UnmarshalBinary(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.True(t, t1.time.Equal(t2.time))
|
require.True(t, t1.time.Equal(t2.time))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMsgPackTimeEdgeCases(t *testing.T) {
|
func TestMsgPackTimeEdgeCases(t *testing.T) {
|
||||||
|
|
@ -138,6 +142,6 @@ func TestMsgPackTimeEdgeCases(t *testing.T) {
|
||||||
|
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
buf, _ = m.MarshalMsg(buf)
|
buf, _ = m.MarshalMsg(buf)
|
||||||
assert.Equal(t, expected[i], buf[12:len(buf)-14])
|
require.Equal(t, expected[i], buf[12:len(buf)-14])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,11 @@ package msgpack
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func toTelegrafMetric(m Metric) telegraf.Metric {
|
func toTelegrafMetric(m Metric) telegraf.Metric {
|
||||||
|
|
@ -20,13 +21,13 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
m2 := &Metric{}
|
m2 := &Metric{}
|
||||||
left, err := m2.UnmarshalMsg(buf)
|
left, err := m2.UnmarshalMsg(buf)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, len(left), 0)
|
require.Equal(t, len(left), 0)
|
||||||
|
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
||||||
}
|
}
|
||||||
|
|
@ -37,61 +38,61 @@ func TestSerializeMetricString(t *testing.T) {
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
m2 := &Metric{}
|
m2 := &Metric{}
|
||||||
left, err := m2.UnmarshalMsg(buf)
|
left, err := m2.UnmarshalMsg(buf)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, len(left), 0)
|
require.Equal(t, len(left), 0)
|
||||||
|
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMultiFields(t *testing.T) {
|
func TestSerializeMultiFields(t *testing.T) {
|
||||||
m := testutil.TestMetric(int(90))
|
m := testutil.TestMetric(90)
|
||||||
m.AddField("value2", 8559615)
|
m.AddField("value2", 8559615)
|
||||||
|
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
m2 := &Metric{}
|
m2 := &Metric{}
|
||||||
left, err := m2.UnmarshalMsg(buf)
|
left, err := m2.UnmarshalMsg(buf)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, len(left), 0)
|
require.Equal(t, len(left), 0)
|
||||||
|
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricWithEscapes(t *testing.T) {
|
func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||||
m := testutil.TestMetric(int(90))
|
m := testutil.TestMetric(90)
|
||||||
m.AddField("U,age=Idle", int64(90))
|
m.AddField("U,age=Idle", int64(90))
|
||||||
m.AddTag("cpu tag", "cpu0")
|
m.AddTag("cpu tag", "cpu0")
|
||||||
|
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
m2 := &Metric{}
|
m2 := &Metric{}
|
||||||
left, err := m2.UnmarshalMsg(buf)
|
left, err := m2.UnmarshalMsg(buf)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, len(left), 0)
|
require.Equal(t, len(left), 0)
|
||||||
|
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMultipleMetric(t *testing.T) {
|
func TestSerializeMultipleMetric(t *testing.T) {
|
||||||
m := testutil.TestMetric(int(90))
|
m := testutil.TestMetric(90)
|
||||||
|
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
|
|
||||||
encoded, err := s.Serialize(m)
|
encoded, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Multiple metrics in continous bytes stream
|
// Multiple metrics in continous bytes stream
|
||||||
var buf []byte
|
var buf []byte
|
||||||
|
|
@ -105,27 +106,27 @@ func TestSerializeMultipleMetric(t *testing.T) {
|
||||||
decodeM := &Metric{}
|
decodeM := &Metric{}
|
||||||
left, err = decodeM.UnmarshalMsg(left)
|
left, err = decodeM.UnmarshalMsg(left)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBatch(t *testing.T) {
|
func TestSerializeBatch(t *testing.T) {
|
||||||
m := testutil.TestMetric(int(90))
|
m := testutil.TestMetric(90)
|
||||||
|
|
||||||
metrics := []telegraf.Metric{m, m, m, m}
|
metrics := []telegraf.Metric{m, m, m, m}
|
||||||
|
|
||||||
s := Serializer{}
|
s := Serializer{}
|
||||||
|
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
left := buf
|
left := buf
|
||||||
for len(left) > 0 {
|
for len(left) > 0 {
|
||||||
decodeM := &Metric{}
|
decodeM := &Metric{}
|
||||||
left, err = decodeM.UnmarshalMsg(left)
|
left, err = decodeM.UnmarshalMsg(left)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
|
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,9 @@ func NewSerializer() (*serializer, error) {
|
||||||
func (s *serializer) Serialize(metric telegraf.Metric) (out []byte, err error) {
|
func (s *serializer) Serialize(metric telegraf.Metric) (out []byte, err error) {
|
||||||
serialized, err := s.createObject(metric)
|
serialized, err := s.createObject(metric)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, nil
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
return serialized, err
|
return serialized, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serializer) SerializeBatch(metrics []telegraf.Metric) (out []byte, err error) {
|
func (s *serializer) SerializeBatch(metrics []telegraf.Metric) (out []byte, err error) {
|
||||||
|
|
@ -56,7 +56,7 @@ func (s *serializer) SerializeBatch(metrics []telegraf.Metric) (out []byte, err
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
m, err := s.createObject(metric)
|
m, err := s.createObject(metric)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("D! [serializer.nowmetric] Dropping invalid metric: %s", metric.Name())
|
return nil, fmt.Errorf("dropping invalid metric: %s", metric.Name())
|
||||||
} else if m != nil {
|
} else if m != nil {
|
||||||
objects = append(objects, m...)
|
objects = append(objects, m...)
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +97,7 @@ func (s *serializer) createObject(metric telegraf.Metric) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format timestamp to UNIX epoch
|
// Format timestamp to UNIX epoch
|
||||||
oimetric.Timestamp = (metric.Time().UnixNano() / int64(time.Millisecond))
|
oimetric.Timestamp = metric.Time().UnixNano() / int64(time.Millisecond)
|
||||||
|
|
||||||
// Loop of fields value pair and build datapoint for each of them
|
// Loop of fields value pair and build datapoint for each of them
|
||||||
for _, field := range metric.FieldList() {
|
for _, field := range metric.FieldList() {
|
||||||
|
|
@ -129,9 +129,6 @@ func (s *serializer) createObject(metric telegraf.Metric) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyValue(v interface{}) bool {
|
func verifyValue(v interface{}) bool {
|
||||||
switch v.(type) {
|
_, ok := v.(string)
|
||||||
case string:
|
return !ok
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,12 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSerializeMetricFloat(t *testing.T) {
|
func TestSerializeMetricFloat(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
|
|
@ -32,9 +25,9 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||||
s, _ := NewSerializer()
|
s, _ := NewSerializer()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":91.5,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, (now.UnixNano() / int64(time.Millisecond))))
|
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":91.5,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, now.UnixNano()/int64(time.Millisecond)))
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerialize_TimestampUnits(t *testing.T) {
|
func TestSerialize_TimestampUnits(t *testing.T) {
|
||||||
|
|
@ -95,10 +88,10 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
s, _ := NewSerializer()
|
s, _ := NewSerializer()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, (now.UnixNano() / int64(time.Millisecond))))
|
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, now.UnixNano()/int64(time.Millisecond)))
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricString(t *testing.T) {
|
func TestSerializeMetricString(t *testing.T) {
|
||||||
|
|
@ -114,9 +107,9 @@ func TestSerializeMetricString(t *testing.T) {
|
||||||
s, _ := NewSerializer()
|
s, _ := NewSerializer()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "null", string(buf))
|
require.Equal(t, "null", string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMultiFields(t *testing.T) {
|
func TestSerializeMultiFields(t *testing.T) {
|
||||||
|
|
@ -138,10 +131,10 @@ func TestSerializeMultiFields(t *testing.T) {
|
||||||
s, _ := NewSerializer()
|
s, _ := NewSerializer()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"},{"metric_type":"usage_total","resource":"","node":"","value":8559615,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, (now.UnixNano() / int64(time.Millisecond)), (now.UnixNano() / int64(time.Millisecond))))
|
expS := []byte(fmt.Sprintf(`[{"metric_type":"usage_idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"},{"metric_type":"usage_total","resource":"","node":"","value":8559615,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, now.UnixNano()/int64(time.Millisecond), now.UnixNano()/int64(time.Millisecond)))
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricWithEscapes(t *testing.T) {
|
func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||||
|
|
@ -156,10 +149,10 @@ func TestSerializeMetricWithEscapes(t *testing.T) {
|
||||||
|
|
||||||
s, _ := NewSerializer()
|
s, _ := NewSerializer()
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := []byte(fmt.Sprintf(`[{"metric_type":"U,age=Idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, (now.UnixNano() / int64(time.Millisecond))))
|
expS := []byte(fmt.Sprintf(`[{"metric_type":"U,age=Idle","resource":"","node":"","value":90,"timestamp":%d,"ci2metric_id":null,"source":"Telegraf"}]`, now.UnixNano()/int64(time.Millisecond)))
|
||||||
assert.Equal(t, string(expS), string(buf))
|
require.Equal(t, string(expS), string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBatch(t *testing.T) {
|
func TestSerializeBatch(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf"
|
||||||
)
|
)
|
||||||
|
|
||||||
const helpString = "Telegraf collected metric"
|
const helpString = "Telegraf collected metric"
|
||||||
|
|
@ -86,10 +87,10 @@ type MetricKey uint64
|
||||||
func MakeMetricKey(labels []LabelPair) MetricKey {
|
func MakeMetricKey(labels []LabelPair) MetricKey {
|
||||||
h := fnv.New64a()
|
h := fnv.New64a()
|
||||||
for _, label := range labels {
|
for _, label := range labels {
|
||||||
h.Write([]byte(label.Name))
|
h.Write([]byte(label.Name)) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte("\x00"))
|
h.Write([]byte("\x00")) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte(label.Value))
|
h.Write([]byte(label.Value)) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte("\x00"))
|
h.Write([]byte("\x00")) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
}
|
}
|
||||||
return MetricKey(h.Sum64())
|
return MetricKey(h.Sum64())
|
||||||
}
|
}
|
||||||
|
|
@ -357,8 +358,7 @@ func (c *Collection) GetEntries(order MetricSortOrder) []Entry {
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch order {
|
if order == SortMetrics {
|
||||||
case SortMetrics:
|
|
||||||
sort.Slice(entries, func(i, j int) bool {
|
sort.Slice(entries, func(i, j int) bool {
|
||||||
lhs := entries[i].Family
|
lhs := entries[i].Family
|
||||||
rhs := entries[j].Family
|
rhs := entries[j].Family
|
||||||
|
|
@ -378,8 +378,7 @@ func (c *Collection) GetMetrics(entry Entry, order MetricSortOrder) []*Metric {
|
||||||
metrics = append(metrics, metric)
|
metrics = append(metrics, metric)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch order {
|
if order == SortMetrics {
|
||||||
case SortMetrics:
|
|
||||||
sort.Slice(metrics, func(i, j int) bool {
|
sort.Slice(metrics, func(i, j int) bool {
|
||||||
lhs := metrics[i].Labels
|
lhs := metrics[i].Labels
|
||||||
rhs := metrics[j].Labels
|
rhs := metrics[j].Labels
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Table struct {
|
type Table struct {
|
||||||
|
|
@ -89,13 +90,13 @@ func sanitize(name string, table Table) (string, bool) {
|
||||||
switch {
|
switch {
|
||||||
case i == 0:
|
case i == 0:
|
||||||
if unicode.In(r, table.First) {
|
if unicode.In(r, table.First) {
|
||||||
b.WriteRune(r)
|
b.WriteRune(r) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if unicode.In(r, table.Rest) {
|
if unicode.In(r, table.Rest) {
|
||||||
b.WriteRune(r)
|
b.WriteRune(r) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
|
||||||
} else {
|
} else {
|
||||||
b.WriteString("_")
|
b.WriteString("_") //nolint:revive // from builder.go: "It returns the length of s and a nil error."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/snappy"
|
"github.com/golang/snappy"
|
||||||
"github.com/influxdata/telegraf/plugins/serializers/prometheus"
|
"github.com/prometheus/prometheus/prompb"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/prometheus/prometheus/prompb"
|
"github.com/influxdata/telegraf/plugins/serializers/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MetricKey uint64
|
type MetricKey uint64
|
||||||
|
|
@ -210,8 +210,7 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
switch s.config.MetricSortOrder {
|
if s.config.MetricSortOrder == SortMetrics {
|
||||||
case SortMetrics:
|
|
||||||
sort.Slice(promTS, func(i, j int) bool {
|
sort.Slice(promTS, func(i, j int) bool {
|
||||||
lhs := promTS[i].Labels
|
lhs := promTS[i].Labels
|
||||||
rhs := promTS[j].Labels
|
rhs := promTS[j].Labels
|
||||||
|
|
@ -241,7 +240,7 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
||||||
return nil, fmt.Errorf("unable to marshal protobuf: %v", err)
|
return nil, fmt.Errorf("unable to marshal protobuf: %v", err)
|
||||||
}
|
}
|
||||||
encoded := snappy.Encode(nil, data)
|
encoded := snappy.Encode(nil, data)
|
||||||
buf.Write(encoded)
|
buf.Write(encoded) //nolint:revive // from buffer.go: "err is always nil"
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,10 +319,10 @@ func (s *Serializer) createLabels(metric telegraf.Metric) []prompb.Label {
|
||||||
func MakeMetricKey(labels []prompb.Label) MetricKey {
|
func MakeMetricKey(labels []prompb.Label) MetricKey {
|
||||||
h := fnv.New64a()
|
h := fnv.New64a()
|
||||||
for _, label := range labels {
|
for _, label := range labels {
|
||||||
h.Write([]byte(label.Name))
|
h.Write([]byte(label.Name)) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte("\x00"))
|
h.Write([]byte("\x00")) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte(label.Value))
|
h.Write([]byte(label.Value)) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
h.Write([]byte("\x00"))
|
h.Write([]byte("\x00")) //nolint:revive // from hash.go: "It never returns an error"
|
||||||
}
|
}
|
||||||
return MetricKey(h.Sum64())
|
return MetricKey(h.Sum64())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ func NewSerializer(config *Config) (Serializer, error) {
|
||||||
case "msgpack":
|
case "msgpack":
|
||||||
serializer, err = NewMsgpackSerializer()
|
serializer, err = NewMsgpackSerializer()
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
|
err = fmt.Errorf("invalid data format: %s", config.DataFormat)
|
||||||
}
|
}
|
||||||
return serializer, err
|
return serializer, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,12 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSerializeMetricFloat(t *testing.T) {
|
func TestSerializeMetricFloat(t *testing.T) {
|
||||||
// Test sub-second time
|
// Test sub-second time
|
||||||
now := time.Unix(1529875740, 819000000)
|
now := time.Unix(1529875740, 819000000)
|
||||||
|
|
@ -30,9 +24,9 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||||
s, _ := NewSerializer(false, false)
|
s, _ := NewSerializer(false, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expS := `{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":1529875740.819}`
|
expS := `{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":1529875740.819}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricFloatHec(t *testing.T) {
|
func TestSerializeMetricFloatHec(t *testing.T) {
|
||||||
|
|
@ -49,9 +43,9 @@ func TestSerializeMetricFloatHec(t *testing.T) {
|
||||||
s, _ := NewSerializer(true, false)
|
s, _ := NewSerializer(true, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expS := `{"time":1529875740.819,"fields":{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
|
expS := `{"time":1529875740.819,"fields":{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricInt(t *testing.T) {
|
func TestSerializeMetricInt(t *testing.T) {
|
||||||
|
|
@ -67,10 +61,10 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
s, _ := NewSerializer(false, false)
|
s, _ := NewSerializer(false, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
|
expS := `{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricIntHec(t *testing.T) {
|
func TestSerializeMetricIntHec(t *testing.T) {
|
||||||
|
|
@ -86,10 +80,10 @@ func TestSerializeMetricIntHec(t *testing.T) {
|
||||||
s, _ := NewSerializer(true, false)
|
s, _ := NewSerializer(true, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"time":0,"fields":{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
|
expS := `{"time":0,"fields":{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricBool(t *testing.T) {
|
func TestSerializeMetricBool(t *testing.T) {
|
||||||
|
|
@ -98,17 +92,17 @@ func TestSerializeMetricBool(t *testing.T) {
|
||||||
"container-name": "telegraf-test",
|
"container-name": "telegraf-test",
|
||||||
}
|
}
|
||||||
fields := map[string]interface{}{
|
fields := map[string]interface{}{
|
||||||
"oomkiller": bool(true),
|
"oomkiller": true,
|
||||||
}
|
}
|
||||||
m := metric.New("docker", tags, fields, now)
|
m := metric.New("docker", tags, fields, now)
|
||||||
|
|
||||||
s, _ := NewSerializer(false, false)
|
s, _ := NewSerializer(false, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"_value":1,"container-name":"telegraf-test","metric_name":"docker.oomkiller","time":0}`
|
expS := `{"_value":1,"container-name":"telegraf-test","metric_name":"docker.oomkiller","time":0}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricBoolHec(t *testing.T) {
|
func TestSerializeMetricBoolHec(t *testing.T) {
|
||||||
|
|
@ -117,17 +111,17 @@ func TestSerializeMetricBoolHec(t *testing.T) {
|
||||||
"container-name": "telegraf-test",
|
"container-name": "telegraf-test",
|
||||||
}
|
}
|
||||||
fields := map[string]interface{}{
|
fields := map[string]interface{}{
|
||||||
"oomkiller": bool(false),
|
"oomkiller": false,
|
||||||
}
|
}
|
||||||
m := metric.New("docker", tags, fields, now)
|
m := metric.New("docker", tags, fields, now)
|
||||||
|
|
||||||
s, _ := NewSerializer(true, false)
|
s, _ := NewSerializer(true, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"time":0,"fields":{"_value":0,"container-name":"telegraf-test","metric_name":"docker.oomkiller"}}`
|
expS := `{"time":0,"fields":{"_value":0,"container-name":"telegraf-test","metric_name":"docker.oomkiller"}}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricString(t *testing.T) {
|
func TestSerializeMetricString(t *testing.T) {
|
||||||
|
|
@ -144,11 +138,11 @@ func TestSerializeMetricString(t *testing.T) {
|
||||||
s, _ := NewSerializer(false, false)
|
s, _ := NewSerializer(false, false)
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"_value":5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
|
expS := `{"_value":5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBatch(t *testing.T) {
|
func TestSerializeBatch(t *testing.T) {
|
||||||
|
|
@ -173,10 +167,10 @@ func TestSerializeBatch(t *testing.T) {
|
||||||
metrics := []telegraf.Metric{m, n}
|
metrics := []telegraf.Metric{m, n}
|
||||||
s, _ := NewSerializer(false, false)
|
s, _ := NewSerializer(false, false)
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"_value":42,"metric_name":"cpu.value","time":0}{"_value":92,"metric_name":"cpu.value","time":0}`
|
expS := `{"_value":42,"metric_name":"cpu.value","time":0}{"_value":92,"metric_name":"cpu.value","time":0}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMulti(t *testing.T) {
|
func TestSerializeMulti(t *testing.T) {
|
||||||
|
|
@ -193,10 +187,10 @@ func TestSerializeMulti(t *testing.T) {
|
||||||
metrics := []telegraf.Metric{m}
|
metrics := []telegraf.Metric{m}
|
||||||
s, _ := NewSerializer(false, true)
|
s, _ := NewSerializer(false, true)
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"metric_name:cpu.system":8,"metric_name:cpu.user":42,"time":0}`
|
expS := `{"metric_name:cpu.system":8,"metric_name:cpu.user":42,"time":0}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeBatchHec(t *testing.T) {
|
func TestSerializeBatchHec(t *testing.T) {
|
||||||
|
|
@ -219,10 +213,10 @@ func TestSerializeBatchHec(t *testing.T) {
|
||||||
metrics := []telegraf.Metric{m, n}
|
metrics := []telegraf.Metric{m, n}
|
||||||
s, _ := NewSerializer(true, false)
|
s, _ := NewSerializer(true, false)
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"time":0,"fields":{"_value":42,"metric_name":"cpu.value"}}{"time":0,"fields":{"_value":92,"metric_name":"cpu.value"}}`
|
expS := `{"time":0,"fields":{"_value":42,"metric_name":"cpu.value"}}{"time":0,"fields":{"_value":92,"metric_name":"cpu.value"}}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMultiHec(t *testing.T) {
|
func TestSerializeMultiHec(t *testing.T) {
|
||||||
|
|
@ -239,8 +233,8 @@ func TestSerializeMultiHec(t *testing.T) {
|
||||||
metrics := []telegraf.Metric{m}
|
metrics := []telegraf.Metric{m}
|
||||||
s, _ := NewSerializer(true, true)
|
s, _ := NewSerializer(true, true)
|
||||||
buf, err := s.SerializeBatch(metrics)
|
buf, err := s.SerializeBatch(metrics)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
expS := `{"time":0,"fields":{"metric_name:cpu.system":8,"metric_name:cpu.usage":42}}`
|
expS := `{"time":0,"fields":{"metric_name:cpu.system":8,"metric_name:cpu.usage":42}}`
|
||||||
assert.Equal(t, expS, string(buf))
|
require.Equal(t, expS, string(buf))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ func NewSerializer(prefix string, useStrict bool, sourceOverride []string) (*Wav
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WavefrontSerializer) serialize(m telegraf.Metric) {
|
func (s *WavefrontSerializer) serializeMetric(m telegraf.Metric) {
|
||||||
const metricSeparator = "."
|
const metricSeparator = "."
|
||||||
|
|
||||||
for fieldName, value := range m.Fields() {
|
for fieldName, value := range m.Fields() {
|
||||||
|
|
@ -90,7 +90,7 @@ func (s *WavefrontSerializer) serialize(m telegraf.Metric) {
|
||||||
func (s *WavefrontSerializer) Serialize(m telegraf.Metric) ([]byte, error) {
|
func (s *WavefrontSerializer) Serialize(m telegraf.Metric) ([]byte, error) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.scratch.Reset()
|
s.scratch.Reset()
|
||||||
s.serialize(m)
|
s.serializeMetric(m)
|
||||||
out := s.scratch.Copy()
|
out := s.scratch.Copy()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|
@ -100,7 +100,7 @@ func (s *WavefrontSerializer) SerializeBatch(metrics []telegraf.Metric) ([]byte,
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.scratch.Reset()
|
s.scratch.Reset()
|
||||||
for _, m := range metrics {
|
for _, m := range metrics {
|
||||||
s.serialize(m)
|
s.serializeMetric(m)
|
||||||
}
|
}
|
||||||
out := s.scratch.Copy()
|
out := s.scratch.Copy()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
@ -200,7 +200,7 @@ func (b *buffer) WriteString(s string) {
|
||||||
*b = append(*b, s...)
|
*b = append(*b, s...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is named WriteChar instead of WriteByte because the 'stdmethods' check
|
// WriteChar has this name instead of WriteByte because the 'stdmethods' check
|
||||||
// of 'go vet' wants WriteByte to have the signature:
|
// of 'go vet' wants WriteByte to have the signature:
|
||||||
//
|
//
|
||||||
// func (b *buffer) WriteByte(c byte) error { ... }
|
// func (b *buffer) WriteByte(c byte) error { ... }
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/influxdata/telegraf/plugins/outputs/wavefront"
|
"github.com/influxdata/telegraf/plugins/outputs/wavefront"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBuildTags(t *testing.T) {
|
func TestBuildTags(t *testing.T) {
|
||||||
|
|
@ -185,7 +186,7 @@ func TestSerializeMetricFloat(t *testing.T) {
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
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)}
|
expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 91.500000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricInt(t *testing.T) {
|
func TestSerializeMetricInt(t *testing.T) {
|
||||||
|
|
@ -204,7 +205,7 @@ func TestSerializeMetricInt(t *testing.T) {
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
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)}
|
expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricBoolTrue(t *testing.T) {
|
func TestSerializeMetricBoolTrue(t *testing.T) {
|
||||||
|
|
@ -223,7 +224,7 @@ func TestSerializeMetricBoolTrue(t *testing.T) {
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
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)}
|
expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 1.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricBoolFalse(t *testing.T) {
|
func TestSerializeMetricBoolFalse(t *testing.T) {
|
||||||
|
|
@ -242,7 +243,7 @@ func TestSerializeMetricBoolFalse(t *testing.T) {
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
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)}
|
expS := []string{fmt.Sprintf("\"cpu.usage.idle\" 0.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricFieldValue(t *testing.T) {
|
func TestSerializeMetricFieldValue(t *testing.T) {
|
||||||
|
|
@ -257,11 +258,12 @@ func TestSerializeMetricFieldValue(t *testing.T) {
|
||||||
m := metric.New("cpu", tags, fields, now)
|
m := metric.New("cpu", tags, fields, now)
|
||||||
|
|
||||||
s := WavefrontSerializer{}
|
s := WavefrontSerializer{}
|
||||||
buf, _ := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
|
require.NoError(t, err)
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
|
|
||||||
expS := []string{fmt.Sprintf("\"cpu\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
expS := []string{fmt.Sprintf("\"cpu\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerializeMetricPrefix(t *testing.T) {
|
func TestSerializeMetricPrefix(t *testing.T) {
|
||||||
|
|
@ -276,11 +278,12 @@ func TestSerializeMetricPrefix(t *testing.T) {
|
||||||
m := metric.New("cpu", tags, fields, now)
|
m := metric.New("cpu", tags, fields, now)
|
||||||
|
|
||||||
s := WavefrontSerializer{Prefix: "telegraf."}
|
s := WavefrontSerializer{Prefix: "telegraf."}
|
||||||
buf, _ := s.Serialize(m)
|
buf, err := s.Serialize(m)
|
||||||
|
require.NoError(t, err)
|
||||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||||
|
|
||||||
expS := []string{fmt.Sprintf("\"telegraf.cpu.usage.idle\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
expS := []string{fmt.Sprintf("\"telegraf.cpu.usage.idle\" 91.000000 %d source=\"realHost\" \"cpu\"=\"cpu0\"", now.UnixNano()/1000000000)}
|
||||||
assert.Equal(t, expS, mS)
|
require.Equal(t, expS, mS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkMetrics(b *testing.B) [4]telegraf.Metric {
|
func benchmarkMetrics(b *testing.B) [4]telegraf.Metric {
|
||||||
|
|
@ -310,7 +313,8 @@ func BenchmarkSerialize(b *testing.B) {
|
||||||
metrics := benchmarkMetrics(b)
|
metrics := benchmarkMetrics(b)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
s.Serialize(metrics[i%len(metrics)])
|
_, err := s.Serialize(metrics[i%len(metrics)])
|
||||||
|
require.NoError(b, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,6 +324,7 @@ func BenchmarkSerializeBatch(b *testing.B) {
|
||||||
metrics := m[:]
|
metrics := m[:]
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
s.SerializeBatch(metrics)
|
_, err := s.SerializeBatch(metrics)
|
||||||
|
require.NoError(b, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue