diff --git a/.golangci.yml b/.golangci.yml index aa6adfb65bfb7..cc9318c05e535 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ linters: - asciicheck - bidichk - bodyclose + - copyloopvar - depguard - dogsled - errcheck diff --git a/plugins/common/cookie/cookie_test.go b/plugins/common/cookie/cookie_test.go index f71b364216ab3..edd40bac5b798 100644 --- a/plugins/common/cookie/cookie_test.go +++ b/plugins/common/cookie/cookie_test.go @@ -230,7 +230,6 @@ func TestAuthConfig_Start(t *testing.T) { }, } for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { srv := newFakeServer(t) c := &CookieAuthConfig{ diff --git a/plugins/inputs/intel_pmu/reader.go b/plugins/inputs/intel_pmu/reader.go index d34083e3ca06a..b4ee81fdae1f9 100644 --- a/plugins/inputs/intel_pmu/reader.go +++ b/plugins/inputs/intel_pmu/reader.go @@ -97,11 +97,8 @@ func (ie *iaEntitiesValuesReader) readCoreEvents(entity *CoreEventEntity) ([]cor metrics := make([]coreMetric, len(entity.activeEvents)) errGroup := errgroup.Group{} - for i, event := range entity.activeEvents { - id := i - actualEvent := event - - if event == nil || event.PerfEvent == nil { + for id, actualEvent := range entity.activeEvents { + if actualEvent == nil || actualEvent.PerfEvent == nil { return nil, errors.New("active event or corresponding perf event is nil") } @@ -170,10 +167,7 @@ func (ie *iaEntitiesValuesReader) readMultiEventSeparately(multiEvent multiEvent metrics := make([]uncoreMetric, len(activeEvents)) group := errgroup.Group{} - for i, event := range activeEvents { - id := i - actualEvent := event - + for id, actualEvent := range activeEvents { group.Go(func() error { values, err := ie.eventReader.readValue(actualEvent) if err != nil { @@ -211,10 +205,7 @@ func (ie *iaEntitiesValuesReader) readMultiEventAgg(multiEvent multiEvent) (unco values := make([]ia.CounterValue, len(activeEvents)) group := errgroup.Group{} - for i, event := range activeEvents { - id := i - actualEvent := event - + for id, actualEvent := range activeEvents { group.Go(func() error { value, err := ie.eventReader.readValue(actualEvent) if err != nil { diff --git a/plugins/inputs/neptune_apex/neptune_apex_test.go b/plugins/inputs/neptune_apex/neptune_apex_test.go index 499c4e2634b0f..e962a005cec9e 100644 --- a/plugins/inputs/neptune_apex/neptune_apex_test.go +++ b/plugins/inputs/neptune_apex/neptune_apex_test.go @@ -44,7 +44,6 @@ func TestGather(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { var acc testutil.Accumulator n.Servers = test.servers @@ -360,7 +359,6 @@ func TestParseXML(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { var acc testutil.Accumulator err := n.parseXML(&acc, test.xmlResponse) @@ -403,7 +401,6 @@ func TestSendRequest(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() h := http.HandlerFunc(func( @@ -462,7 +459,6 @@ func TestParseTime(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() res, err := parseTime(test.input, test.timeZone) @@ -507,7 +503,6 @@ func TestFindProbe(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() index := findProbe(test.probeName, fakeProbes) diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index 29e80f9a13c35..c3ce0a4a24e8b 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -451,8 +451,7 @@ func TestServerName(t *testing.T) { {name: "errors", fromCfg: "otherex.com", fromTLS: "example.com", url: "https://other.example.com", err: true}, } - for _, elt := range tests { - test := elt + for _, test := range tests { t.Run(test.name, func(t *testing.T) { sc := &X509Cert{ Sources: []string{test.url}, diff --git a/plugins/parsers/csv/parser.go b/plugins/parsers/csv/parser.go index c9b86f75fd75b..02e092a2943e9 100644 --- a/plugins/parsers/csv/parser.go +++ b/plugins/parsers/csv/parser.go @@ -305,8 +305,7 @@ func parseCSV(p *Parser, r io.Reader) ([]telegraf.Metric, error) { continue } //concatenate header names - for i, h := range header { - name := h + for i, name := range header { if p.TrimSpace { name = strings.Trim(name, " ") } diff --git a/plugins/parsers/graphite/config.go b/plugins/parsers/graphite/config.go index 2bff0e08badc4..c156602c55e81 100644 --- a/plugins/parsers/graphite/config.go +++ b/plugins/parsers/graphite/config.go @@ -26,8 +26,8 @@ func (c *Config) validateTemplates() error { // map to keep track of filters we see filters := map[string]struct{}{} - for i, t := range c.Templates { - parts := strings.Fields(t) + for i, template := range c.Templates { + parts := strings.Fields(template) // Ensure template string is non-empty if len(parts) == 0 { return fmt.Errorf("missing template at position: %d", i) @@ -37,10 +37,9 @@ func (c *Config) validateTemplates() error { } if len(parts) > 3 { - return fmt.Errorf("invalid template format: %q", t) + return fmt.Errorf("invalid template format: %q", template) } - template := t filter := "" tags := "" if len(parts) >= 2 { diff --git a/plugins/parsers/json/json_flattener.go b/plugins/parsers/json/json_flattener.go index 566f85862e91b..fdb7ad070b2b2 100644 --- a/plugins/parsers/json/json_flattener.go +++ b/plugins/parsers/json/json_flattener.go @@ -22,7 +22,7 @@ func (f *JSONFlattener) FlattenJSON( // FullFlattenJSON flattens nested maps/interfaces into a fields map (including bools and string) func (f *JSONFlattener) FullFlattenJSON( - fieldname string, + fieldName string, v interface{}, convertString bool, convertBool bool, @@ -33,45 +33,44 @@ func (f *JSONFlattener) FullFlattenJSON( switch t := v.(type) { case map[string]interface{}: - for k, v := range t { - fieldkey := k - if fieldname != "" { - fieldkey = fieldname + "_" + fieldkey + for fieldKey, fieldVal := range t { + if fieldName != "" { + fieldKey = fieldName + "_" + fieldKey } - err := f.FullFlattenJSON(fieldkey, v, convertString, convertBool) + err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool) if err != nil { return err } } case []interface{}: - for i, v := range t { - fieldkey := strconv.Itoa(i) - if fieldname != "" { - fieldkey = fieldname + "_" + fieldkey + for i, fieldVal := range t { + fieldKey := strconv.Itoa(i) + if fieldName != "" { + fieldKey = fieldName + "_" + fieldKey } - err := f.FullFlattenJSON(fieldkey, v, convertString, convertBool) + err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool) if err != nil { return err } } case float64: - f.Fields[fieldname] = t + f.Fields[fieldName] = t case string: if !convertString { return nil } - f.Fields[fieldname] = v.(string) + f.Fields[fieldName] = v.(string) case bool: if !convertBool { return nil } - f.Fields[fieldname] = v.(bool) + f.Fields[fieldName] = v.(bool) case nil: return nil default: return fmt.Errorf("JSON Flattener: got unexpected type %T with value %v (%s)", - t, t, fieldname) + t, t, fieldName) } return nil } diff --git a/plugins/processors/strings/strings.go b/plugins/processors/strings/strings.go index 48c0bcc513e13..0b1971703ff42 100644 --- a/plugins/processors/strings/strings.go +++ b/plugins/processors/strings/strings.go @@ -68,8 +68,7 @@ func (c *converter) convertTag(metric telegraf.Metric) { tags[c.Tag] = tv } - for key, value := range tags { - dest := key + for dest, value := range tags { if c.Tag != "*" && c.Dest != "" { dest = c.Dest } @@ -111,8 +110,7 @@ func (c *converter) convertField(metric telegraf.Metric) { fields[c.Field] = fv } - for key, value := range fields { - dest := key + for dest, value := range fields { if c.Field != "*" && c.Dest != "" { dest = c.Dest } @@ -194,7 +192,6 @@ 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 { @@ -203,7 +200,6 @@ 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 { @@ -212,7 +208,6 @@ 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 { @@ -221,17 +216,14 @@ 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.ReplaceAll(s, c.Old, c.New) if newString == "" { @@ -243,7 +235,6 @@ 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 @@ -254,7 +245,6 @@ func (s *Strings) initOnce() { s.converters = append(s.converters, c) } for _, c := range s.Base64Decode { - c := c c.fn = func(s string) string { data, err := base64.StdEncoding.DecodeString(s) if err != nil { @@ -268,7 +258,6 @@ func (s *Strings) initOnce() { s.converters = append(s.converters, c) } for _, c := range s.ValidUTF8 { - c := c c.fn = func(s string) string { return strings.ToValidUTF8(s, c.Replacement) } s.converters = append(s.converters, c) } diff --git a/tools/config_includer/generator.go b/tools/config_includer/generator.go index 0f57c490843ac..d4686cc984d33 100644 --- a/tools/config_includer/generator.go +++ b/tools/config_includer/generator.go @@ -120,11 +120,11 @@ func main() { } pwd = string(filepath.Separator) + pwd for _, iname := range extractIncludes(tmpl) { - ifn := iname - if !strings.HasPrefix(ifn, "/") { - ifn = filepath.Join(pwd, ifn) + if !strings.HasPrefix(iname, "/") { + newUnresolved[iname] = filepath.Join(pwd, iname) + } else { + newUnresolved[iname] = iname } - newUnresolved[iname] = ifn } } unresolved = newUnresolved