Skip to content

Commit

Permalink
delete nil fields in the metric maker.
Browse files Browse the repository at this point in the history
closes #1771
  • Loading branch information
sparrc committed Oct 12, 2016
1 parent 03b2984 commit 8ca4a50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions internal/models/makemetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,9 +130,9 @@ func makemetric(
delete(fields, k)
continue
}
default:
fields[k] = v
}

fields[k] = v
}

var m telegraf.Metric
Expand Down
26 changes: 26 additions & 0 deletions internal/models/running_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8ca4a50

Please sign in to comment.