Skip to content

Commit 4a54705

Browse files
authored
Update golangci-lint config for v2 (#1034)
This brings the common files in line with other prometheus repos. Most of these settings are coppied from prometheus/prometheus. Where it was a small change, I have enabled the new linte rs and made the fixes. The remaining linters that are commented out deserve to be enabled and the fixes applied as independent changes. Signed-off-by: Joe Adams <[email protected]>
1 parent 25c76fa commit 4a54705

20 files changed

+144
-67
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
3434
if: github.repository == 'prometheus/snmp_exporter'
3535
- name: Lint
36-
uses: golangci/golangci-lint-action@4696ba8babb6127d732c3c6dde519db15edab9ea # v6.5.1
36+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # v7.0.0
3737
with:
3838
args: --verbose
39-
version: v1.64.6
39+
version: v2.0.2

.golangci.yml

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,124 @@
11
---
2+
version: "2"
3+
4+
formatters:
5+
enable:
6+
- gci
7+
- gofumpt
8+
settings:
9+
gci:
10+
sections:
11+
- standard
12+
- prefix(github.com/prometheus-community/elasticsearch_exporter)
13+
- default
14+
215
linters:
316
enable:
17+
- depguard
18+
# TODO(@sysadmind): Enable and fix the issues.
19+
# - errorlint
20+
- exptostd
21+
# TODO(@sysadmind): Enable and fix the issues.
22+
# - gocritic
23+
# - godot
24+
- loggercheck
25+
# TODO(@sysadmind): Enable and fix the issues.
26+
# - misspell
27+
- nilnesserr
28+
- nolintlint
29+
# TODO(@sysadmind): Enable and fix the issues.
30+
# - perfsprint
31+
- predeclared
432
- revive
533
- sloglint
34+
- testifylint
35+
- unconvert
36+
- unused
37+
- usestdlibvars
38+
- whitespace
39+
exclusions:
40+
rules:
41+
# Disable errcheck for test files.
42+
- linters:
43+
- errcheck
44+
path: _test.go
645

7-
issues:
8-
exclude-rules:
9-
- path: _test.go
10-
linters:
11-
- errcheck
46+
# Disable errcheck rule for some specific functions.
47+
- linters:
48+
- errcheck
49+
# Taken from the default exclusions in v1.
50+
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
1251

13-
linters-settings:
14-
revive:
15-
rules:
16-
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
17-
- name: unused-parameter
18-
severity: warning
19-
disabled: true
52+
settings:
53+
revive:
54+
rules:
55+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
56+
- name: blank-imports
57+
- name: comment-spacings
58+
- name: context-as-argument
59+
arguments:
60+
# Allow functions with test or bench signatures.
61+
- allowTypesBefore: '*testing.T,testing.TB'
62+
- name: context-keys-type
63+
- name: dot-imports
64+
- name: early-return
65+
arguments:
66+
- "preserveScope"
67+
# A lot of false positives: incorrectly identifies channel draining as "empty code block".
68+
# See https://github.com/mgechev/revive/issues/386
69+
- name: empty-block
70+
disabled: true
71+
- name: error-naming
72+
- name: error-return
73+
- name: error-strings
74+
- name: errorf
75+
# TODO(@sysadmind): Enable and fix the issues.
76+
# - name: exported
77+
- name: increment-decrement
78+
- name: indent-error-flow
79+
arguments:
80+
- "preserveScope"
81+
- name: package-comments
82+
# TODO(beorn7/sysadmind): Currently, we have a lot of missing package doc comments. Maybe we should have them.
83+
disabled: true
84+
- name: range
85+
- name: receiver-naming
86+
- name: redefines-builtin-id
87+
- name: superfluous-else
88+
arguments:
89+
- "preserveScope"
90+
- name: time-naming
91+
# TODO(@sysadmind): Enable and fix the issues.
92+
# - name: unexported-return
93+
- name: unreachable-code
94+
- name: unused-parameter
95+
severity: warning
96+
disabled: true
97+
- name: var-declaration
98+
- name: var-naming
99+
100+
depguard:
101+
rules:
102+
main:
103+
deny:
104+
- pkg: "sync/atomic"
105+
desc: "Use go.uber.org/atomic instead of sync/atomic"
106+
- pkg: "github.com/stretchr/testify/assert"
107+
desc: "Use github.com/stretchr/testify/require instead of github.com/stretchr/testify/assert"
108+
- pkg: "github.com/go-kit/kit/log"
109+
desc: "Use github.com/go-kit/log instead of github.com/go-kit/kit/log"
110+
- pkg: "io/ioutil"
111+
desc: "Use corresponding 'os' or 'io' functions instead."
112+
- pkg: "regexp"
113+
desc: "Use github.com/grafana/regexp instead of regexp"
114+
- pkg: "github.com/pkg/errors"
115+
desc: "Use 'errors' or 'fmt' instead of github.com/pkg/errors"
116+
- pkg: "gzip"
117+
desc: "Use github.com/klauspost/compress instead of gzip"
118+
- pkg: "zlib"
119+
desc: "Use github.com/klauspost/compress instead of zlib"
120+
- pkg: "golang.org/x/exp/slices"
121+
desc: "Use 'slices' instead."
122+
issues:
123+
max-issues-per-linter: 0
124+
max-same-issues: 0

Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
6161
SKIP_GOLANGCI_LINT :=
6262
GOLANGCI_LINT :=
6363
GOLANGCI_LINT_OPTS ?=
64-
GOLANGCI_LINT_VERSION ?= v1.64.6
64+
GOLANGCI_LINT_VERSION ?= v2.0.2
6565
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64.
6666
# windows isn't included here because of the path separator being different.
6767
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))

