diff --git a/.golangci.yml b/.golangci.yml index 6654cb95e..e8665aede 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -99,6 +99,7 @@ linters-settings: - offBy1 - regexpPattern - sloppyTestFuncName + - sloppyReassign - sloppyTypeAssert - sortSlice - sprintfQuotedString diff --git a/internal/rotate/file_writer.go b/internal/rotate/file_writer.go index 797560358..72793ae64 100644 --- a/internal/rotate/file_writer.go +++ b/internal/rotate/file_writer.go @@ -81,7 +81,7 @@ func (w *FileWriter) Write(p []byte) (n int, err error) { } w.bytesWritten += int64(n) - if err = w.rotateIfNeeded(); err != nil { + if err := w.rotateIfNeeded(); err != nil { return 0, err } @@ -144,14 +144,14 @@ func (w *FileWriter) rotateIfNeeded() error { } func (w *FileWriter) rotate() (err error) { - if err = w.current.Close(); err != nil { + if err := w.current.Close(); err != nil { return err } // Use year-month-date for readability, unix time to make the file name unique with second precision now := time.Now() rotatedFilename := fmt.Sprintf(w.filenameRotationTemplate, now.Format(DateFormat), strconv.FormatInt(now.Unix(), 10)) - if err = os.Rename(w.filename, rotatedFilename); err != nil { + if err := os.Rename(w.filename, rotatedFilename); err != nil { return err } @@ -174,7 +174,7 @@ func (w *FileWriter) purgeArchivesIfNeeded() (err error) { //sort files alphanumerically to delete older files first sort.Strings(matches) for _, filename := range matches[:len(matches)-w.maxArchives] { - if err = os.Remove(filename); err != nil { + if err := os.Remove(filename); err != nil { return err } } diff --git a/plugins/common/cookie/cookie.go b/plugins/common/cookie/cookie.go index 849ab8ed7..e275160b2 100644 --- a/plugins/common/cookie/cookie.go +++ b/plugins/common/cookie/cookie.go @@ -33,7 +33,7 @@ type CookieAuthConfig struct { } func (c *CookieAuthConfig) Start(client *http.Client, log telegraf.Logger, clock clockutil.Clock) (err error) { - if err = c.initializeClient(client); err != nil { + if err := c.initializeClient(client); err != nil { return err } diff --git a/plugins/common/starlark/field_dict.go b/plugins/common/starlark/field_dict.go index a1a553570..50b3ca388 100644 --- a/plugins/common/starlark/field_dict.go +++ b/plugins/common/starlark/field_dict.go @@ -243,7 +243,7 @@ func asStarlarkValue(value interface{}) (starlark.Value, error) { if err != nil { return starlark.None, err } - if err = dict.SetKey(sKey, sValue); err != nil { + if err := dict.SetKey(sKey, sValue); err != nil { return starlark.None, err } } diff --git a/plugins/inputs/azure_monitor/azure_monitor_test.go b/plugins/inputs/azure_monitor/azure_monitor_test.go index c5c3548f6..9c803b34c 100644 --- a/plugins/inputs/azure_monitor/azure_monitor_test.go +++ b/plugins/inputs/azure_monitor/azure_monitor_test.go @@ -41,7 +41,7 @@ func (marc *mockAzureResourcesClient) List(_ context.Context, _ *armresources.Cl } var genericResourcesExpanded []*armresources.GenericResourceExpanded - if err = json.Unmarshal(file, &genericResourcesExpanded); err != nil { + if err := json.Unmarshal(file, &genericResourcesExpanded); err != nil { return nil, err } @@ -67,7 +67,7 @@ func (marc *mockAzureResourcesClient) ListByResourceGroup( } var genericResourcesExpanded []*armresources.GenericResourceExpanded - if err = json.Unmarshal(file, &genericResourcesExpanded); err != nil { + if err := json.Unmarshal(file, &genericResourcesExpanded); err != nil { return nil, err } @@ -111,7 +111,7 @@ func (mamdc *mockAzureMetricDefinitionsClient) List( } var metricDefinitions [][]*armmonitor.MetricDefinition - if err = json.Unmarshal(file, &metricDefinitions); err != nil { + if err := json.Unmarshal(file, &metricDefinitions); err != nil { return armmonitor.MetricDefinitionsClientListResponse{}, err } @@ -152,7 +152,7 @@ func (mamc *mockAzureMetricsClient) List( } var metricResponses []armmonitor.Response - if err = json.Unmarshal(file, &metricResponses); err != nil { + if err := json.Unmarshal(file, &metricResponses); err != nil { return armmonitor.MetricsClientListResponse{}, err } diff --git a/plugins/inputs/dpdk/dpdk.go b/plugins/inputs/dpdk/dpdk.go index e6d225c57..726adb1fd 100644 --- a/plugins/inputs/dpdk/dpdk.go +++ b/plugins/inputs/dpdk/dpdk.go @@ -75,14 +75,14 @@ func (dpdk *dpdk) Init() error { } var err error - if err = isSocket(dpdk.SocketPath); err != nil { + if err := isSocket(dpdk.SocketPath); err != nil { return err } dpdk.rawdevCommands = []string{"/rawdev/xstats"} dpdk.ethdevCommands = []string{"/ethdev/stats", "/ethdev/xstats", "/ethdev/link_status"} - if err = dpdk.validateCommands(); err != nil { + if err := dpdk.validateCommands(); err != nil { return err } diff --git a/plugins/inputs/intel_dlb/intel_dlb.go b/plugins/inputs/intel_dlb/intel_dlb.go index 46ed1f93e..187c8f16c 100644 --- a/plugins/inputs/intel_dlb/intel_dlb.go +++ b/plugins/inputs/intel_dlb/intel_dlb.go @@ -85,7 +85,7 @@ func (d *IntelDLB) Init() error { d.Log.Debugf("Using default eventdev commands '%v'", eventdevDefaultCommands) } - if err = validateEventdevCommands(d.EventdevCommands); err != nil { + if err := validateEventdevCommands(d.EventdevCommands); err != nil { return err } diff --git a/plugins/inputs/intel_rdt/intel_rdt.go b/plugins/inputs/intel_rdt/intel_rdt.go index df056f48d..8d61cbb20 100644 --- a/plugins/inputs/intel_rdt/intel_rdt.go +++ b/plugins/inputs/intel_rdt/intel_rdt.go @@ -123,7 +123,7 @@ func (r *IntelRDT) Initialize() error { if r.SamplingInterval == 0 { r.SamplingInterval = defaultSamplingInterval } - if err = validateInterval(r.SamplingInterval); err != nil { + if err := validateInterval(r.SamplingInterval); err != nil { return err } r.parsedCores, err = parseCoresConfig(r.Cores) diff --git a/plugins/inputs/jenkins/jenkins.go b/plugins/inputs/jenkins/jenkins.go index 341337cd3..37caa9d97 100644 --- a/plugins/inputs/jenkins/jenkins.go +++ b/plugins/inputs/jenkins/jenkins.go @@ -72,7 +72,7 @@ func (j *Jenkins) Gather(acc telegraf.Accumulator) error { if err != nil { return err } - if err = j.initialize(client); err != nil { + if err := j.initialize(client); err != nil { return err } } diff --git a/plugins/inputs/kibana/kibana.go b/plugins/inputs/kibana/kibana.go index d22865240..1beb86a70 100644 --- a/plugins/inputs/kibana/kibana.go +++ b/plugins/inputs/kibana/kibana.go @@ -238,7 +238,7 @@ func (k *Kibana) gatherJSONData(url string, v interface{}) (host string, err err return request.Host, fmt.Errorf("%s returned HTTP status %s: %q", url, response.Status, body) } - if err = json.NewDecoder(response.Body).Decode(v); err != nil { + if err := json.NewDecoder(response.Body).Decode(v); err != nil { return request.Host, err } diff --git a/plugins/inputs/libvirt/libvirt.go b/plugins/inputs/libvirt/libvirt.go index eab5c6fe4..181d052e8 100644 --- a/plugins/inputs/libvirt/libvirt.go +++ b/plugins/inputs/libvirt/libvirt.go @@ -152,7 +152,7 @@ func (l *Libvirt) isThereAnythingToGather() bool { func (l *Libvirt) Gather(acc telegraf.Accumulator) error { var err error - if err = l.utils.EnsureConnected(l.LibvirtURI); err != nil { + if err := l.utils.EnsureConnected(l.LibvirtURI); err != nil { return err } diff --git a/plugins/inputs/mailchimp/chimp_api.go b/plugins/inputs/mailchimp/chimp_api.go index 71e7bcea6..f603bbfb8 100644 --- a/plugins/inputs/mailchimp/chimp_api.go +++ b/plugins/inputs/mailchimp/chimp_api.go @@ -161,7 +161,7 @@ func (a *ChimpAPI) runChimp(params ReportsParams) ([]byte, error) { a.log.Debugf("response Body: %q", string(body)) } - if err = chimpErrorCheck(body); err != nil { + if err := chimpErrorCheck(body); err != nil { return nil, err } return body, nil diff --git a/plugins/inputs/mysql/mysql.go b/plugins/inputs/mysql/mysql.go index 68359120b..7a39f9995 100644 --- a/plugins/inputs/mysql/mysql.go +++ b/plugins/inputs/mysql/mysql.go @@ -641,7 +641,7 @@ func (m *Mysql) gatherSlaveStatuses(db *sql.DB, servtag string, acc telegraf.Acc vals[i] = sql.RawBytes{} valPtrs[i] = &vals[i] } - if err = rows.Scan(valPtrs...); err != nil { + if err := rows.Scan(valPtrs...); err != nil { return err } @@ -763,7 +763,7 @@ func (m *Mysql) gatherGlobalStatuses(db *sql.DB, servtag string, acc telegraf.Ac var key string var val sql.RawBytes - if err = rows.Scan(&key, &val); err != nil { + if err := rows.Scan(&key, &val); err != nil { return err } diff --git a/plugins/inputs/opensearch_query/opensearch_query_test.go b/plugins/inputs/opensearch_query/opensearch_query_test.go index 8f8db4fb0..edcc01560 100644 --- a/plugins/inputs/opensearch_query/opensearch_query_test.go +++ b/plugins/inputs/opensearch_query/opensearch_query_test.go @@ -627,7 +627,7 @@ func setupIntegrationTest(t *testing.T, image string) (*testutil.Container, *Ope return &container, o, err } - if err = indexer.Close(context.Background()); err != nil { + if err := indexer.Close(context.Background()); err != nil { return &container, o, err } diff --git a/plugins/inputs/phpfpm/fcgi.go b/plugins/inputs/phpfpm/fcgi.go index 262c62a1e..9cda44b22 100644 --- a/plugins/inputs/phpfpm/fcgi.go +++ b/plugins/inputs/phpfpm/fcgi.go @@ -120,7 +120,7 @@ type record struct { } func (rec *record) read(r io.Reader) (err error) { - if err = binary.Read(r, binary.BigEndian, &rec.h); err != nil { + if err := binary.Read(r, binary.BigEndian, &rec.h); err != nil { return err } if rec.h.Version != 1 { diff --git a/plugins/inputs/phpfpm/fcgi_client.go b/plugins/inputs/phpfpm/fcgi_client.go index 8f6d29079..87b757dcf 100644 --- a/plugins/inputs/phpfpm/fcgi_client.go +++ b/plugins/inputs/phpfpm/fcgi_client.go @@ -48,7 +48,7 @@ func (c *conn) Request(env map[string]string, requestData string) (retout []byte } if len(requestData) > 0 { - if err = c.writeRecord(typeStdin, reqID, []byte(requestData)); err != nil { + if err := c.writeRecord(typeStdin, reqID, []byte(requestData)); err != nil { return nil, nil, err } } diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index 8d65e6338..1fb39a690 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -198,7 +198,7 @@ func (p *Postgresql) accRow(measName string, row scanner, acc telegraf.Accumulat } // deconstruct array of variables and send to Scan - if err = row.Scan(columnVars...); err != nil { + if err := row.Scan(columnVars...); err != nil { return err } diff --git a/plugins/inputs/powerdns_recursor/protocol_v3.go b/plugins/inputs/powerdns_recursor/protocol_v3.go index b0e2884e3..857f851df 100644 --- a/plugins/inputs/powerdns_recursor/protocol_v3.go +++ b/plugins/inputs/powerdns_recursor/protocol_v3.go @@ -34,7 +34,7 @@ func (p *PowerdnsRecursor) gatherFromV3Server(address string, acc telegraf.Accum command := []byte("get-all") - if err = writeNativeUIntToConn(conn, uint(len(command))); err != nil { + if err := writeNativeUIntToConn(conn, uint(len(command))); err != nil { return err } diff --git a/plugins/inputs/salesforce/salesforce.go b/plugins/inputs/salesforce/salesforce.go index 20c82fdd9..ab452f4f0 100644 --- a/plugins/inputs/salesforce/salesforce.go +++ b/plugins/inputs/salesforce/salesforce.go @@ -117,7 +117,7 @@ func (s *Salesforce) fetchLimits() (limits, error) { defer resp.Body.Close() if resp.StatusCode == http.StatusUnauthorized { - if err = s.login(); err != nil { + if err := s.login(); err != nil { return l, err } resp, err = s.queryLimits() diff --git a/plugins/inputs/sflow/packetdecoder.go b/plugins/inputs/sflow/packetdecoder.go index 02b94b8e4..d03141f82 100644 --- a/plugins/inputs/sflow/packetdecoder.go +++ b/plugins/inputs/sflow/packetdecoder.go @@ -63,7 +63,7 @@ func (d *PacketDecoder) DecodeOnePacket(r io.Reader) (*V5Format, error) { return nil, fmt.Errorf("Version %d not supported, only version 5", p.Version) } var addressIPType AddressType - if err = read(r, &addressIPType, "address ip type"); err != nil { + if err := read(r, &addressIPType, "address ip type"); err != nil { return nil, err } switch addressIPType { @@ -76,16 +76,16 @@ func (d *PacketDecoder) DecodeOnePacket(r io.Reader) (*V5Format, error) { default: return nil, fmt.Errorf("Unknown address IP type %d", addressIPType) } - if err = read(r, &p.AgentAddress.IP, "Agent Address IP"); err != nil { + if err := read(r, &p.AgentAddress.IP, "Agent Address IP"); err != nil { return nil, err } - if err = read(r, &p.SubAgentID, "SubAgentID"); err != nil { + if err := read(r, &p.SubAgentID, "SubAgentID"); err != nil { return nil, err } - if err = read(r, &p.SequenceNumber, "SequenceNumber"); err != nil { + if err := read(r, &p.SequenceNumber, "SequenceNumber"); err != nil { return nil, err } - if err = read(r, &p.Uptime, "Uptime"); err != nil { + if err := read(r, &p.Uptime, "Uptime"); err != nil { return nil, err } diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index 3aec29b52..7cbf7df31 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -434,12 +434,12 @@ func (s *Statsd) tcpListen(listener *net.TCPListener) error { } if s.TCPKeepAlive { - if err = conn.SetKeepAlive(true); err != nil { + if err := conn.SetKeepAlive(true); err != nil { return err } if s.TCPKeepAlivePeriod != nil { - if err = conn.SetKeepAlivePeriod(time.Duration(*s.TCPKeepAlivePeriod)); err != nil { + if err := conn.SetKeepAlivePeriod(time.Duration(*s.TCPKeepAlivePeriod)); err != nil { return err } } diff --git a/plugins/inputs/teamspeak/teamspeak.go b/plugins/inputs/teamspeak/teamspeak.go index be01210fc..41d78a9e3 100644 --- a/plugins/inputs/teamspeak/teamspeak.go +++ b/plugins/inputs/teamspeak/teamspeak.go @@ -40,10 +40,10 @@ func (ts *Teamspeak) connect() error { if len(ts.Nickname) > 0 { for _, vserver := range ts.VirtualServers { - if err = ts.client.Use(vserver); err != nil { + if err := ts.client.Use(vserver); err != nil { return err } - if err = ts.client.SetNick(ts.Nickname); err != nil { + if err := ts.client.SetNick(ts.Nickname); err != nil { return err } } diff --git a/plugins/inputs/win_eventlog/util.go b/plugins/inputs/win_eventlog/util.go index 9c1e324f8..054592db5 100644 --- a/plugins/inputs/win_eventlog/util.go +++ b/plugins/inputs/win_eventlog/util.go @@ -50,7 +50,7 @@ func GetFromSnapProcess(pid uint32) (string, error) { defer windows.CloseHandle(snap) var pe32 windows.ProcessEntry32 pe32.Size = uint32(unsafe.Sizeof(pe32)) //nolint:gosec // G103: Valid use of unsafe call to determine the size of the struct - if err = windows.Process32First(snap, &pe32); err != nil { + if err := windows.Process32First(snap, &pe32); err != nil { return "", err } for { diff --git a/plugins/inputs/win_perf_counters/win_perf_counters.go b/plugins/inputs/win_perf_counters/win_perf_counters.go index ea453120e..ed70c262d 100644 --- a/plugins/inputs/win_perf_counters/win_perf_counters.go +++ b/plugins/inputs/win_perf_counters/win_perf_counters.go @@ -208,7 +208,7 @@ func (m *WinPerfCounters) AddItem(counterPath, computer, objectName, instance, c hostCounter = &hostCountersInfo{computer: computer, tag: sourceTag} m.hostCounters[computer] = hostCounter hostCounter.query = m.queryCreator.NewPerformanceQuery(computer) - if err = hostCounter.query.Open(); err != nil { + if err := hostCounter.query.Open(); err != nil { return err } hostCounter.counters = make([]*counter, 0) @@ -413,11 +413,11 @@ func (m *WinPerfCounters) Gather(acc telegraf.Accumulator) error { var err error if m.lastRefreshed.IsZero() || (m.CountersRefreshInterval > 0 && m.lastRefreshed.Add(time.Duration(m.CountersRefreshInterval)).Before(time.Now())) { - if err = m.cleanQueries(); err != nil { + if err := m.cleanQueries(); err != nil { return err } - if err = m.ParseConfig(); err != nil { + if err := m.ParseConfig(); err != nil { return err } for _, hostCounterSet := range m.hostCounters { @@ -439,7 +439,7 @@ func (m *WinPerfCounters) Gather(acc telegraf.Accumulator) error { } } else { hostCounterSet.timestamp = time.Now() - if err = hostCounterSet.query.CollectData(); err != nil { + if err := hostCounterSet.query.CollectData(); err != nil { return err } } diff --git a/plugins/inputs/zipkin/codec/thrift/thrift.go b/plugins/inputs/zipkin/codec/thrift/thrift.go index a198f4b7e..a05202148 100644 --- a/plugins/inputs/zipkin/codec/thrift/thrift.go +++ b/plugins/inputs/zipkin/codec/thrift/thrift.go @@ -29,13 +29,13 @@ func UnmarshalThrift(body []byte) ([]*zipkincore.Span, error) { spans := make([]*zipkincore.Span, 0, size) for i := 0; i < size; i++ { zs := &zipkincore.Span{} - if err = zs.Read(context.Background(), transport); err != nil { + if err := zs.Read(context.Background(), transport); err != nil { return nil, err } spans = append(spans, zs) } - if err = transport.ReadListEnd(context.Background()); err != nil { + if err := transport.ReadListEnd(context.Background()); err != nil { return nil, err } return spans, nil diff --git a/plugins/outputs/sumologic/sumologic.go b/plugins/outputs/sumologic/sumologic.go index a7f1f6466..2dc5abf4a 100644 --- a/plugins/outputs/sumologic/sumologic.go +++ b/plugins/outputs/sumologic/sumologic.go @@ -158,7 +158,7 @@ func (s *SumoLogic) writeRequestChunk(reqBody []byte) error { return err } - if err = gz.Close(); err != nil { + if err := gz.Close(); err != nil { return err } diff --git a/plugins/outputs/syslog/syslog.go b/plugins/outputs/syslog/syslog.go index 3f6244572..fbf574fad 100644 --- a/plugins/outputs/syslog/syslog.go +++ b/plugins/outputs/syslog/syslog.go @@ -105,7 +105,7 @@ func (s *Syslog) Close() error { func (s *Syslog) Write(metrics []telegraf.Metric) (err error) { if s.Conn == nil { // previous write failed with permanent error and socket was closed. - if err = s.Connect(); err != nil { + if err := s.Connect(); err != nil { return err } }