Skip to content

Commit 2cfd1eb

Browse files
authored
Enable same linters as the Prometheus repo itself (prometheus#1056)
* Add gofumpt to github workflow & fix all files for it Signed-off-by: sazary <[email protected]> * Add goimports to golangci & fix it's issues Signed-off-by: sazary <[email protected]> * Add revive to golangci & fix it's issues Signed-off-by: sazary <[email protected]> * Add errcheck & misspell to golangci and fix their issues Signed-off-by: sazary <[email protected]> * Add govet & gosimple to golangci and fix their issues Signed-off-by: sazary <[email protected]> * Enable all default linters of golangci Signed-off-by: sazary <[email protected]>
1 parent ebd77f0 commit 2cfd1eb

39 files changed

+179
-146
lines changed

.github/workflows/golangci-lint.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
name: golangci-lint
23
on:
34
push:

.golangci.yml

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1-
# Run only staticcheck for now. Additional linters will be enabled one-by-one.
1+
---
2+
3+
run:
4+
deadline: 5m
5+
6+
output:
7+
sort-results: true
8+
29
linters:
310
enable:
4-
- staticcheck
5-
disable-all: true
11+
- gofumpt
12+
- goimports
13+
- revive
14+
- misspell
15+
16+
issues:
17+
max-same-issues: 0
18+
exclude-rules:
19+
- path: _test.go
20+
linters:
21+
- errcheck
22+
- govet
23+
- structcheck
24+
25+
linters-settings:
26+
errcheck:
27+
exclude: scripts/errcheck_excludes.txt
28+
goimports:
29+
local-prefixes: github.com/prometheus/client_golang
30+
gofumpt:
31+
extra-rules: true

api/client_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ func BenchmarkClient(b *testing.B) {
134134

135135
for _, sizeKB := range []int{4, 50, 1000, 2000} {
136136
b.Run(fmt.Sprintf("%dKB", sizeKB), func(b *testing.B) {
137-
138137
testServer := httptest.NewServer(serveSpaces{sizeKB})
139138
defer testServer.Close()
140139

api/prometheus/v1/api.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ func marshalPointJSON(ptr unsafe.Pointer, stream *json.Stream) {
109109

110110
stream.WriteRaw(`"`)
111111
stream.WriteArrayEnd()
112-
113112
}
114113

115114
func marshalPointJSONIsEmpty(ptr unsafe.Pointer) bool {
@@ -230,25 +229,25 @@ type API interface {
230229
// Config returns the current Prometheus configuration.
231230
Config(ctx context.Context) (ConfigResult, error)
232231
// DeleteSeries deletes data for a selection of series in a time range.
233-
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
232+
DeleteSeries(ctx context.Context, matches []string, startTime, endTime time.Time) error
234233
// Flags returns the flag values that Prometheus was launched with.
235234
Flags(ctx context.Context) (FlagsResult, error)
236235
// LabelNames returns the unique label names present in the block in sorted order by given time range and matchers.
237-
LabelNames(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]string, Warnings, error)
236+
LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time) ([]string, Warnings, error)
238237
// LabelValues performs a query for the values of the given label, time range and matchers.
239-
LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error)
238+
LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time) (model.LabelValues, Warnings, error)
240239
// Query performs a query for the given time.
241240
Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error)
242241
// QueryRange performs a query for the given range.
243242
QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error)
244243
// QueryExemplars performs a query for exemplars by the given query and time range.
245-
QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error)
244+
QueryExemplars(ctx context.Context, query string, startTime, endTime time.Time) ([]ExemplarQueryResult, error)
246245
// Buildinfo returns various build information properties about the Prometheus server
247246
Buildinfo(ctx context.Context) (BuildinfoResult, error)
248247
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
249248
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
250249
// Series finds series by label matchers.
251-
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error)
250+
Series(ctx context.Context, matches []string, startTime, endTime time.Time) ([]model.LabelSet, Warnings, error)
252251
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
253252
// under the TSDB's data directory and returns the directory as response.
254253
Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
@@ -257,9 +256,9 @@ type API interface {
257256
// Targets returns an overview of the current state of the Prometheus target discovery.
258257
Targets(ctx context.Context) (TargetsResult, error)
259258
// TargetsMetadata returns metadata about metrics currently scraped by the target.
260-
TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, error)
259+
TargetsMetadata(ctx context.Context, matchTarget, metric, limit string) ([]MetricMetadata, error)
261260
// Metadata returns metadata about metrics currently scraped by the metric name.
262-
Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error)
261+
Metadata(ctx context.Context, metric, limit string) (map[string][]Metadata, error)
263262
// TSDB returns the cardinality statistics.
264263
TSDB(ctx context.Context) (TSDBResult, error)
265264
// WalReplay returns the current replay status of the wal.
@@ -699,7 +698,7 @@ func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
699698
return res, json.Unmarshal(body, &res)
700699
}
701700

