Add left function to string processor (#6189)

This commit is contained in:
Russ Savage 2019-07-31 16:55:25 -07:00 committed by Daniel Nelson
parent 28f1bdb696
commit ef4f4eed3a
3 changed files with 56 additions and 0 deletions

View File

@ -11,6 +11,7 @@ Implemented functions are:
- trim_prefix - trim_prefix
- trim_suffix - trim_suffix
- replace - replace
- left
Please note that in this implementation these are processed in the order that they appear above. Please note that in this implementation these are processed in the order that they appear above.
@ -62,6 +63,11 @@ If you'd like to apply multiple processings to the same `tag_key` or `field_key`
# measurement = "*" # measurement = "*"
# old = ":" # old = ":"
# new = "_" # new = "_"
## Trims strings based on width
# [[processors.strings.left]]
# field = "message"
# width = 10
``` ```
#### Trim, TrimLeft, TrimRight #### Trim, TrimLeft, TrimRight

View File

@ -17,6 +17,7 @@ type Strings struct {
TrimPrefix []converter `toml:"trim_prefix"` TrimPrefix []converter `toml:"trim_prefix"`
TrimSuffix []converter `toml:"trim_suffix"` TrimSuffix []converter `toml:"trim_suffix"`
Replace []converter `toml:"replace"` Replace []converter `toml:"replace"`
Left []converter `toml:"left"`
converters []converter converters []converter
init bool init bool
@ -36,6 +37,7 @@ type converter struct {
Prefix string Prefix string
Old string Old string
New string New string
Width int
fn ConvertFunc fn ConvertFunc
} }
@ -79,6 +81,11 @@ const sampleConfig = `
# measurement = "*" # measurement = "*"
# old = ":" # old = ":"
# new = "_" # new = "_"
## Trims strings based on width
# [[processors.strings.left]]
# field = "message"
# width = 10
` `
func (s *Strings) SampleConfig() string { func (s *Strings) SampleConfig() string {
@ -270,6 +277,17 @@ func (s *Strings) initOnce() {
} }
s.converters = append(s.converters, c) s.converters = append(s.converters, c)
} }
for _, c := range s.Left {
c := c
c.fn = func(s string) string {
if len(s) < c.Width {
return s
} else {
return s[:c.Width]
}
}
s.converters = append(s.converters, c)
}
s.init = true s.init = true
} }

View File

@ -479,6 +479,38 @@ func TestFieldKeyConversions(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 trim the existing field to 6 characters",
plugin: &Strings{
Left: []converter{
{
Field: "Request",
Width: 6,
},
},
},
check: func(t *testing.T, actual telegraf.Metric) {
fv, ok := actual.GetField("Request")
require.True(t, ok)
require.Equal(t, "/mixed", fv)
},
},
{
name: "Should do nothing to the string",
plugin: &Strings{
Left: []converter{
{
Field: "Request",
Width: 600,
},
},
},
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)
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {