feat(processors.enum): Add multiple tag mapping (#16701)

This commit is contained in:
Maya Strandboge 2025-04-08 07:34:24 -05:00 committed by GitHub
parent 5bb0002948
commit 05879b220e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 13 deletions

View File

@ -27,8 +27,8 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Names of the fields to map. Globs accepted. ## Names of the fields to map. Globs accepted.
fields = ["status"] fields = ["status"]
## Name of the tag to map. Globs accepted. ## Name of the tags to map. Globs accepted.
# tag = "status" # tags = ["status"]
## Destination tag or field to be used for the mapped value. By default the ## Destination tag or field to be used for the mapped value. By default the
## source tag or field is used, overwriting the original value. ## source tag or field is used, overwriting the original value.

View File

@ -19,8 +19,9 @@ type EnumMapper struct {
} }
type Mapping 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"` Field string `toml:"field" deprecated:"1.35.0;1.40.0;use 'fields' instead"`
Tags []string `toml:"tags"`
Fields []string `toml:"fields"` Fields []string `toml:"fields"`
Dest string `toml:"dest"` Dest string `toml:"dest"`
Default interface{} `toml:"default"` Default interface{} `toml:"default"`
@ -48,14 +49,17 @@ func (mapper *EnumMapper) Init() error {
} }
mapping.fieldFilter = fieldFilter mapping.fieldFilter = fieldFilter
// Handle deprecated tag option
if mapping.Tag != "" { if mapping.Tag != "" {
tagFilter, err := filter.Compile([]string{mapping.Tag}) mapping.Tags = append(mapping.Tags, mapping.Tag)
}
tagFilter, err := filter.Compile(mapping.Tags)
if err != nil { if err != nil {
return fmt.Errorf("failed to create new tag filter: %w", err) return fmt.Errorf("failed to create new tag filter: %w", err)
} }
mapping.tagFilter = tagFilter mapping.tagFilter = tagFilter
} }
}
return nil return nil
} }

View File

@ -72,7 +72,7 @@ func TestRetainsMetric(t *testing.T) {
func TestMapsSingleStringValueTag(t *testing.T) { func TestMapsSingleStringValueTag(t *testing.T) {
mapper := EnumMapper{Mappings: []*Mapping{{ mapper := EnumMapper{Mappings: []*Mapping{{
Tag: "tag", Tags: []string{"tag"},
ValueMappings: map[string]interface{}{"tag_value": "valuable"}, ValueMappings: map[string]interface{}{"tag_value": "valuable"},
}}} }}}
err := mapper.Init() err := mapper.Init()
@ -232,7 +232,7 @@ func TestFieldGlobMatching(t *testing.T) {
func TestTagGlobMatching(t *testing.T) { func TestTagGlobMatching(t *testing.T) {
mapper := EnumMapper{Mappings: []*Mapping{{ mapper := EnumMapper{Mappings: []*Mapping{{
Tag: "*", Tags: []string{"*"},
ValueMappings: map[string]interface{}{"tag_value": "glob"}, ValueMappings: map[string]interface{}{"tag_value": "glob"},
}}} }}}
err := mapper.Init() err := mapper.Init()
@ -281,7 +281,7 @@ func TestTracking(t *testing.T) {
m, _ = metric.WithTracking(m, notify) m, _ = metric.WithTracking(m, notify)
mapper := EnumMapper{Mappings: []*Mapping{{ mapper := EnumMapper{Mappings: []*Mapping{{
Tag: "*", Tags: []string{"*"},
ValueMappings: map[string]interface{}{"tag_value": "glob"}, ValueMappings: map[string]interface{}{"tag_value": "glob"},
}}} }}}
err := mapper.Init() err := mapper.Init()

View File

@ -4,8 +4,8 @@
## Names of the fields to map. Globs accepted. ## Names of the fields to map. Globs accepted.
fields = ["status"] fields = ["status"]
## Name of the tag to map. Globs accepted. ## Name of the tags to map. Globs accepted.
# tag = "status" # tags = ["status"]
## Destination tag or field to be used for the mapped value. By default the ## Destination tag or field to be used for the mapped value. By default the
## source tag or field is used, overwriting the original value. ## source tag or field is used, overwriting the original value.