Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding left string function #6189

Merged
merged 2 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugins/processors/strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Implemented functions are:
- trim_prefix
- trim_suffix
- replace
- left

Please note that in this implementation these are processed in the order that they appear above.

Expand Down Expand Up @@ -62,6 +63,11 @@ If you'd like to apply multiple processings to the same `tag_key` or `field_key`
# measurement = "*"
# old = ":"
# new = "_"

## Trims strings based on width
# [[processors.strings.left]]
# field = "message"
# width = 10
```

#### Trim, TrimLeft, TrimRight
Expand Down
18 changes: 18 additions & 0 deletions plugins/processors/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +37,7 @@ type converter struct {
Prefix string
Old string
New string
Width int

fn ConvertFunc
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 32 additions & 0 deletions plugins/processors/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,38 @@ func TestFieldKeyConversions(t *testing.T) {
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 {
t.Run(tt.name, func(t *testing.T) {
Expand Down