Fix usage of loop variable in function closure (#5195)

This commit is contained in:
Daniel Nelson 2018-12-27 13:08:19 -08:00 committed by GitHub
parent 9a0861f7e2
commit 3fbfe3acd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 26 deletions

View File

@ -72,8 +72,8 @@ const sampleConfig = `
# field = "read_count"
# suffix = "_count"
## Replace substrings within field names
# [[processors.strings.trim_suffix]]
## Replace all non-overlapping instances of old with new
# [[processors.strings.replace]]
# measurement = "*"
# old = ":"
# new = "_"
@ -100,8 +100,8 @@ func (c *converter) convertTag(metric telegraf.Metric) {
tags[c.Tag] = tv
}
for tag, value := range tags {
dest := tag
for key, value := range tags {
dest := key
if c.Tag != "*" && c.Dest != "" {
dest = c.Dest
}
@ -122,9 +122,9 @@ func (c *converter) convertField(metric telegraf.Metric) {
fields[c.Field] = fv
}
for tag, value := range fields {
dest := tag
if c.Tag != "*" && c.Dest != "" {
for key, value := range fields {
dest := key
if c.Field != "*" && c.Dest != "" {
dest = c.Dest
}
if fv, ok := value.(string); ok {
@ -170,6 +170,7 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.Trim {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.Trim(s, c.Cutset) }
} else {
@ -178,6 +179,7 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimLeft {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.TrimLeft(s, c.Cutset) }
} else {
@ -186,6 +188,7 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimRight {
c := c
if c.Cutset != "" {
c.fn = func(s string) string { return strings.TrimRight(s, c.Cutset) }
} else {
@ -194,14 +197,17 @@ func (s *Strings) initOnce() {
s.converters = append(s.converters, c)
}
for _, c := range s.TrimPrefix {
c := c
c.fn = func(s string) string { return strings.TrimPrefix(s, c.Prefix) }
s.converters = append(s.converters, c)
}
for _, c := range s.TrimSuffix {
c := c
c.fn = func(s string) string { return strings.TrimSuffix(s, c.Suffix) }
s.converters = append(s.converters, c)
}
for _, c := range s.Replace {
c := c
c.fn = func(s string) string {
newString := strings.Replace(s, c.Old, c.New, -1)
if newString == "" {

View File

@ -25,24 +25,6 @@ func newM1() telegraf.Metric {
return m1
}
func newM2() telegraf.Metric {
m2, _ := metric.New("IIS_log",
map[string]string{
"verb": "GET",
"resp_code": "200",
"s-computername": "MIXEDCASE_hostname",
},
map[string]interface{}{
"request": "/mixed/CASE/paTH/?from=-1D&to=now",
"cs-host": "AAAbbb",
"ignore_number": int64(200),
"ignore_bool": true,
},
time.Now(),
)
return m2
}
func TestFieldConversions(t *testing.T) {
tests := []struct {
name string
@ -404,9 +386,38 @@ func TestMultipleConversions(t *testing.T) {
Tag: "verb",
},
},
Replace: []converter{
{
Tag: "foo",
Old: "a",
New: "x",
},
{
Tag: "bar",
Old: "b",
New: "y",
},
},
}
processed := plugin.Apply(newM2())
m, _ := metric.New("IIS_log",
map[string]string{
"verb": "GET",
"resp_code": "200",
"s-computername": "MIXEDCASE_hostname",
"foo": "a",
"bar": "b",
},
map[string]interface{}{
"request": "/mixed/CASE/paTH/?from=-1D&to=now",
"cs-host": "AAAbbb",
"ignore_number": int64(200),
"ignore_bool": true,
},
time.Now(),
)
processed := plugin.Apply(m)
expectedFields := map[string]interface{}{
"request": "/mixed/case/path/?from=-1d&to=now",
@ -419,6 +430,8 @@ func TestMultipleConversions(t *testing.T) {
"verb": "GET",
"resp_code": "200",
"s-computername": "mixedcase_hostname",
"foo": "x",
"bar": "y",
}
assert.Equal(t, expectedFields, processed[0].Fields())