702-
func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error {
701+
func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime, endTime time.Time) error {
703702
u := h.client.URL(epDeleteSeries, nil)
704703
q := u.Query()
705704

@@ -772,7 +771,7 @@ func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
772771
return res, json.Unmarshal(body, &res)
773772
}
774773

775-
func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]string, Warnings, error) {
774+
func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time) ([]string, Warnings, error) {
776775
u := h.client.URL(epLabels, nil)
777776
q := u.Query()
778777
q.Set("start", formatTime(startTime))
@@ -795,7 +794,7 @@ func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime ti
795794
return labelNames, w, json.Unmarshal(body, &labelNames)
796795
}
797796

798-
func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error) {
797+
func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time) (model.LabelValues, Warnings, error) {
799798
u := h.client.URL(epLabelValues, map[string]string{"name": label})
800799
q := u.Query()
801800
q.Set("start", formatTime(startTime))
@@ -833,7 +832,6 @@ func WithTimeout(timeout time.Duration) Option {
833832
}
834833

835834
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {
836-
837835
u := h.client.URL(epQuery, nil)
838836
q := u.Query()
839837

@@ -890,7 +888,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range, opts ..
890888
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
891889
}
892890

893-
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error) {
891+
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime, endTime time.Time) ([]model.LabelSet, Warnings, error) {
894892
u := h.client.URL(epSeries, nil)
895893
q := u.Query()
896894

@@ -973,7 +971,7 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
973971
return res, json.Unmarshal(body, &res)
974972
}
975973

976-
func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, error) {
974+
func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget, metric, limit string) ([]MetricMetadata, error) {
977975
u := h.client.URL(epTargetsMetadata, nil)
978976
q := u.Query()
979977

@@ -997,7 +995,7 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
997995
return res, json.Unmarshal(body, &res)
998996
}
999997

1000-
func (h *httpAPI) Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error) {
998+
func (h *httpAPI) Metadata(ctx context.Context, metric, limit string) (map[string][]Metadata, error) {
1001999
u := h.client.URL(epMetadata, nil)
10021000
q := u.Query()
10031001

@@ -1054,7 +1052,7 @@ func (h *httpAPI) WalReplay(ctx context.Context) (WalReplayStatus, error) {
10541052
return res, json.Unmarshal(body, &res)
10551053
}
10561054

1057-
func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error) {
1055+
func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime, endTime time.Time) ([]ExemplarQueryResult, error) {
10581056
u := h.client.URL(epQueryExemplars, nil)
10591057
q := u.Query()
10601058

@@ -1162,7 +1160,6 @@ func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Respon
11621160
}
11631161

11641162
return resp, []byte(result.Data), result.Warnings, err
1165-
11661163
}
11671164

11681165
// DoGetFallback will attempt to do the request as-is, and on a 405 or 501 it

api/prometheus/v1/api_test.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
6565
}
6666

6767
func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
68-
6968
test := c.curTest
7069

