Fix usage of loop variable in function closure (#5195)
This commit is contained in:
parent
9a0861f7e2
commit
3fbfe3acd2
|
@ -72,8 +72,8 @@ const sampleConfig = `
|
||||||
# field = "read_count"
|
# field = "read_count"
|
||||||
# suffix = "_count"
|
# suffix = "_count"
|
||||||
|
|
||||||
## Replace substrings within field names
|
## Replace all non-overlapping instances of old with new
|
||||||
# [[processors.strings.trim_suffix]]
|
# [[processors.strings.replace]]
|
||||||
# measurement = "*"
|
# measurement = "*"
|
||||||
# old = ":"
|
# old = ":"
|
||||||
# new = "_"
|
# new = "_"
|
||||||
|
@ -100,8 +100,8 @@ func (c *converter) convertTag(metric telegraf.Metric) {
|
||||||
tags[c.Tag] = tv
|
tags[c.Tag] = tv
|
||||||
}
|
}
|
||||||
|
|
||||||
for tag, value := range tags {
|
for key, value := range tags {
|
||||||
dest := tag
|
dest := key
|
||||||
if c.Tag != "*" && c.Dest != "" {
|
if c.Tag != "*" && c.Dest != "" {
|
||||||
dest = c.Dest
|
dest = c.Dest
|
||||||
}
|
}
|
||||||
|
@ -122,9 +122,9 @@ func (c *converter) convertField(metric telegraf.Metric) {
|
||||||
fields[c.Field] = fv
|
fields[c.Field] = fv
|
||||||
}
|
}
|
||||||
|
|
||||||
for tag, value := range fields {
|
for key, value := range fields {
|
||||||
dest := tag
|
dest := key
|
||||||
if c.Tag != "*" && c.Dest != "" {
|
if c.Field != "*" && c.Dest != "" {
|
||||||
dest = c.Dest
|
dest = c.Dest
|
||||||
}
|
}
|
||||||
if fv, ok := value.(string); ok {
|
if fv, ok := value.(string); ok {
|
||||||
|
@ -170,6 +170,7 @@ func (s *Strings) initOnce() {
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.Trim {
|
for _, c := range s.Trim {
|
||||||
|
c := c
|
||||||
if c.Cutset != "" {
|
if c.Cutset != "" {
|
||||||
c.fn = func(s string) string { return strings.Trim(s, c.Cutset) }
|
c.fn = func(s string) string { return strings.Trim(s, c.Cutset) }
|
||||||
} else {
|
} else {
|
||||||
|
@ -178,6 +179,7 @@ func (s *Strings) initOnce() {
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.TrimLeft {
|
for _, c := range s.TrimLeft {
|
||||||
|
c := c
|
||||||
if c.Cutset != "" {
|
if c.Cutset != "" {
|
||||||
c.fn = func(s string) string { return strings.TrimLeft(s, c.Cutset) }
|
c.fn = func(s string) string { return strings.TrimLeft(s, c.Cutset) }
|
||||||
} else {
|
} else {
|
||||||
|
@ -186,6 +188,7 @@ func (s *Strings) initOnce() {
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.TrimRight {
|
for _, c := range s.TrimRight {
|
||||||
|
c := c
|
||||||
if c.Cutset != "" {
|
if c.Cutset != "" {
|
||||||
c.fn = func(s string) string { return strings.TrimRight(s, c.Cutset) }
|
c.fn = func(s string) string { return strings.TrimRight(s, c.Cutset) }
|
||||||
} else {
|
} else {
|
||||||
|
@ -194,14 +197,17 @@ func (s *Strings) initOnce() {
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.TrimPrefix {
|
for _, c := range s.TrimPrefix {
|
||||||
|
c := c
|
||||||
c.fn = func(s string) string { return strings.TrimPrefix(s, c.Prefix) }
|
c.fn = func(s string) string { return strings.TrimPrefix(s, c.Prefix) }
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.TrimSuffix {
|
for _, c := range s.TrimSuffix {
|
||||||
|
c := c
|
||||||
c.fn = func(s string) string { return strings.TrimSuffix(s, c.Suffix) }
|
c.fn = func(s string) string { return strings.TrimSuffix(s, c.Suffix) }
|
||||||
s.converters = append(s.converters, c)
|
s.converters = append(s.converters, c)
|
||||||
}
|
}
|
||||||
for _, c := range s.Replace {
|
for _, c := range s.Replace {
|
||||||
|
c := c
|
||||||
c.fn = func(s string) string {
|
c.fn = func(s string) string {
|
||||||
newString := strings.Replace(s, c.Old, c.New, -1)
|
newString := strings.Replace(s, c.Old, c.New, -1)
|
||||||
if newString == "" {
|
if newString == "" {
|
||||||
|
|
|
@ -25,24 +25,6 @@ func newM1() telegraf.Metric {
|
||||||
return m1
|
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) {
|
func TestFieldConversions(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
@ -404,9 +386,38 @@ func TestMultipleConversions(t *testing.T) {
|
||||||
Tag: "verb",
|
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{}{
|
expectedFields := map[string]interface{}{
|
||||||
"request": "/mixed/case/path/?from=-1d&to=now",
|
"request": "/mixed/case/path/?from=-1d&to=now",
|
||||||
|
@ -419,6 +430,8 @@ func TestMultipleConversions(t *testing.T) {
|
||||||
"verb": "GET",
|
"verb": "GET",
|
||||||
"resp_code": "200",
|
"resp_code": "200",
|
||||||
"s-computername": "mixedcase_hostname",
|
"s-computername": "mixedcase_hostname",
|
||||||
|
"foo": "x",
|
||||||
|
"bar": "y",
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, expectedFields, processed[0].Fields())
|
assert.Equal(t, expectedFields, processed[0].Fields())
|
||||||
|
|
Loading…
Reference in New Issue