Fix metric can have duplicate field (#4422)

This commit is contained in:
Daniel Nelson 2018-07-13 22:54:34 -07:00 committed by GitHub
parent af98d070f5
commit 6d876c18e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -168,6 +168,7 @@ func (m *metric) AddField(key string, value interface{}) {
for i, field := range m.fields {
if key == field.Key {
m.fields[i] = &telegraf.Field{Key: key, Value: convertField(value)}
return
}
}
m.fields = append(m.fields, &telegraf.Field{Key: key, Value: convertField(value)})

View File

@ -30,6 +30,7 @@ func TestNewMetric(t *testing.T) {
require.Equal(t, now, m.Time())
}
// cpu value=1
func baseMetric() telegraf.Metric {
tags := map[string]string{}
fields := map[string]interface{}{
@ -111,6 +112,8 @@ func TestAddFieldOverwrites(t *testing.T) {
m.AddField("value", 1.0)
m.AddField("value", 42.0)
require.Equal(t, 1, len(m.FieldList()))
value, ok := m.GetField("value")
require.True(t, ok)
require.Equal(t, 42.0, value)
@ -122,6 +125,8 @@ func TestAddFieldChangesType(t *testing.T) {
m.AddField("value", 1.0)
m.AddField("value", "xyzzy")
require.Equal(t, 1, len(m.FieldList()))
value, ok := m.GetField("value")
require.True(t, ok)
require.Equal(t, "xyzzy", value)