Skip to content

Commit

Permalink
Fix metric can have duplicate field (#4422)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6d876c1)
  • Loading branch information
danielnelson committed Jul 14, 2018
1 parent 47a5800 commit d20507e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
5 changes: 5 additions & 0 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d20507e

Please sign in to comment.