diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 5342cbe086..3893ef86b1 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -33,7 +33,7 @@ jobs: run: sudo apt-get update && sudo apt-get -y install libsnmp-dev if: github.repository == 'prometheus/snmp_exporter' - name: Lint - uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # v6.5.2 + uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # v7.0.0 with: args: --verbose - version: v1.64.6 + version: v2.0.2 diff --git a/.golangci.yml b/.golangci.yml index 8731aefdf1..26d1f5ad57 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,32 +1,48 @@ +version: "2" linters: enable: - depguard - - goimports - misspell - revive - -issues: - exclude-rules: - - path: _test.go - linters: - - errcheck - -linters-settings: - depguard: - rules: - no_exec_policy: - files: - - "!$test" - deny: - - pkg: "os/exec" - desc: "Using os/exec to run sub processes it not allowed by policy" - errcheck: - exclude-functions: + settings: + depguard: + rules: + no_exec_policy: + files: + - '!$test' + deny: + - pkg: os/exec + desc: Using os/exec to run sub processes it not allowed by policy + errcheck: # Used in HTTP handlers, any error is handled by the server itself. - - (net/http.ResponseWriter).Write - revive: + exclude-functions: + - (net/http.ResponseWriter).Write + revive: + rules: + - name: unused-parameter + severity: warning + disabled: true + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling rules: - # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter - - name: unused-parameter - severity: warning - disabled: true + - linters: + - errcheck + path: _test.go + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - goimports + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/Makefile.common b/Makefile.common index 8cb3838598..81bad5f42d 100644 --- a/Makefile.common +++ b/Makefile.common @@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_ SKIP_GOLANGCI_LINT := GOLANGCI_LINT := GOLANGCI_LINT_OPTS ?= -GOLANGCI_LINT_VERSION ?= v1.64.6 +GOLANGCI_LINT_VERSION ?= v2.0.2 # golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64. # windows isn't included here because of the path separator being different. ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin)) diff --git a/collector/boot_time_bsd.go b/collector/boot_time_bsd.go index d632fef094..49b4145cff 100644 --- a/collector/boot_time_bsd.go +++ b/collector/boot_time_bsd.go @@ -18,9 +18,10 @@ package collector import ( + "log/slog" + "github.com/prometheus/client_golang/prometheus" "golang.org/x/sys/unix" - "log/slog" ) type bootTimeCollector struct { diff --git a/collector/btrfs_linux.go b/collector/btrfs_linux.go index 2dbdf5aa35..acb7e64c85 100644 --- a/collector/btrfs_linux.go +++ b/collector/btrfs_linux.go @@ -69,7 +69,7 @@ func (c *btrfsCollector) Update(ch chan<- prometheus.Metric) error { for _, s := range stats { // match up procfs and ioctl info by filesystem UUID (without dashes) - var fsUUID = strings.Replace(s.UUID, "-", "", -1) + var fsUUID = strings.ReplaceAll(s.UUID, "-", "") ioctlStats := ioctlStatsMap[fsUUID] c.updateBtrfsStats(ch, s, ioctlStats) } diff --git a/collector/cpu_linux.go b/collector/cpu_linux.go index 8ca703657b..46ab0bf531 100644 --- a/collector/cpu_linux.go +++ b/collector/cpu_linux.go @@ -87,9 +87,9 @@ func NewCPUCollector(logger *slog.Logger) (Collector, error) { isolcpus, err := sfs.IsolatedCPUs() if err != nil { if !os.IsNotExist(err) { - return nil, fmt.Errorf("Unable to get isolated cpus: %w", err) + return nil, fmt.Errorf("couldn't get isolated cpus: %w", err) } - logger.Debug("Could not open isolated file", "error", err) + logger.Debug("couldn't open isolated file", "error", err) } c := &cpuCollector{ diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index 98e66dbc20..e7618ac53d 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -212,16 +212,18 @@ func (e *EthtoolFixture) LinkInfo(intf string) (ethtool.EthtoolCmd, error) { items := strings.Split(line, ": ") if items[0] == "Supported pause frame use" { - if items[1] == "Symmetric" { + switch items[1] { + case "Symmetric": res.Supported |= (1 << unix.ETHTOOL_LINK_MODE_Pause_BIT) - } else if items[1] == "Receive-only" { + case "Receive-only": res.Supported |= (1 << unix.ETHTOOL_LINK_MODE_Asym_Pause_BIT) } } if items[0] == "Advertised pause frame use" { - if items[1] == "Symmetric" { + switch items[1] { + case "Symmetric": res.Advertising |= (1 << unix.ETHTOOL_LINK_MODE_Pause_BIT) - } else if items[1] == "Receive-only" { + case "Receive-only": res.Advertising |= (1 << unix.ETHTOOL_LINK_MODE_Asym_Pause_BIT) } } diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go index 3a7bda4d24..127a3be1d6 100644 --- a/collector/filesystem_linux.go +++ b/collector/filesystem_linux.go @@ -215,8 +215,8 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) { // Ensure we handle the translation of \040 and \011 // as per fstab(5). - parts[4] = strings.Replace(parts[4], "\\040", " ", -1) - parts[4] = strings.Replace(parts[4], "\\011", "\t", -1) + parts[4] = strings.ReplaceAll(parts[4], "\\040", " ") + parts[4] = strings.ReplaceAll(parts[4], "\\011", "\t") filesystems = append(filesystems, filesystemLabels{ device: parts[m+3], diff --git a/collector/meminfo_darwin.go b/collector/meminfo_darwin.go index b5b10b4b92..26a6a1ac2d 100644 --- a/collector/meminfo_darwin.go +++ b/collector/meminfo_darwin.go @@ -52,7 +52,7 @@ func (c *meminfoCollector) getMemInfo() (map[string]float64, error) { &infoCount, ) if ret != C.KERN_SUCCESS { - return nil, fmt.Errorf("Couldn't get memory statistics, host_statistics returned %d", ret) + return nil, fmt.Errorf("couldn't get memory statistics, host_statistics returned %d", ret) } totalb, err := unix.Sysctl("hw.memsize") if err != nil { diff --git a/collector/meminfo_linux.go b/collector/meminfo_linux.go index 98d5a5c06b..62807c2fe9 100644 --- a/collector/meminfo_linux.go +++ b/collector/meminfo_linux.go @@ -44,7 +44,7 @@ func NewMeminfoCollector(logger *slog.Logger) (Collector, error) { func (c *meminfoCollector) getMemInfo() (map[string]float64, error) { meminfo, err := c.fs.Meminfo() if err != nil { - return nil, fmt.Errorf("Failed to get memory info: %s", err) + return nil, fmt.Errorf("failed to get memory info: %s", err) } metrics := make(map[string]float64) diff --git a/collector/perf_linux_test.go b/collector/perf_linux_test.go index e385851153..1d39e8a95d 100644 --- a/collector/perf_linux_test.go +++ b/collector/perf_linux_test.go @@ -33,10 +33,10 @@ func canTestPerf(t *testing.T) { if err != nil { t.Skip("Procfs not mounted, skipping perf tests") } - paranoidStr := strings.Replace(string(paranoidBytes), "\n", "", -1) + paranoidStr := strings.ReplaceAll(string(paranoidBytes), "\n", "") paranoid, err := strconv.Atoi(paranoidStr) if err != nil { - t.Fatalf("Expected perf_event_paranoid to be an int, got: %s", paranoidStr) + t.Fatalf("expected perf_event_paranoid to be an int, got: %s", paranoidStr) } if paranoid >= 1 { t.Skip("Skipping perf tests, set perf_event_paranoid to 0") diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 1c290377e6..f12b6ef896 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -122,11 +122,12 @@ func TestSystemdSummary(t *testing.T) { summary := summarizeUnits(fixtures[0]) for _, state := range unitStatesName { - if state == "inactive" { + switch state { + case "inactive": testSummaryHelper(t, state, summary[state], 3.0) - } else if state == "active" { + case "active": testSummaryHelper(t, state, summary[state], 1.0) - } else { + default: testSummaryHelper(t, state, summary[state], 0.0) } } diff --git a/collector/zfs_linux.go b/collector/zfs_linux.go index 4baf2b3569..ff544a7929 100644 --- a/collector/zfs_linux.go +++ b/collector/zfs_linux.go @@ -435,5 +435,5 @@ type zfsSysctl string func (s zfsSysctl) metricName() string { parts := strings.Split(string(s), ".") - return strings.Replace(parts[len(parts)-1], "-", "_", -1) + return strings.ReplaceAll(parts[len(parts)-1], "-", "_") } diff --git a/node_exporter.go b/node_exporter.go index 88441e1e21..22939cbc56 100644 --- a/node_exporter.go +++ b/node_exporter.go @@ -112,7 +112,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { h.logger.Warn("Couldn't create filtered metrics handler:", "err", err) w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) + fmt.Fprintf(w, "Couldn't create filtered metrics handler: %s", err) return } filteredHandler.ServeHTTP(w, r)