From d620391f71dd875b02cbe16b5878a3adefad1c66 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Thu, 22 Feb 2024 10:27:54 -0800 Subject: [PATCH] Enable more linters (#622) --- .github/workflows/lint.yml | 5 ----- .golangci.yml | 10 ++++++++++ .pre-commit-config.yaml | 14 ++------------ client/nginx.go | 2 +- collector/helper_test.go | 8 ++++++-- collector/nginx.go | 4 ++-- collector/nginx_plus.go | 26 +++++++++++++------------- exporter.go | 6 +++--- exporter_test.go | 4 ++++ 9 files changed, 41 insertions(+), 38 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c1320e2..1edcaf0 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,9 +20,6 @@ jobs: lint: name: Lint runs-on: ubuntu-22.04 - permissions: - contents: read # for actions/checkout to fetch code - pull-requests: read # for golangci/golangci-lint-action to fetch pull requests steps: - name: Checkout Repository uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 @@ -34,8 +31,6 @@ jobs: - name: Lint Code uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0 - with: - only-new-issues: true actionlint: name: Actionlint diff --git a/.golangci.yml b/.golangci.yml index 815a592..47dfa3a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -31,10 +31,15 @@ linters-settings: errcheck: exclude-functions: - (github.com/go-kit/log.Logger).Log + govet: + check-shadowing: true + enable-all: true linters: enable: - asciicheck + - bidichk + - dupword - errcheck - errorlint - gofmt @@ -44,18 +49,23 @@ linters: - gosimple - govet - ineffassign + - loggercheck - makezero - misspell - nilerr - noctx + - perfsprint - predeclared - promlinter - revive - staticcheck + - tagalign + - tparallel - typecheck - unconvert - unparam - unused + - usestdlibvars - wastedassign disable-all: true issues: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 337446f..1a4df5f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,20 +25,10 @@ repos: - id: requirements-txt-fixer - id: fix-byte-order-marker - - repo: local - hooks: - - id: golang-diff - name: create-go-diff - entry: bash -c 'git diff -p origin/main > /tmp/diff.patch' - language: system - types: [go] - pass_filenames: false - - repo: https://github.com/golangci/golangci-lint rev: v1.56.2 hooks: - - id: golangci-lint - args: [--new-from-patch=/tmp/diff.patch] + - id: golangci-lint-full - repo: https://github.com/gitleaks/gitleaks rev: v8.18.2 @@ -55,4 +45,4 @@ repos: hooks: - id: yamllint ci: - skip: [golang-diff, golangci-lint, yamllint] + skip: [golangci-lint-full, yamllint] diff --git a/client/nginx.go b/client/nginx.go index 327d758..c47dfec 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -16,8 +16,8 @@ Reading: %d Writing: %d Waiting: %d // NginxClient allows you to fetch NGINX metrics from the stub_status page. type NginxClient struct { - apiEndpoint string httpClient *http.Client + apiEndpoint string } // StubStats represents NGINX stub_status metrics. diff --git a/collector/helper_test.go b/collector/helper_test.go index 31cb022..2678095 100644 --- a/collector/helper_test.go +++ b/collector/helper_test.go @@ -9,8 +9,10 @@ func TestMergeLabels(t *testing.T) { t.Parallel() tests := []struct { - name string - mapA, mapB, want map[string]string + mapA map[string]string + mapB map[string]string + want map[string]string + name string }{ { name: "base case", @@ -32,7 +34,9 @@ func TestMergeLabels(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) { t.Errorf("mergeLabels() = %v, want %v", got, tt.want) } diff --git a/collector/nginx.go b/collector/nginx.go index cc86034..1b1b0d1 100644 --- a/collector/nginx.go +++ b/collector/nginx.go @@ -11,11 +11,11 @@ import ( // NginxCollector collects NGINX metrics. It implements prometheus.Collector interface. type NginxCollector struct { + upMetric prometheus.Gauge + logger log.Logger nginxClient *client.NginxClient metrics map[string]*prometheus.Desc - upMetric prometheus.Gauge mutex sync.Mutex - logger log.Logger } // NewNginxCollector creates an NginxCollector. diff --git a/collector/nginx_plus.go b/collector/nginx_plus.go index 1938d34..736d8af 100644 --- a/collector/nginx_plus.go +++ b/collector/nginx_plus.go @@ -33,11 +33,11 @@ type LabelUpdater interface { // NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface. type NginxPlusCollector struct { + upMetric prometheus.Gauge + logger log.Logger + cacheZoneMetrics map[string]*prometheus.Desc + workerMetrics map[string]*prometheus.Desc nginxClient *plusclient.NginxClient - totalMetrics map[string]*prometheus.Desc - serverZoneMetrics map[string]*prometheus.Desc - upstreamMetrics map[string]*prometheus.Desc - upstreamServerMetrics map[string]*prometheus.Desc streamServerZoneMetrics map[string]*prometheus.Desc streamZoneSyncMetrics map[string]*prometheus.Desc streamUpstreamMetrics map[string]*prometheus.Desc @@ -47,21 +47,21 @@ type NginxPlusCollector struct { limitRequestMetrics map[string]*prometheus.Desc limitConnectionMetrics map[string]*prometheus.Desc streamLimitConnectionMetrics map[string]*prometheus.Desc - cacheZoneMetrics map[string]*prometheus.Desc - upMetric prometheus.Gauge - mutex sync.Mutex - variableLabelNames VariableLabelNames + upstreamServerMetrics map[string]*prometheus.Desc + upstreamMetrics map[string]*prometheus.Desc + streamUpstreamServerPeerLabels map[string][]string + serverZoneMetrics map[string]*prometheus.Desc upstreamServerLabels map[string][]string streamUpstreamServerLabels map[string][]string serverZoneLabels map[string][]string streamServerZoneLabels map[string][]string upstreamServerPeerLabels map[string][]string - streamUpstreamServerPeerLabels map[string][]string - cacheZoneLabels map[string][]string - variableLabelsMutex sync.RWMutex - logger log.Logger workerLabels map[string][]string - workerMetrics map[string]*prometheus.Desc + cacheZoneLabels map[string][]string + totalMetrics map[string]*prometheus.Desc + variableLabelNames VariableLabelNames + variableLabelsMutex sync.RWMutex + mutex sync.Mutex } // UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels diff --git a/exporter.go b/exporter.go index 93dd8e1..72c46d0 100644 --- a/exporter.go +++ b/exporter.go @@ -67,7 +67,7 @@ func parseUnixSocketAddress(address string) (string, string, error) { addressPartsLength := len(addressParts) if addressPartsLength > 3 || addressPartsLength < 1 { - return "", "", fmt.Errorf("address for unix domain socket has wrong format") + return "", "", errors.New("address for unix domain socket has wrong format") } unixSocketPath := addressParts[1] @@ -103,7 +103,7 @@ func main() { // convert deprecated flags to new format for i, arg := range os.Args { if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { - newArg := fmt.Sprintf("-%s", arg) + newArg := "-" + arg fmt.Printf("the flag format is deprecated and will be removed in a future release, please use the new format: %s\n", newArg) os.Args[i] = newArg } @@ -261,8 +261,8 @@ func registerCollector(logger log.Logger, transport *http.Transport, } type userAgentRoundTripper struct { - agent string rt http.RoundTripper + agent string } func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { diff --git a/exporter_test.go b/exporter_test.go index eba5928..3eefc73 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -38,7 +38,9 @@ func TestParsePositiveDuration(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() got, err := parsePositiveDuration(tt.testInput) if (err != nil) != tt.wantErr { t.Errorf("parsePositiveDuration() error = %v, wantErr %v", err, tt.wantErr) @@ -91,7 +93,9 @@ func TestParseUnixSocketAddress(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() socketPath, requestPath, err := parseUnixSocketAddress(tt.testInput) if (err != nil) != tt.wantErr { t.Errorf("parseUnixSocketAddress() error = %v, wantErr %v", err, tt.wantErr)