From 6d876c18e0297689a13d31c72c82d859f89d95cd Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 13 Jul 2018 22:54:34 -0700 Subject: [PATCH] Fix metric can have duplicate field (#4422) --- metric/metric.go | 1 + metric/metric_test.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/metric/metric.go b/metric/metric.go index 077b3a314..9f1a42ccb 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -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)}) diff --git a/metric/metric_test.go b/metric/metric_test.go index 47d44f3ef..004fa5915 100644 --- a/metric/metric_test.go +++ b/metric/metric_test.go @@ -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)