diff --git a/internal/models/makemetric.go b/internal/models/makemetric.go index afaa9475e..71427607c 100644 --- a/internal/models/makemetric.go +++ b/internal/models/makemetric.go @@ -81,6 +81,9 @@ func makemetric( // Validate uint64 and float64 fields // convert all int & uint types to int64 switch val := v.(type) { + case nil: + // delete nil fields + delete(fields, k) case uint: fields[k] = int64(val) continue @@ -127,9 +130,9 @@ func makemetric( delete(fields, k) continue } + default: + fields[k] = v } - - fields[k] = v } var m telegraf.Metric diff --git a/internal/models/running_input_test.go b/internal/models/running_input_test.go index 12283057d..3d3b65b95 100644 --- a/internal/models/running_input_test.go +++ b/internal/models/running_input_test.go @@ -29,6 +29,32 @@ func TestMakeMetricNoFields(t *testing.T) { assert.Nil(t, m) } +// nil fields should get dropped +func TestMakeMetricNilFields(t *testing.T) { + now := time.Now() + ri := RunningInput{ + Config: &InputConfig{ + Name: "TestRunningInput", + }, + } + + m := ri.MakeMetric( + "RITest", + map[string]interface{}{ + "value": int(101), + "nil": nil, + }, + map[string]string{}, + telegraf.Untyped, + now, + ) + assert.Equal( + t, + fmt.Sprintf("RITest value=101i %d", now.UnixNano()), + m.String(), + ) +} + // make an untyped, counter, & gauge metric func TestMakeMetric(t *testing.T) { now := time.Now()