1
0
mirror of https://github.com/nginxinc/nginx-prometheus-exporter.git synced 2025-07-31 21:04:21 +03:00

Enable more linters (#622)

This commit is contained in:
Luca Comellini
2024-02-22 10:27:54 -08:00
committed by GitHub
parent e848697cec
commit d620391f71
9 changed files with 41 additions and 38 deletions

View File

@ -20,9 +20,6 @@ jobs:
lint: lint:
name: Lint name: Lint
runs-on: ubuntu-22.04 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: steps:
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
@ -34,8 +31,6 @@ jobs:
- name: Lint Code - name: Lint Code
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0 uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
with:
only-new-issues: true
actionlint: actionlint:
name: Actionlint name: Actionlint

View File

@ -31,10 +31,15 @@ linters-settings:
errcheck: errcheck:
exclude-functions: exclude-functions:
- (github.com/go-kit/log.Logger).Log - (github.com/go-kit/log.Logger).Log
govet:
check-shadowing: true
enable-all: true
linters: linters:
enable: enable:
- asciicheck - asciicheck
- bidichk
- dupword
- errcheck - errcheck
- errorlint - errorlint
- gofmt - gofmt
@ -44,18 +49,23 @@ linters:
- gosimple - gosimple
- govet - govet
- ineffassign - ineffassign
- loggercheck
- makezero - makezero
- misspell - misspell
- nilerr - nilerr
- noctx - noctx
- perfsprint
- predeclared - predeclared
- promlinter - promlinter
- revive - revive
- staticcheck - staticcheck
- tagalign
- tparallel
- typecheck - typecheck
- unconvert - unconvert
- unparam - unparam
- unused - unused
- usestdlibvars
- wastedassign - wastedassign
disable-all: true disable-all: true
issues: issues:

View File

@ -25,20 +25,10 @@ repos:
- id: requirements-txt-fixer - id: requirements-txt-fixer
- id: fix-byte-order-marker - 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 - repo: https://github.com/golangci/golangci-lint
rev: v1.56.2 rev: v1.56.2
hooks: hooks:
- id: golangci-lint - id: golangci-lint-full
args: [--new-from-patch=/tmp/diff.patch]
- repo: https://github.com/gitleaks/gitleaks - repo: https://github.com/gitleaks/gitleaks
rev: v8.18.2 rev: v8.18.2
@ -55,4 +45,4 @@ repos:
hooks: hooks:
- id: yamllint - id: yamllint
ci: ci:
skip: [golang-diff, golangci-lint, yamllint] skip: [golangci-lint-full, yamllint]

View File

@ -16,8 +16,8 @@ Reading: %d Writing: %d Waiting: %d
// NginxClient allows you to fetch NGINX metrics from the stub_status page. // NginxClient allows you to fetch NGINX metrics from the stub_status page.
type NginxClient struct { type NginxClient struct {
apiEndpoint string
httpClient *http.Client httpClient *http.Client
apiEndpoint string
} }
// StubStats represents NGINX stub_status metrics. // StubStats represents NGINX stub_status metrics.

View File

@ -9,8 +9,10 @@ func TestMergeLabels(t *testing.T) {
t.Parallel() t.Parallel()
tests := []struct { tests := []struct {
name string mapA map[string]string
mapA, mapB, want map[string]string mapB map[string]string
want map[string]string
name string
}{ }{
{ {
name: "base case", name: "base case",
@ -32,7 +34,9 @@ func TestMergeLabels(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) { if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) {
t.Errorf("mergeLabels() = %v, want %v", got, tt.want) t.Errorf("mergeLabels() = %v, want %v", got, tt.want)
} }

View File

@ -11,11 +11,11 @@ import (
// NginxCollector collects NGINX metrics. It implements prometheus.Collector interface. // NginxCollector collects NGINX metrics. It implements prometheus.Collector interface.
type NginxCollector struct { type NginxCollector struct {
upMetric prometheus.Gauge
logger log.Logger
nginxClient *client.NginxClient nginxClient *client.NginxClient
metrics map[string]*prometheus.Desc metrics map[string]*prometheus.Desc
upMetric prometheus.Gauge
mutex sync.Mutex mutex sync.Mutex
logger log.Logger
} }
// NewNginxCollector creates an NginxCollector. // NewNginxCollector creates an NginxCollector.

View File

@ -33,11 +33,11 @@ type LabelUpdater interface {
// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface. // NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
type NginxPlusCollector struct { type NginxPlusCollector struct {
upMetric prometheus.Gauge
logger log.Logger
cacheZoneMetrics map[string]*prometheus.Desc
workerMetrics map[string]*prometheus.Desc
nginxClient *plusclient.NginxClient 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 streamServerZoneMetrics map[string]*prometheus.Desc
streamZoneSyncMetrics map[string]*prometheus.Desc streamZoneSyncMetrics map[string]*prometheus.Desc
streamUpstreamMetrics map[string]*prometheus.Desc streamUpstreamMetrics map[string]*prometheus.Desc
@ -47,21 +47,21 @@ type NginxPlusCollector struct {
limitRequestMetrics map[string]*prometheus.Desc limitRequestMetrics map[string]*prometheus.Desc
limitConnectionMetrics map[string]*prometheus.Desc limitConnectionMetrics map[string]*prometheus.Desc
streamLimitConnectionMetrics map[string]*prometheus.Desc streamLimitConnectionMetrics map[string]*prometheus.Desc
cacheZoneMetrics map[string]*prometheus.Desc upstreamServerMetrics map[string]*prometheus.Desc
upMetric prometheus.Gauge upstreamMetrics map[string]*prometheus.Desc
mutex sync.Mutex streamUpstreamServerPeerLabels map[string][]string
variableLabelNames VariableLabelNames serverZoneMetrics map[string]*prometheus.Desc
upstreamServerLabels map[string][]string upstreamServerLabels map[string][]string
streamUpstreamServerLabels map[string][]string streamUpstreamServerLabels map[string][]string
serverZoneLabels map[string][]string serverZoneLabels map[string][]string
streamServerZoneLabels map[string][]string streamServerZoneLabels map[string][]string
upstreamServerPeerLabels 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 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 // UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels

View File

@ -67,7 +67,7 @@ func parseUnixSocketAddress(address string) (string, string, error) {
addressPartsLength := len(addressParts) addressPartsLength := len(addressParts)
if addressPartsLength > 3 || addressPartsLength < 1 { 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] unixSocketPath := addressParts[1]
@ -103,7 +103,7 @@ func main() {
// convert deprecated flags to new format // convert deprecated flags to new format
for i, arg := range os.Args { for i, arg := range os.Args {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { 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) 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 os.Args[i] = newArg
} }
@ -261,8 +261,8 @@ func registerCollector(logger log.Logger, transport *http.Transport,
} }
type userAgentRoundTripper struct { type userAgentRoundTripper struct {
agent string
rt http.RoundTripper rt http.RoundTripper
agent string
} }
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

View File

@ -38,7 +38,9 @@ func TestParsePositiveDuration(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := parsePositiveDuration(tt.testInput) got, err := parsePositiveDuration(tt.testInput)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("parsePositiveDuration() error = %v, wantErr %v", err, 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 { for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
socketPath, requestPath, err := parseUnixSocketAddress(tt.testInput) socketPath, requestPath, err := parseUnixSocketAddress(tt.testInput)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("parseUnixSocketAddress() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("parseUnixSocketAddress() error = %v, wantErr %v", err, tt.wantErr)