Skip to content

Commit

Permalink
rename spaces to underscore, add snakecase option
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed Oct 18, 2021
1 parent a381c68 commit ea7817c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
5 changes: 3 additions & 2 deletions plugins/inputs/ethtool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ The ethtool input plugin pulls ethernet device stats. Fields pulled will depend
## Some drivers declare statistics with extra whitespace, different spacing,
## and mix cases. This list, when enabled, can be used to clean the keys.
## Here are the current possible normalizations:
## * snakecase: converts camelCaseWords to camel_case_words
## * trim: removes leading and trailing whitespace
## * lower: changes all capitalized letters to lowercase
## * spaces: replaces spaces with underscores
# normalize_keys = ["trim", "lower", "spaces"]
## * underscore: replaces spaces with underscores
# normalize_keys = ["snakecase", "trim", "lower", "underscore"]
```

Interfaces can be included or ignored using:
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/ethtool/ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ const (
## Some drivers declare statistics with extra whitespace, different spacing,
## and mix cases. This list, when enabled, can be used to clean the keys.
## Here are the current possible normalizations:
## * snakecase: converts camelCaseWords to camel_case_words
## * trim: removes leading and trailing whitespace
## * lower: changes all capitalized letters to lowercase
## * spaces: replaces spaces with underscores
# normalize_keys = ["trim", "lower", "spaces"]
## * underscore: replaces spaces with underscores
# normalize_keys = ["snakecase", "trim", "lower", "underscore"]
`
)

Expand Down
20 changes: 18 additions & 2 deletions plugins/inputs/ethtool/ethtool_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ethtool

import (
"net"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -89,21 +90,36 @@ func (e *Ethtool) gatherEthtoolStats(iface net.Interface, acc telegraf.Accumulat
}

// normalize key string; order matters to avoid replacing whitespace with
// underscores, then trying to trim those same underscores.
// underscores, then trying to trim those same underscores. Likewise with
// camelcase before trying to lower case things.
func (e *Ethtool) normalizeKey(key string) string {
// must trim whitespace or this will have a leading _
if inStringSlice(e.NormalizeKeys, "snakecase") {
key = camelCase2SnakeCase(strings.TrimSpace(key))
}
// must occur before underscore, otherwise nothing to trim
if inStringSlice(e.NormalizeKeys, "trim") {
key = strings.TrimSpace(key)
}
if inStringSlice(e.NormalizeKeys, "lower") {
key = strings.ToLower(key)
}
if inStringSlice(e.NormalizeKeys, "spaces") {
if inStringSlice(e.NormalizeKeys, "underscore") {
key = strings.ReplaceAll(key, " ", "_")
}

return key
}

func camelCase2SnakeCase(value string) string {
matchFirstCap := regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap := regexp.MustCompile("([a-z0-9])([A-Z])")

snake := matchFirstCap.ReplaceAllString(value, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}

func inStringSlice(slice []string, value string) bool {
for _, item := range slice {
if item == value {
Expand Down
33 changes: 30 additions & 3 deletions plugins/inputs/ethtool/ethtool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ type TestCase struct {
func TestNormalizedKeys(t *testing.T) {
cases := []TestCase{
{
normalization: []string{"spaces"},
normalization: []string{"underscore"},
stats: map[string]uint64{
"port rx": 1,
" Port_tx": 0,
Expand All @@ -403,7 +403,7 @@ func TestNormalizedKeys(t *testing.T) {
},
},
{
normalization: []string{"spaces", "lower"},
normalization: []string{"underscore", "lower"},
stats: map[string]uint64{
"Port rx": 1,
" Port_tx": 0,
Expand All @@ -416,7 +416,7 @@ func TestNormalizedKeys(t *testing.T) {
},
},
{
normalization: []string{"spaces", "lower", "trim"},
normalization: []string{"underscore", "lower", "trim"},
stats: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
Expand All @@ -428,6 +428,32 @@ func TestNormalizedKeys(t *testing.T) {
"interface_up": 0,
},
},
{
normalization: []string{"underscore", "lower", "snakecase", "trim"},
stats: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
"interface_up": 0,
},
expectedFields: map[string]uint64{
"port_rx": 1,
"port_tx": 0,
"interface_up": 0,
},
},
{
normalization: []string{"snakecase"},
stats: map[string]uint64{
" PortRX ": 1,
" PortTX": 0,
"interface_up": 0,
},
expectedFields: map[string]uint64{
"port_rx": 1,
"port_tx": 0,
"interface_up": 0,
},
},
{
normalization: []string{},
stats: map[string]uint64{
Expand Down Expand Up @@ -466,6 +492,7 @@ func TestNormalizedKeys(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, acc.Metrics, 1)

acc.AssertContainsFields(t, pluginName, toStringMapInterface(c.expectedFields))
acc.AssertContainsTaggedFields(t, pluginName, toStringMapInterface(c.expectedFields), expectedTags)
}
}

0 comments on commit ea7817c

Please sign in to comment.