From a6fc61eadd6519204112b85e081823660a896661 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Fri, 7 Oct 2016 18:12:36 +0100 Subject: [PATCH] delete nil fields in the metric maker. closes #1771 --- internal/models/makemetric.go | 7 +++++-- internal/models/running_input_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/internal/models/makemetric.go b/internal/models/makemetric.go index afaa9475e8761..71427607c187c 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 12283057d390b..3d3b65b953151 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()