From 05879b220e38a7a38f6d22d0a6c4e54f3c52964f Mon Sep 17 00:00:00 2001 From: Maya Strandboge <136023093+mstrandboge@users.noreply.github.com> Date: Tue, 8 Apr 2025 07:34:24 -0500 Subject: [PATCH] feat(processors.enum): Add multiple tag mapping (#16701) --- plugins/processors/enum/README.md | 4 ++-- plugins/processors/enum/enum.go | 16 ++++++++++------ plugins/processors/enum/enum_test.go | 6 +++--- plugins/processors/enum/sample.conf | 4 ++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/plugins/processors/enum/README.md b/plugins/processors/enum/README.md index ec94fc3c1..f6e98ee26 100644 --- a/plugins/processors/enum/README.md +++ b/plugins/processors/enum/README.md @@ -27,8 +27,8 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## Names of the fields to map. Globs accepted. fields = ["status"] - ## Name of the tag to map. Globs accepted. - # tag = "status" + ## Name of the tags to map. Globs accepted. + # tags = ["status"] ## Destination tag or field to be used for the mapped value. By default the ## source tag or field is used, overwriting the original value. diff --git a/plugins/processors/enum/enum.go b/plugins/processors/enum/enum.go index e396cfa4c..32da96d03 100644 --- a/plugins/processors/enum/enum.go +++ b/plugins/processors/enum/enum.go @@ -19,8 +19,9 @@ type EnumMapper struct { } type Mapping struct { - Tag string `toml:"tag"` + Tag string `toml:"tag" deprecated:"1.35.0;1.40.0;use 'tags' instead"` Field string `toml:"field" deprecated:"1.35.0;1.40.0;use 'fields' instead"` + Tags []string `toml:"tags"` Fields []string `toml:"fields"` Dest string `toml:"dest"` Default interface{} `toml:"default"` @@ -48,13 +49,16 @@ func (mapper *EnumMapper) Init() error { } mapping.fieldFilter = fieldFilter + // Handle deprecated tag option if mapping.Tag != "" { - tagFilter, err := filter.Compile([]string{mapping.Tag}) - if err != nil { - return fmt.Errorf("failed to create new tag filter: %w", err) - } - mapping.tagFilter = tagFilter + mapping.Tags = append(mapping.Tags, mapping.Tag) } + + tagFilter, err := filter.Compile(mapping.Tags) + if err != nil { + return fmt.Errorf("failed to create new tag filter: %w", err) + } + mapping.tagFilter = tagFilter } return nil diff --git a/plugins/processors/enum/enum_test.go b/plugins/processors/enum/enum_test.go index aa5c6916a..93df743c0 100644 --- a/plugins/processors/enum/enum_test.go +++ b/plugins/processors/enum/enum_test.go @@ -72,7 +72,7 @@ func TestRetainsMetric(t *testing.T) { func TestMapsSingleStringValueTag(t *testing.T) { mapper := EnumMapper{Mappings: []*Mapping{{ - Tag: "tag", + Tags: []string{"tag"}, ValueMappings: map[string]interface{}{"tag_value": "valuable"}, }}} err := mapper.Init() @@ -232,7 +232,7 @@ func TestFieldGlobMatching(t *testing.T) { func TestTagGlobMatching(t *testing.T) { mapper := EnumMapper{Mappings: []*Mapping{{ - Tag: "*", + Tags: []string{"*"}, ValueMappings: map[string]interface{}{"tag_value": "glob"}, }}} err := mapper.Init() @@ -281,7 +281,7 @@ func TestTracking(t *testing.T) { m, _ = metric.WithTracking(m, notify) mapper := EnumMapper{Mappings: []*Mapping{{ - Tag: "*", + Tags: []string{"*"}, ValueMappings: map[string]interface{}{"tag_value": "glob"}, }}} err := mapper.Init() diff --git a/plugins/processors/enum/sample.conf b/plugins/processors/enum/sample.conf index d50b86ff6..852cf08db 100644 --- a/plugins/processors/enum/sample.conf +++ b/plugins/processors/enum/sample.conf @@ -4,8 +4,8 @@ ## Names of the fields to map. Globs accepted. fields = ["status"] - ## Name of the tag to map. Globs accepted. - # tag = "status" + ## Name of the tags to map. Globs accepted. + # tags = ["status"] ## Destination tag or field to be used for the mapped value. By default the ## source tag or field is used, overwriting the original value.