Skip to content

Commit

Permalink
Add integer support to processors.enum.
Browse files Browse the repository at this point in the history
Fixes: #7364
  • Loading branch information
HarshitOnGitHub committed May 8, 2020
1 parent 48b8357 commit 5e9a768
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plugins/processors/enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The Enum Processor allows the configuration of value mappings for metric tags or fields.
The main use-case for this is to rewrite status codes such as _red_, _amber_ and
_green_ by numeric values such as 0, 1, 2. The plugin supports string and bool
_green_ by numeric values such as 0, 1, 2. The plugin supports string, int and bool
types for the field values. Multiple tags or fields can be configured with separate
value mappings for each. Default mapping values can be configured to be
used for all values, which are not contained in the value_mappings. The
Expand Down
16 changes: 11 additions & 5 deletions plugins/processors/enum/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (mapper *EnumMapper) applyMappings(metric telegraf.Metric) telegraf.Metric
for _, mapping := range mapper.Mappings {
if mapping.Field != "" {
if originalValue, isPresent := metric.GetField(mapping.Field); isPresent {
if adjustedValue, isString := adjustBoolValue(originalValue).(string); isString {
if adjustedValue, isString := adjustValue(originalValue).(string); isString {
if mappedValue, isMappedValuePresent := mapping.mapValue(adjustedValue); isMappedValuePresent {
writeField(metric, mapping.getDestination(), mappedValue)
}
Expand All @@ -86,11 +86,17 @@ func (mapper *EnumMapper) applyMappings(metric telegraf.Metric) telegraf.Metric
return metric
}

func adjustBoolValue(in interface{}) interface{} {
if mappedBool, isBool := in.(bool); isBool == true {
return strconv.FormatBool(mappedBool)
func adjustValue(in interface{}) interface{} {
switch val := in.(type) {
case bool:
return strconv.FormatBool(val)
case int64:
return strconv.FormatInt(val, 10)
case uint64:
return strconv.FormatUint(val, 10)
default:
return in
}
return in
}

func (mapping *Mapping) mapValue(original string) (interface{}, bool) {
Expand Down
24 changes: 21 additions & 3 deletions plugins/processors/enum/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func createTestMetric() telegraf.Metric {
map[string]interface{}{
"string_value": "test",
"int_value": int(13),
"uint_value": uint(50),
"float_value": float64(3.14),
"true_value": true,
},
time.Now(),
Expand Down Expand Up @@ -75,12 +77,12 @@ func TestMapsSingleStringValueTag(t *testing.T) {
assertTagValue(t, "valuable", "tag", tags)
}

func TestNoFailureOnMappingsOnNonStringValuedFields(t *testing.T) {
mapper := EnumMapper{Mappings: []Mapping{{Field: "int_value", ValueMappings: map[string]interface{}{"13i": int64(7)}}}}
func TestNoFailureOnMappingsOnNonSupportedValuedFields(t *testing.T) {
mapper := EnumMapper{Mappings: []Mapping{{Field: "float_value", ValueMappings: map[string]interface{}{"3.14": "pi"}}}}

fields := calculateProcessedValues(mapper, createTestMetric())

assertFieldValue(t, 13, "int_value", fields)
assertFieldValue(t, float64(3.14), "float_value", fields)
}

func TestMapSingleBoolValue(t *testing.T) {
Expand All @@ -91,6 +93,22 @@ func TestMapSingleBoolValue(t *testing.T) {
assertFieldValue(t, 1, "true_value", fields)
}

func TestMapSingleIntValue(t *testing.T) {
mapper := EnumMapper{Mappings: []Mapping{{Field: "int_value", ValueMappings: map[string]interface{}{"13": int64(14)}}}}

fields := calculateProcessedValues(mapper, createTestMetric())

assertFieldValue(t, 14, "int_value", fields)
}

func TestMapSingleUintValue(t *testing.T) {
mapper := EnumMapper{Mappings: []Mapping{{Field: "uint_value", ValueMappings: map[string]interface{}{"50": int64(5)}}}}

fields := calculateProcessedValues(mapper, createTestMetric())

assertFieldValue(t, int64(5), "uint_value", fields)
}

func TestMapsToDefaultValueOnUnknownSourceValue(t *testing.T) {
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", Default: int64(42), ValueMappings: map[string]interface{}{"other": int64(1)}}}}

Expand Down

0 comments on commit 5e9a768

Please sign in to comment.