Fix metric can have duplicate field (#4422)
This commit is contained in:
parent
af98d070f5
commit
6d876c18e0
|
@ -168,6 +168,7 @@ func (m *metric) AddField(key string, value interface{}) {
|
||||||
for i, field := range m.fields {
|
for i, field := range m.fields {
|
||||||
if key == field.Key {
|
if key == field.Key {
|
||||||
m.fields[i] = &telegraf.Field{Key: key, Value: convertField(value)}
|
m.fields[i] = &telegraf.Field{Key: key, Value: convertField(value)}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.fields = append(m.fields, &telegraf.Field{Key: key, Value: convertField(value)})
|
m.fields = append(m.fields, &telegraf.Field{Key: key, Value: convertField(value)})
|
||||||
|
|
|
@ -30,6 +30,7 @@ func TestNewMetric(t *testing.T) {
|
||||||
require.Equal(t, now, m.Time())
|
require.Equal(t, now, m.Time())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cpu value=1
|
||||||
func baseMetric() telegraf.Metric {
|
func baseMetric() telegraf.Metric {
|
||||||
tags := map[string]string{}
|
tags := map[string]string{}
|
||||||
fields := map[string]interface{}{
|
fields := map[string]interface{}{
|
||||||
|
@ -111,6 +112,8 @@ func TestAddFieldOverwrites(t *testing.T) {
|
||||||
m.AddField("value", 1.0)
|
m.AddField("value", 1.0)
|
||||||
m.AddField("value", 42.0)
|
m.AddField("value", 42.0)
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(m.FieldList()))
|
||||||
|
|
||||||
value, ok := m.GetField("value")
|
value, ok := m.GetField("value")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 42.0, value)
|
require.Equal(t, 42.0, value)
|
||||||
|
@ -122,6 +125,8 @@ func TestAddFieldChangesType(t *testing.T) {
|
||||||
m.AddField("value", 1.0)
|
m.AddField("value", 1.0)
|
||||||
m.AddField("value", "xyzzy")
|
m.AddField("value", "xyzzy")
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(m.FieldList()))
|
||||||
|
|
||||||
value, ok := m.GetField("value")
|
value, ok := m.GetField("value")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, "xyzzy", value)
|
require.Equal(t, "xyzzy", value)
|
||||||
|
|
Loading…
Reference in New Issue