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

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