Add support for titlecase transformation to strings processor (#6982)

This commit is contained in:
Manthan Sharma 2020-02-05 07:02:41 +05:30 committed by GitHub
parent 0cc71dbd51
commit ae22db4b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -5,6 +5,7 @@ The `strings` plugin maps certain go string functions onto measurement, tag, and
Implemented functions are: Implemented functions are:
- lowercase - lowercase
- uppercase - uppercase
- titlecase
- trim - trim
- trim_left - trim_left
- trim_right - trim_right
@ -35,6 +36,10 @@ If you'd like to apply multiple processings to the same `tag_key` or `field_key`
# [[processors.strings.uppercase]] # [[processors.strings.uppercase]]
# tag = "method" # tag = "method"
## Convert a field value to titlecase
# [[processors.strings.titlecase]]
# field = "status"
## Trim leading and trailing whitespace using the default cutset ## Trim leading and trailing whitespace using the default cutset
# [[processors.strings.trim]] # [[processors.strings.trim]]
# field = "message" # field = "message"

View File

@ -13,6 +13,7 @@ import (
type Strings struct { type Strings struct {
Lowercase []converter `toml:"lowercase"` Lowercase []converter `toml:"lowercase"`
Uppercase []converter `toml:"uppercase"` Uppercase []converter `toml:"uppercase"`
Titlecase []converter `toml:"titlecase"`
Trim []converter `toml:"trim"` Trim []converter `toml:"trim"`
TrimLeft []converter `toml:"trim_left"` TrimLeft []converter `toml:"trim_left"`
TrimRight []converter `toml:"trim_right"` TrimRight []converter `toml:"trim_right"`
@ -55,6 +56,10 @@ const sampleConfig = `
# field = "uri_stem" # field = "uri_stem"
# dest = "uri_stem_normalised" # dest = "uri_stem_normalised"
## Convert a field value to titlecase
# [[processors.strings.titlecase]]
# field = "status"
## Trim leading and trailing whitespace using the default cutset ## Trim leading and trailing whitespace using the default cutset
# [[processors.strings.trim]] # [[processors.strings.trim]]
# field = "message" # field = "message"
@ -235,6 +240,10 @@ func (s *Strings) initOnce() {
c.fn = strings.ToUpper c.fn = strings.ToUpper
s.converters = append(s.converters, c) s.converters = append(s.converters, c)
} }
for _, c := range s.Titlecase {
c.fn = strings.Title
s.converters = append(s.converters, c)
}
for _, c := range s.Trim { for _, c := range s.Trim {
c := c c := c
if c.Cutset != "" { if c.Cutset != "" {

View File

@ -78,6 +78,21 @@ func TestFieldConversions(t *testing.T) {
require.Equal(t, "/MIXED/CASE/PATH/?FROM=-1D&TO=NOW", fv) require.Equal(t, "/MIXED/CASE/PATH/?FROM=-1D&TO=NOW", fv)
}, },
}, },
{
name: "Should change existing field to titlecase",
plugin: &Strings{
Titlecase: []converter{
{
Field: "request",
},
},
},
check: func(t *testing.T, actual telegraf.Metric) {
fv, ok := actual.GetField("request")
require.True(t, ok)
require.Equal(t, "/Mixed/CASE/PaTH/?From=-1D&To=Now", fv)
},
},
{ {
name: "Should add new lowercase field", name: "Should add new lowercase field",
plugin: &Strings{ plugin: &Strings{
@ -331,6 +346,7 @@ func TestFieldKeyConversions(t *testing.T) {
// Tag/field key multiple executions occur in the following order: (initOnce) // Tag/field key multiple executions occur in the following order: (initOnce)
// Lowercase // Lowercase
// Uppercase // Uppercase
// Titlecase
// Trim // Trim
// TrimLeft // TrimLeft
// TrimRight // TrimRight
@ -595,6 +611,30 @@ func TestTagConversions(t *testing.T) {
require.Equal(t, "MIXEDCASE_HOSTNAME", tv) require.Equal(t, "MIXEDCASE_HOSTNAME", tv)
}, },
}, },
{
name: "Should add new titlecase tag",
plugin: &Strings{
Titlecase: []converter{
{
Tag: "s-computername",
Dest: "s-computername_titlecase",
},
},
},
check: func(t *testing.T, actual telegraf.Metric) {
tv, ok := actual.GetTag("verb")
require.True(t, ok)
require.Equal(t, "GET", tv)
tv, ok = actual.GetTag("s-computername")
require.True(t, ok)
require.Equal(t, "MIXEDCASE_hostname", tv)
tv, ok = actual.GetTag("s-computername_titlecase")
require.True(t, ok)
require.Equal(t, "MIXEDCASE_hostname", tv)
},
},
} }
for _, tt := range tests { for _, tt := range tests {
@ -736,6 +776,11 @@ func TestMultipleConversions(t *testing.T) {
Tag: "verb", Tag: "verb",
}, },
}, },
Titlecase: []converter{
{
Field: "status",
},
},
Replace: []converter{ Replace: []converter{
{ {
Tag: "foo", Tag: "foo",
@ -763,6 +808,7 @@ func TestMultipleConversions(t *testing.T) {
"cs-host": "AAAbbb", "cs-host": "AAAbbb",
"ignore_number": int64(200), "ignore_number": int64(200),
"ignore_bool": true, "ignore_bool": true,
"status": "green",
}, },
time.Now(), time.Now(),
) )
@ -775,6 +821,7 @@ func TestMultipleConversions(t *testing.T) {
"ignore_bool": true, "ignore_bool": true,
"cs-host": "AAAbbb", "cs-host": "AAAbbb",
"cs-host_lowercase": "aaabbb", "cs-host_lowercase": "aaabbb",
"status": "Green",
} }
expectedTags := map[string]string{ expectedTags := map[string]string{
"verb": "GET", "verb": "GET",