From 7cbde183de4381ab0df2cb055cdcb57cda4304b4 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Huy Date: Wed, 14 Apr 2021 04:31:07 +0700 Subject: [PATCH] Support float64 in enum processor (#8911) --- plugins/processors/enum/README.md | 2 +- plugins/processors/enum/enum.go | 2 ++ plugins/processors/enum/enum_test.go | 16 +++++++--------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/processors/enum/README.md b/plugins/processors/enum/README.md index 651e58e7d2fce..0aecaaa430474 100644 --- a/plugins/processors/enum/README.md +++ b/plugins/processors/enum/README.md @@ -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, int and bool +_green_ by numeric values such as 0, 1, 2. The plugin supports string, int, float64 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 diff --git a/plugins/processors/enum/enum.go b/plugins/processors/enum/enum.go index 6a4a7f67afffd..f31987775b6a5 100644 --- a/plugins/processors/enum/enum.go +++ b/plugins/processors/enum/enum.go @@ -145,6 +145,8 @@ func adjustValue(in interface{}) interface{} { return strconv.FormatBool(val) case int64: return strconv.FormatInt(val, 10) + case float64: + return strconv.FormatFloat(val, 'f', -1, 64) case uint64: return strconv.FormatUint(val, 10) default: diff --git a/plugins/processors/enum/enum_test.go b/plugins/processors/enum/enum_test.go index f8e3a34d0381c..53603ae0153c7 100644 --- a/plugins/processors/enum/enum_test.go +++ b/plugins/processors/enum/enum_test.go @@ -63,6 +63,7 @@ func TestRetainsMetric(t *testing.T) { assertFieldValue(t, "test", "string_value", fields) assertFieldValue(t, 200, "int_value", fields) assertFieldValue(t, 500, "uint_value", fields) + assertFieldValue(t, float64(3.14), "float_value", fields) assertFieldValue(t, true, "true_value", fields) assert.Equal(t, "m1", target.Name()) assert.Equal(t, source.Tags(), target.Tags()) @@ -78,15 +79,6 @@ func TestMapsSingleStringValueTag(t *testing.T) { assertTagValue(t, "valuable", "tag", tags) } -func TestNoFailureOnMappingsOnNonSupportedValuedFields(t *testing.T) { - mapper := EnumMapper{Mappings: []Mapping{{Field: "float_value", ValueMappings: map[string]interface{}{"3.14": "pi"}}}} - err := mapper.Init() - require.Nil(t, err) - fields := calculateProcessedValues(mapper, createTestMetric()) - - assertFieldValue(t, float64(3.14), "float_value", fields) -} - func TestMappings(t *testing.T) { mappings := []map[string][]interface{}{ { @@ -113,6 +105,12 @@ func TestMappings(t *testing.T) { "mapped_value": []interface{}{"internal_error", 1, false, false, false, false}, "expected_value": []interface{}{"internal_error", 1, false, 500, 500, 500}, }, + { + "field_name": []interface{}{"float_value"}, + "target_value": []interface{}{"3.14", "3.14", "3.14", "3.14", "not_float", "5"}, + "mapped_value": []interface{}{"pi", 1, false, float64(100.2), float64(3.14), "pi"}, + "expected_value": []interface{}{"pi", 1, false, float64(100.2), float64(3.14), float64(3.14)}, + }, } for _, mapping := range mappings {