collector/cluster_settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh
151151
q.Set("include_defaults", "true")
152152
u.RawQuery = q.Encode()
153153

154-
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
154+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
155155
if err != nil {
156156
return err
157157
}

collector/data_stream.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ func (ds *DataStream) Update(ctx context.Context, ch chan<- prometheus.Metric) e
113113
float64(dataStream.StoreSizeBytes),
114114
dataStream.DataStream,
115115
)
116-
117116
}
118117

119118
return nil
120-
121119
}

collector/data_stream_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
)
2828

2929
func TestDataStream(t *testing.T) {
30-
3130
tests := []struct {
3231
name string
3332
file string

collector/ilm.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (i *ILM) Update(ctx context.Context, ch chan<- prometheus.Metric) error {
122122
bool2Float(statusActive),
123123
status,
124124
)
125-
126125
}
127126

128127
return nil

collector/ilm_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ func TestILM(t *testing.T) {
5454
indexF, err := os.Open(path.Join("../fixtures/ilm_indices", tt.file))
5555
if err != nil {
5656
t.Fatal(err)
57-
5857
}
5958
defer indexF.Close()
6059

6160
statusF, err := os.Open(path.Join("../fixtures/ilm_status", tt.file))
6261
if err != nil {
6362
t.Fatal(err)
64-
6563
}
6664
defer statusF.Close()
6765

@@ -75,7 +73,6 @@ func TestILM(t *testing.T) {
7573
})
7674

7775
sm.ServeHTTP(w, r)
78-
7976
}))
8077
defer ts.Close()
8178

collector/indices.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
"sort"
2323
"strconv"
2424

25-
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
2625
"github.com/prometheus/client_golang/prometheus"
26+
27+
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
2728
)
2829

2930
var (
@@ -448,7 +449,6 @@ type Indices struct {
448449

449450
// NewIndices defines Indices Prometheus metrics
450451
func NewIndices(logger *slog.Logger, client *http.Client, url *url.URL, shards bool, includeAliases bool) *Indices {
451-
452452
indices := &Indices{
453453
logger: logger,
454454
client: client,
@@ -661,7 +661,6 @@ func (i *Indices) Collect(ch chan<- prometheus.Metric) {
661661

662662
// Index stats
663663
for indexName, indexStats := range indexStatsResp.Indices {
664-
665664
ch <- prometheus.MustNewConstMetric(
666665
indicesDocsPrimary,
667666
prometheus.GaugeValue,

collector/indices_mappings.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ import (
2525
"github.com/prometheus/client_golang/prometheus"
2626
)
2727

28-
var (
29-
defaultIndicesMappingsLabels = []string{"index"}
30-
)
28+
var defaultIndicesMappingsLabels = []string{"index"}
3129

3230
type indicesMappingsMetric struct {
3331
Type prometheus.ValueType
@@ -72,7 +70,6 @@ func NewIndicesMappings(logger *slog.Logger, client *http.Client, url *url.URL)
7270
func countFieldsRecursive(properties IndexMappingProperties, fieldCounter float64) float64 {
7371
// iterate over all properties
7472
for _, property := range properties {
75-
7673
if property.Type != nil && *property.Type != "object" {
7774
// property has a type set - counts as a field unless the value is object
7875
// as the recursion below will handle counting that

0 commit comments

Comments
 (0)