Skip to content

Commit 2877168

Browse files
authored
Add more linters (#733)
1 parent a32ca97 commit 2877168

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ that issue here in this description (not in the title of the PR).
77

88
Before creating a PR, run through this checklist and mark each as complete.
99

10-
- [ ] I have read the [CONTRIBUTING](https://github.com/nginxinc/nginx-prometheus-exporter/blob/main/CONTRIBUTING.md)
11-
guide
10+
- [ ] I have read the [CONTRIBUTING](https://github.com/nginxinc/nginx-prometheus-exporter/blob/main/CONTRIBUTING.md) guide
1211
- [ ] I have proven my fix is effective or that my feature works
1312
- [ ] I have checked that all unit tests pass after adding my changes
1413
- [ ] I have ensured the README is up to date

.golangci.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ linters-settings:
1414
- name: error-strings
1515
- name: errorf
1616
- name: exported
17-
- name: if-return
1817
- name: increment-decrement
1918
- name: indent-error-flow
2019
- name: package-comments
@@ -32,41 +31,57 @@ linters-settings:
3231
exclude-functions:
3332
- (github.com/go-kit/log.Logger).Log
3433
govet:
35-
check-shadowing: true
3634
enable-all: true
37-
3835
linters:
3936
enable:
37+
- asasalint
4038
- asciicheck
4139
- bidichk
4240
- dupword
4341
- errcheck
42+
- errname
4443
- errorlint
44+
- exportloopref
45+
- fatcontext
46+
- forcetypeassert
47+
- gocheckcompilerdirectives
48+
- godot
4549
- gofmt
4650
- gofumpt
4751
- goimports
4852
- gosec
4953
- gosimple
54+
- gosmopolitan
5055
- govet
5156
- ineffassign
57+
- intrange
5258
- loggercheck
5359
- makezero
5460
- misspell
5561
- nilerr
5662
- noctx
63+
- nolintlint
64+
- paralleltest
5765
- perfsprint
66+
- prealloc
5867
- predeclared
5968
- promlinter
69+
- reassign
6070
- revive
6171
- staticcheck
72+
- stylecheck
6273
- tagalign
74+
- tenv
75+
- thelper
6376
- tparallel
6477
- typecheck
6578
- unconvert
6679
- unparam
6780
- unused
6881
- usestdlibvars
6982
- wastedassign
83+
- whitespace
84+
- wrapcheck
7085
disable-all: true
7186
issues:
7287
max-issues-per-linter: 0

collector/nginx_plus.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/prometheus/client_golang/prometheus"
1212
)
1313

14-
// LabelUpdater updates the labels of upstream server and server zone metrics
14+
// LabelUpdater updates the labels of upstream server and server zone metrics.
1515
type LabelUpdater interface {
1616
UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels map[string][]string)
1717
DeleteUpstreamServerPeerLabels(peers []string)
@@ -64,7 +64,7 @@ type NginxPlusCollector struct {
6464
mutex sync.Mutex
6565
}
6666

67-
// UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels
67+
// UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels.
6868
func (c *NginxPlusCollector) UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels map[string][]string) {
6969
c.variableLabelsMutex.Lock()
7070
for k, v := range upstreamServerPeerLabels {
@@ -73,7 +73,7 @@ func (c *NginxPlusCollector) UpdateUpstreamServerPeerLabels(upstreamServerPeerLa
7373
c.variableLabelsMutex.Unlock()
7474
}
7575

76-
// DeleteUpstreamServerPeerLabels deletes the Upstream Server Peer Labels
76+
// DeleteUpstreamServerPeerLabels deletes the Upstream Server Peer Labels.
7777
func (c *NginxPlusCollector) DeleteUpstreamServerPeerLabels(peers []string) {
7878
c.variableLabelsMutex.Lock()
7979
for _, k := range peers {
@@ -82,7 +82,7 @@ func (c *NginxPlusCollector) DeleteUpstreamServerPeerLabels(peers []string) {
8282
c.variableLabelsMutex.Unlock()
8383
}
8484

85-
// UpdateStreamUpstreamServerPeerLabels updates the Upstream Server Peer Labels
85+
// UpdateStreamUpstreamServerPeerLabels updates the Upstream Server Peer Labels.
8686
func (c *NginxPlusCollector) UpdateStreamUpstreamServerPeerLabels(streamUpstreamServerPeerLabels map[string][]string) {
8787
c.variableLabelsMutex.Lock()
8888
for k, v := range streamUpstreamServerPeerLabels {
@@ -91,7 +91,7 @@ func (c *NginxPlusCollector) UpdateStreamUpstreamServerPeerLabels(streamUpstream
9191
c.variableLabelsMutex.Unlock()
9292
}
9393

94-
// DeleteStreamUpstreamServerPeerLabels deletes the Upstream Server Peer Labels
94+
// DeleteStreamUpstreamServerPeerLabels deletes the Upstream Server Peer Labels.
9595
func (c *NginxPlusCollector) DeleteStreamUpstreamServerPeerLabels(peers []string) {
9696
c.variableLabelsMutex.Lock()
9797
for _, k := range peers {
@@ -100,7 +100,7 @@ func (c *NginxPlusCollector) DeleteStreamUpstreamServerPeerLabels(peers []string
100100
c.variableLabelsMutex.Unlock()
101101
}
102102

103-
// UpdateUpstreamServerLabels updates the Upstream Server Labels
103+
// UpdateUpstreamServerLabels updates the Upstream Server Labels.
104104
func (c *NginxPlusCollector) UpdateUpstreamServerLabels(upstreamServerLabelValues map[string][]string) {
105105
c.variableLabelsMutex.Lock()
106106
for k, v := range upstreamServerLabelValues {
@@ -109,7 +109,7 @@ func (c *NginxPlusCollector) UpdateUpstreamServerLabels(upstreamServerLabelValue
109109
c.variableLabelsMutex.Unlock()
110110
}
111111

112-
// DeleteUpstreamServerLabels deletes the Upstream Server Labels
112+
// DeleteUpstreamServerLabels deletes the Upstream Server Labels.
113113
func (c *NginxPlusCollector) DeleteUpstreamServerLabels(upstreamNames []string) {
114114
c.variableLabelsMutex.Lock()
115115
for _, k := range upstreamNames {
@@ -118,7 +118,7 @@ func (c *NginxPlusCollector) DeleteUpstreamServerLabels(upstreamNames []string)
118118
c.variableLabelsMutex.Unlock()
119119
}
120120

121-
// UpdateStreamUpstreamServerLabels updates the Upstream Server Labels
121+
// UpdateStreamUpstreamServerLabels updates the Upstream Server Labels.
122122
func (c *NginxPlusCollector) UpdateStreamUpstreamServerLabels(streamUpstreamServerLabelValues map[string][]string) {
123123
c.variableLabelsMutex.Lock()
124124
for k, v := range streamUpstreamServerLabelValues {
@@ -127,7 +127,7 @@ func (c *NginxPlusCollector) UpdateStreamUpstreamServerLabels(streamUpstreamServ
127127
c.variableLabelsMutex.Unlock()
128128
}
129129

130-
// DeleteStreamUpstreamServerLabels deletes the Upstream Server Labels
130+
// DeleteStreamUpstreamServerLabels deletes the Upstream Server Labels.
131131
func (c *NginxPlusCollector) DeleteStreamUpstreamServerLabels(streamUpstreamNames []string) {
132132
c.variableLabelsMutex.Lock()
133133
for _, k := range streamUpstreamNames {
@@ -136,7 +136,7 @@ func (c *NginxPlusCollector) DeleteStreamUpstreamServerLabels(streamUpstreamName
136136
c.variableLabelsMutex.Unlock()
137137
}
138138

139-
// UpdateServerZoneLabels updates the Server Zone Labels
139+
// UpdateServerZoneLabels updates the Server Zone Labels.
140140
func (c *NginxPlusCollector) UpdateServerZoneLabels(serverZoneLabelValues map[string][]string) {
141141
c.variableLabelsMutex.Lock()
142142
for k, v := range serverZoneLabelValues {
@@ -145,7 +145,7 @@ func (c *NginxPlusCollector) UpdateServerZoneLabels(serverZoneLabelValues map[st
145145
c.variableLabelsMutex.Unlock()
146146
}
147147

148-
// DeleteServerZoneLabels deletes the Server Zone Labels
148+
// DeleteServerZoneLabels deletes the Server Zone Labels.
149149
func (c *NginxPlusCollector) DeleteServerZoneLabels(zoneNames []string) {
150150
c.variableLabelsMutex.Lock()
151151
for _, k := range zoneNames {
@@ -154,7 +154,7 @@ func (c *NginxPlusCollector) DeleteServerZoneLabels(zoneNames []string) {
154154
c.variableLabelsMutex.Unlock()
155155
}
156156

157-
// UpdateStreamServerZoneLabels updates the Stream Server Zone Labels
157+
// UpdateStreamServerZoneLabels updates the Stream Server Zone Labels.
158158
func (c *NginxPlusCollector) UpdateStreamServerZoneLabels(streamServerZoneLabelValues map[string][]string) {
159159
c.variableLabelsMutex.Lock()
160160
for k, v := range streamServerZoneLabelValues {
@@ -163,7 +163,7 @@ func (c *NginxPlusCollector) UpdateStreamServerZoneLabels(streamServerZoneLabelV
163163
c.variableLabelsMutex.Unlock()
164164
}
165165

166-
// DeleteStreamServerZoneLabels deletes the Stream Server Zone Labels
166+
// DeleteStreamServerZoneLabels deletes the Stream Server Zone Labels.
167167
func (c *NginxPlusCollector) DeleteStreamServerZoneLabels(zoneNames []string) {
168168
c.variableLabelsMutex.Lock()
169169
for _, k := range zoneNames {
@@ -172,7 +172,7 @@ func (c *NginxPlusCollector) DeleteStreamServerZoneLabels(zoneNames []string) {
172172
c.variableLabelsMutex.Unlock()
173173
}
174174

175-
// UpdateCacheZoneLabels updates the Upstream Cache Zone labels
175+
// UpdateCacheZoneLabels updates the Upstream Cache Zone labels.
176176
func (c *NginxPlusCollector) UpdateCacheZoneLabels(cacheZoneLabelValues map[string][]string) {
177177
c.variableLabelsMutex.Lock()
178178
for k, v := range cacheZoneLabelValues {
@@ -181,7 +181,7 @@ func (c *NginxPlusCollector) UpdateCacheZoneLabels(cacheZoneLabelValues map[stri
181181
c.variableLabelsMutex.Unlock()
182182
}
183183

184-
// DeleteCacheZoneLabels deletes the Cache Zone Labels
184+
// DeleteCacheZoneLabels deletes the Cache Zone Labels.
185185
func (c *NginxPlusCollector) DeleteCacheZoneLabels(cacheZoneNames []string) {
186186
c.variableLabelsMutex.Lock()
187187
for _, k := range cacheZoneNames {
@@ -226,7 +226,7 @@ func (c *NginxPlusCollector) getStreamUpstreamServerPeerLabelValues(peer string)
226226
return c.streamUpstreamServerPeerLabels[peer]
227227
}
228228

229-
// UpdateWorkerLabels updates the Worker Labels
229+
// UpdateWorkerLabels updates the Worker Labels.
230230
func (c *NginxPlusCollector) UpdateWorkerLabels(workerLabelValues map[string][]string) {
231231
c.variableLabelsMutex.Lock()
232232
for k, v := range workerLabelValues {
@@ -235,7 +235,7 @@ func (c *NginxPlusCollector) UpdateWorkerLabels(workerLabelValues map[string][]s
235235
c.variableLabelsMutex.Unlock()
236236
}
237237

238-
// DeleteWorkerLabels deletes the Worker Labels
238+
// DeleteWorkerLabels deletes the Worker Labels.
239239
func (c *NginxPlusCollector) DeleteWorkerLabels(id []string) {
240240
c.variableLabelsMutex.Lock()
241241
for _, k := range id {
@@ -250,7 +250,7 @@ func (c *NginxPlusCollector) getWorkerLabelValues(id string) []string {
250250
return c.workerLabels[id]
251251
}
252252

253-
// VariableLabelNames holds all the variable label names for the different metrics
253+
// VariableLabelNames holds all the variable label names for the different metrics.
254254
type VariableLabelNames struct {
255255
UpstreamServerVariableLabelNames []string
256256
ServerZoneVariableLabelNames []string
@@ -262,7 +262,7 @@ type VariableLabelNames struct {
262262
WorkerPIDVariableLabelNames []string
263263
}
264264

265-
// NewVariableLabelNames NewVariableLabels creates a new struct for VariableNames for the collector
265+
// NewVariableLabelNames NewVariableLabels creates a new struct for VariableNames for the collector.
266266
func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZoneVariableLabelNames []string, upstreamServerPeerVariableLabelNames []string,
267267
streamUpstreamServerVariableLabelNames []string, streamServerZoneLabels []string, streamUpstreamServerPeerVariableLabelNames []string, cacheZoneLabelNames []string, workerPIDVariableLabelNames []string,
268268
) VariableLabelNames {
@@ -1219,7 +1219,6 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
12191219
}
12201220

12211221
for name, zone := range stats.Caches {
1222-
12231222
var cold float64
12241223
if zone.Cold {
12251224
cold = 1.0

exporter.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
3434
)
3535

36-
// positiveDuration is a wrapper of time.Duration to ensure only positive values are accepted
36+
// positiveDuration is a wrapper of time.Duration to ensure only positive values are accepted.
3737
type positiveDuration struct{ time.Duration }
3838

3939
func (pd *positiveDuration) Set(s string) error {
@@ -49,7 +49,7 @@ func (pd *positiveDuration) Set(s string) error {
4949
func parsePositiveDuration(s string) (positiveDuration, error) {
5050
dur, err := time.ParseDuration(s)
5151
if err != nil {
52-
return positiveDuration{}, err
52+
return positiveDuration{}, fmt.Errorf("failed to parse duration %q: %w", s, err)
5353
}
5454
if dur < 0 {
5555
return positiveDuration{}, fmt.Errorf("negative duration %v is not valid", dur)
@@ -82,7 +82,7 @@ func parseUnixSocketAddress(address string) (string, string, error) {
8282
var (
8383
constLabels = map[string]string{}
8484

85-
// Command-line flags
85+
// Command-line flags.
8686
webConfig = kingpinflag.AddFlags(kingpin.CommandLine, ":9113")
8787
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").Envar("TELEMETRY_PATH").String()
8888
nginxPlus = kingpin.Flag("nginx.plus", "Start the exporter for NGINX Plus. By default, the exporter is started for NGINX.").Default("false").Envar("NGINX_PLUS").Bool()
@@ -92,7 +92,7 @@ var (
9292
sslClientCert = kingpin.Flag("nginx.ssl-client-cert", "Path to the PEM encoded client certificate file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_CERT").String()
9393
sslClientKey = kingpin.Flag("nginx.ssl-client-key", "Path to the PEM encoded client certificate key file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_KEY").String()
9494

95-
// Custom command-line flags
95+
// Custom command-line flags.
9696
timeout = createPositiveDurationFlag(kingpin.Flag("nginx.timeout", "A timeout for scraping metrics from NGINX or NGINX Plus.").Default("5s").Envar("TIMEOUT").HintOptions("5s", "10s", "30s", "1m", "5m"))
9797
)
9898

@@ -269,7 +269,11 @@ type userAgentRoundTripper struct {
269269
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
270270
req = cloneRequest(req)
271271
req.Header.Set("User-Agent", rt.agent)
272-
return rt.rt.RoundTrip(req)
272+
roundTrip, err := rt.rt.RoundTrip(req)
273+
if err != nil {
274+
return nil, fmt.Errorf("round trip failed: %w", err)
275+
}
276+
return roundTrip, nil
273277
}
274278

275279
func cloneRequest(req *http.Request) *http.Request {
@@ -287,7 +291,7 @@ func cloneRequest(req *http.Request) *http.Request {
287291
}
288292

289293
// addMissingEnvironmentFlags sets Envar on any flag which has
290-
// the "web." prefix which doesn't already have an Envar set
294+
// the "web." prefix which doesn't already have an Envar set.
291295
func addMissingEnvironmentFlags(ka *kingpin.Application) {
292296
for _, f := range ka.Model().FlagGroupModel.Flags {
293297
if strings.HasPrefix(f.Name, "web.") && f.Envar == "" {

exporter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func TestParseUnixSocketAddress(t *testing.T) {
112112
}
113113

114114
func TestAddMissingEnvironmentFlags(t *testing.T) {
115+
t.Parallel()
115116
expectedMatches := map[string]string{
116117
"non-matching-flag": "",
117118
"web.missing-env": "MISSING_ENV",
@@ -143,6 +144,7 @@ func TestAddMissingEnvironmentFlags(t *testing.T) {
143144
}
144145

145146
func TestConvertFlagToEnvar(t *testing.T) {
147+
t.Parallel()
146148
cases := []struct {
147149
input string
148150
output string

0 commit comments

Comments
 (0)