7170
if req.URL.Path != test.reqPath {
@@ -101,7 +100,6 @@ func (c *apiTestClient) DoGetFallback(ctx context.Context, u *url.URL, args url.
101100
}
102101

103102
func TestAPIs(t *testing.T) {
104-
105103
testTime := time.Now()
106104

107105
tc := &apiTestClient{
@@ -131,7 +129,7 @@ func TestAPIs(t *testing.T) {
131129
}
132130
}
133131

134-
doDeleteSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
132+
doDeleteSeries := func(matcher string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
135133
return func() (interface{}, Warnings, error) {
136134
return nil, nil, promAPI.DeleteSeries(context.Background(), []string{matcher}, startTime, endTime)
137135
}
@@ -182,7 +180,7 @@ func TestAPIs(t *testing.T) {
182180
}
183181
}
184182

185-
doSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
183+
doSeries := func(matcher string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
186184
return func() (interface{}, Warnings, error) {
187185
return promAPI.Series(context.Background(), []string{matcher}, startTime, endTime)
188186
}
@@ -209,14 +207,14 @@ func TestAPIs(t *testing.T) {
209207
}
210208
}
211209

212-
doTargetsMetadata := func(matchTarget string, metric string, limit string) func() (interface{}, Warnings, error) {
210+
doTargetsMetadata := func(matchTarget, metric, limit string) func() (interface{}, Warnings, error) {
213211
return func() (interface{}, Warnings, error) {
214212
v, err := promAPI.TargetsMetadata(context.Background(), matchTarget, metric, limit)
215213
return v, nil, err
216214
}
217215
}
218216

219-
doMetadata := func(metric string, limit string) func() (interface{}, Warnings, error) {
217+
doMetadata := func(metric, limit string) func() (interface{}, Warnings, error) {
220218
return func() (interface{}, Warnings, error) {
221219
v, err := promAPI.Metadata(context.Background(), metric, limit)
222220
return v, nil, err
@@ -237,7 +235,7 @@ func TestAPIs(t *testing.T) {
237235
}
238236
}
239237

240-
doQueryExemplars := func(query string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
238+
doQueryExemplars := func(query string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
241239
return func() (interface{}, Warnings, error) {
242240
v, err := promAPI.QueryExemplars(context.Background(), query, startTime, endTime)
243241
return v, nil, err
@@ -471,7 +469,8 @@ func TestAPIs(t *testing.T) {
471469
{
472470
"__name__": "up",
473471
"job": "prometheus",
474-
"instance": "localhost:9090"},
472+
"instance": "localhost:9090",
473+
},
475474
},
476475
reqMethod: "GET",
477476
reqPath: "/api/v1/series",
@@ -495,7 +494,8 @@ func TestAPIs(t *testing.T) {
495494
{
496495
"__name__": "up",
497496
"job": "prometheus",
498-
"instance": "localhost:9090"},
497+
"instance": "localhost:9090",
498+
},
499499
},
500500
inWarnings: []string{"a"},
501501
reqMethod: "GET",
@@ -586,7 +586,8 @@ func TestAPIs(t *testing.T) {
586586
{
587587
"__name__": "up",
588588
"job": "prometheus",
589-
"instance": "localhost:9090"},
589+
"instance": "localhost:9090",
590+
},
590591
},
591592
reqMethod: "POST",
592593
reqPath: "/api/v1/admin/tsdb/delete_series",
@@ -1115,7 +1116,7 @@ func TestAPIs(t *testing.T) {
11151116
"limit": []string{"1"},
11161117
},
11171118
res: map[string][]Metadata{
1118-
"go_goroutines": []Metadata{
1119+
"go_goroutines": {
11191120
{
11201121
Type: "gauge",
11211122
Help: "Number of goroutines that currently exist.",
@@ -1523,7 +1524,6 @@ func TestAPIClientDo(t *testing.T) {
15231524

15241525
for i, test := range tests {
15251526
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
1526-
15271527
tc.ch <- test
15281528

15291529
_, body, warnings, err := client.Do(context.Background(), tc.req)
@@ -1564,7 +1564,6 @@ func TestAPIClientDo(t *testing.T) {
15641564
t.Fatalf("expected body :%v, but got:%v", test.expectedBody, string(body))
15651565
}
15661566
})
1567-
15681567
}
15691568
}
15701569

api/prometheus/v1/example_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"os"
2323
"time"
2424

25+
"github.com/prometheus/common/config"
26+
2527
"github.com/prometheus/client_golang/api"
2628
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
27-
"github.com/prometheus/common/config"
2829
)
2930

3031
func ExampleAPI_query() {

prometheus/collector_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func (c collectorDescribedByCollect) Describe(ch chan<- *Desc) {
3030
}
3131

3232
func TestDescribeByCollect(t *testing.T) {
33-
3433
goodCollector := collectorDescribedByCollect{
3534
cnt: NewCounter(CounterOpts{Name: "c1", Help: "help c1"}),
3635
gge: NewGauge(GaugeOpts{Name: "g1", Help: "help g1"}),

prometheus/collectors/go_collector_latest.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ const (
7272
//
7373
// The current default is GoRuntimeMemStatsCollection, so the compatibility mode with
7474
// client_golang pre v1.12 (move to runtime/metrics).
75-
func WithGoCollections(flags GoCollectionOption) goOption {
75+
//nolint:staticcheck // Ignore SA1019 until v2.
76+
func WithGoCollections(flags GoCollectionOption) func(options *prometheus.GoCollectorOptions) {
7677
return func(o *goOptions) {
7778
o.EnabledCollections = uint32(flags)
7879
}

prometheus/counter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func TestCounterExemplar(t *testing.T) {
231231
}
232232
expectedExemplar := &dto.Exemplar{
233233
Label: []*dto.LabelPair{
234-
&dto.LabelPair{Name: proto.String("foo"), Value: proto.String("bar")},
234+
{Name: proto.String("foo"), Value: proto.String("bar")},
235235
},
236236
Value: proto.Float64(42),
237237
Timestamp: ts,

prometheus/desc.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121

2222
"github.com/cespare/xxhash/v2"
23+
2324
"github.com/prometheus/client_golang/prometheus/internal"
2425

2526
//nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.

prometheus/example_metricvec_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ func (v *InfoVec) MustCurryWith(labels prometheus.Labels) *InfoVec {
108108
}
109109

110110
func ExampleMetricVec() {
111-
112111
infoVec := NewInfoVec(
113112
"library_version_info",
114113
"Versions of the libraries used in this binary.",

prometheus/example_timer_complex_test.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,25 @@ import (
1919
"github.com/prometheus/client_golang/prometheus"
2020
)
2121

22-
var (
23-
// apiRequestDuration tracks the duration separate for each HTTP status
24-
// class (1xx, 2xx, ...). This creates a fair amount of time series on
25-
// the Prometheus server. Usually, you would track the duration of
26-
// serving HTTP request without partitioning by outcome. Do something
27-
// like this only if needed. Also note how only status classes are
28-
// tracked, not every single status code. The latter would create an
29-
// even larger amount of time series. Request counters partitioned by
30-
// status code are usually OK as each counter only creates one time
31-
// series. Histograms are way more expensive, so partition with care and
32-
// only where you really need separate latency tracking. Partitioning by
33-
// status class is only an example. In concrete cases, other partitions
34-
// might make more sense.
35-
apiRequestDuration = prometheus.NewHistogramVec(
36-
prometheus.HistogramOpts{
37-
Name: "api_request_duration_seconds",
38-
Help: "Histogram for the request duration of the public API, partitioned by status class.",
39-
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
40-
},
41-
[]string{"status_class"},
42-
)
22+
// apiRequestDuration tracks the duration separate for each HTTP status
23+
// class (1xx, 2xx, ...). This creates a fair amount of time series on
24+
// the Prometheus server. Usually, you would track the duration of
25+
// serving HTTP request without partitioning by outcome. Do something
26+
// like this only if needed. Also note how only status classes are
27+
// tracked, not every single status code. The latter would create an
28+
// even larger amount of time series. Request counters partitioned by
29+
// status code are usually OK as each counter only creates one time
30+
// series. Histograms are way more expensive, so partition with care and
31+
// only where you really need separate latency tracking. Partitioning by
32+
// status class is only an example. In concrete cases, other partitions
33+
// might make more sense.
34+
var apiRequestDuration = prometheus.NewHistogramVec(
35+
prometheus.HistogramOpts{
36+
Name: "api_request_duration_seconds",
37+
Help: "Histogram for the request duration of the public API, partitioned by status class.",
38+
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
39+
},
40+
[]string{"status_class"},
4341
)
4442

4543
func handler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)