Skip to content

Commit 1a79d53

Browse files
committed
fix assorted oddities found by golangci-lint
Signed-off-by: Christoph Mewes <[email protected]>
1 parent 4048091 commit 1a79d53

File tree

19 files changed

+82
-168
lines changed

19 files changed

+82
-168
lines changed

api/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
109109

110110
for arg, val := range args {
111111
arg = ":" + arg
112-
p = strings.Replace(p, arg, val, -1)
112+
p = strings.ReplaceAll(p, arg, val)
113113
}
114114

115115
u := *c.endpoint

api/prometheus/v1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ..
858858
}
859859

860860
var qres queryResult
861-
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
861+
return qres.v, warnings, json.Unmarshal(body, &qres)
862862
}
863863

864864
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error) {
@@ -887,7 +887,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range, opts ..
887887

888888
var qres queryResult
889889

890-
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
890+
return qres.v, warnings, json.Unmarshal(body, &qres)
891891
}
892892

893893
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error) {

api/prometheus/v1/api_test.go

Lines changed: 14 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ type apiTest struct {
4040
inRes interface{}
4141

4242
reqPath string
43-
reqParam url.Values
4443
reqMethod string
4544
res interface{}
46-
warnings Warnings
4745
err error
4846
}
4947

@@ -55,7 +53,7 @@ type apiTestClient struct {
5553
func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
5654
path := ep
5755
for k, v := range args {
58-
path = strings.Replace(path, ":"+k, v, -1)
56+
path = strings.ReplaceAll(path, ":"+k, v)
5957
}
6058
u := &url.URL{
6159
Host: "test:9090",
@@ -257,11 +255,6 @@ func TestAPIs(t *testing.T) {
257255

258256
reqMethod: "POST",
259257
reqPath: "/api/v1/query",
260-
reqParam: url.Values{
261-
"query": []string{"2"},
262-
"time": []string{testTime.Format(time.RFC3339Nano)},
263-
"timeout": []string{(5 * time.Second).String()},
264-
},
265258
res: &model.Scalar{
266259
Value: 2,
267260
Timestamp: model.TimeFromUnix(testTime.Unix()),
@@ -273,11 +266,7 @@ func TestAPIs(t *testing.T) {
273266

274267
reqMethod: "POST",
275268
reqPath: "/api/v1/query",
276-
reqParam: url.Values{
277-
"query": []string{"2"},
278-
"time": []string{testTime.Format(time.RFC3339Nano)},
279-
},
280-
err: fmt.Errorf("some error"),
269+
err: fmt.Errorf("some error"),
281270
},
282271
{
283272
do: doQuery("2", testTime),
@@ -291,11 +280,7 @@ func TestAPIs(t *testing.T) {
291280

292281
reqMethod: "POST",
293282
reqPath: "/api/v1/query",
294-
reqParam: url.Values{
295-
"query": []string{"2"},
296-
"time": []string{testTime.Format(time.RFC3339Nano)},
297-
},
298-
err: errors.New("server_error: server error: 500"),
283+
err: errors.New("server_error: server error: 500"),
299284
},
300285
{
301286
do: doQuery("2", testTime),
@@ -309,11 +294,7 @@ func TestAPIs(t *testing.T) {
309294

310295
reqMethod: "POST",
311296
reqPath: "/api/v1/query",
312-
reqParam: url.Values{
313-
"query": []string{"2"},
314-
"time": []string{testTime.Format(time.RFC3339Nano)},
315-
},
316-
err: errors.New("client_error: client error: 404"),
297+
err: errors.New("client_error: client error: 404"),
317298
},
318299
// Warning only.
319300
{
@@ -329,15 +310,10 @@ func TestAPIs(t *testing.T) {
329310

330311
reqMethod: "POST",
331312
reqPath: "/api/v1/query",
332-
reqParam: url.Values{
333-
"query": []string{"2"},
334-
"time": []string{testTime.Format(time.RFC3339Nano)},
335-
},
336313
res: &model.Scalar{
337314
Value: 2,
338315
Timestamp: model.TimeFromUnix(testTime.Unix()),
339316
},
340-
warnings: []string{"warning"},
341317
},
342318
// Warning + error.
343319
{
@@ -353,12 +329,7 @@ func TestAPIs(t *testing.T) {
353329

354330
reqMethod: "POST",
355331
reqPath: "/api/v1/query",
356-
reqParam: url.Values{
357-
"query": []string{"2"},
358-
"time": []string{testTime.Format(time.RFC3339Nano)},
359-
},
360-
err: errors.New("client_error: client error: 404"),
361-
warnings: []string{"warning"},
332+
err: errors.New("client_error: client error: 404"),
362333
},
363334

364335
{
@@ -371,14 +342,7 @@ func TestAPIs(t *testing.T) {
371342

372343
reqMethod: "POST",
373344
reqPath: "/api/v1/query_range",
374-
reqParam: url.Values{
375-
"query": []string{"2"},
376-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
377-
"end": []string{testTime.Format(time.RFC3339Nano)},
378-
"step": []string{time.Minute.String()},
379-
"timeout": []string{(5 * time.Second).String()},
380-
},
381-
err: fmt.Errorf("some error"),
345+
err: fmt.Errorf("some error"),
382346
},
383347

384348
{
@@ -395,7 +359,6 @@ func TestAPIs(t *testing.T) {
395359
reqMethod: "GET",
396360
reqPath: "/api/v1/labels",
397361
res: []string{"val1", "val2"},
398-
warnings: []string{"a"},
399362
},
400363

401364
{
@@ -412,14 +375,12 @@ func TestAPIs(t *testing.T) {
412375
reqMethod: "GET",
413376
reqPath: "/api/v1/labels",
414377
err: fmt.Errorf("some error"),
415-
warnings: []string{"a"},
416378
},
417379
{
418380
do: doLabelNames([]string{"up"}),
419381
inRes: []string{"val1", "val2"},
420382
reqMethod: "GET",
421383
reqPath: "/api/v1/labels",
422-
reqParam: url.Values{"match[]": {"up"}},
423384
res: []string{"val1", "val2"},
424385
},
425386

@@ -437,7 +398,6 @@ func TestAPIs(t *testing.T) {
437398
reqMethod: "GET",
438399
reqPath: "/api/v1/label/mylabel/values",
439400
res: model.LabelValues{"val1", "val2"},
440-
warnings: []string{"a"},
441401
},
442402

443403
{
@@ -454,14 +414,12 @@ func TestAPIs(t *testing.T) {
454414
reqMethod: "GET",
455415
reqPath: "/api/v1/label/mylabel/values",
456416
err: fmt.Errorf("some error"),
457-
warnings: []string{"a"},
458417
},
459418
{
460419
do: doLabelValues([]string{"up"}, "mylabel"),
461420
inRes: []string{"val1", "val2"},
462421
reqMethod: "GET",
463422
reqPath: "/api/v1/label/mylabel/values",
464-
reqParam: url.Values{"match[]": {"up"}},
465423
res: model.LabelValues{"val1", "val2"},
466424
},
467425

@@ -475,11 +433,6 @@ func TestAPIs(t *testing.T) {
475433
},
476434
reqMethod: "GET",
477435
reqPath: "/api/v1/series",
478-
reqParam: url.Values{
479-
"match": []string{"up"},
480-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
481-
"end": []string{testTime.Format(time.RFC3339Nano)},
482-
},
483436
res: []model.LabelSet{
484437
{
485438
"__name__": "up",
@@ -500,32 +453,21 @@ func TestAPIs(t *testing.T) {
500453
inWarnings: []string{"a"},
501454
reqMethod: "GET",
502455
reqPath: "/api/v1/series",
503-
reqParam: url.Values{
504-
"match": []string{"up"},
505-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
506-
"end": []string{testTime.Format(time.RFC3339Nano)},
507-
},
508456
res: []model.LabelSet{
509457
{
510458
"__name__": "up",
511459
"job": "prometheus",
512460
"instance": "localhost:9090",
513461
},
514462
},
515-
warnings: []string{"a"},
516463
},
517464

518465
{
519466
do: doSeries("up", testTime.Add(-time.Minute), testTime),
520467
inErr: fmt.Errorf("some error"),
521468
reqMethod: "GET",
522469
reqPath: "/api/v1/series",
523-
reqParam: url.Values{
524-
"match": []string{"up"},
525-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
526-
"end": []string{testTime.Format(time.RFC3339Nano)},
527-
},
528-
err: fmt.Errorf("some error"),
470+
err: fmt.Errorf("some error"),
529471
},
530472
// Series with error and warning.
531473
{
@@ -534,13 +476,7 @@ func TestAPIs(t *testing.T) {
534476
inWarnings: []string{"a"},
535477
reqMethod: "GET",
536478
reqPath: "/api/v1/series",
537-
reqParam: url.Values{
538-
"match": []string{"up"},
539-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
540-
"end": []string{testTime.Format(time.RFC3339Nano)},
541-
},
542-
err: fmt.Errorf("some error"),
543-
warnings: []string{"a"},
479+
err: fmt.Errorf("some error"),
544480
},
545481

546482
{
@@ -550,9 +486,6 @@ func TestAPIs(t *testing.T) {
550486
},
551487
reqMethod: "POST",
552488
reqPath: "/api/v1/admin/tsdb/snapshot",
553-
reqParam: url.Values{
554-
"skip_head": []string{"true"},
555-
},
556489
res: SnapshotResult{
557490
Name: "20171210T211224Z-2be650b6d019eb54",
558491
},
@@ -590,24 +523,14 @@ func TestAPIs(t *testing.T) {
590523
},
591524
reqMethod: "POST",
592525
reqPath: "/api/v1/admin/tsdb/delete_series",
593-
reqParam: url.Values{
594-
"match": []string{"up"},
595-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
596-
"end": []string{testTime.Format(time.RFC3339Nano)},
597-
},
598526
},
599527

600528
{
601529
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
602530
inErr: fmt.Errorf("some error"),
603531
reqMethod: "POST",
604532
reqPath: "/api/v1/admin/tsdb/delete_series",
605-
reqParam: url.Values{
606-
"match": []string{"up"},
607-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
608-
"end": []string{testTime.Format(time.RFC3339Nano)},
609-
},
610-
err: fmt.Errorf("some error"),
533+
err: fmt.Errorf("some error"),
611534
},
612535

613536
{
@@ -1066,11 +989,6 @@ func TestAPIs(t *testing.T) {
1066989
},
1067990
reqMethod: "GET",
1068991
reqPath: "/api/v1/targets/metadata",
1069-
reqParam: url.Values{
1070-
"match_target": []string{"{job=\"prometheus\"}"},
1071-
"metric": []string{"go_goroutines"},
1072-
"limit": []string{"1"},
1073-
},
1074992
res: []MetricMetadata{
1075993
{
1076994
Target: map[string]string{
@@ -1089,12 +1007,7 @@ func TestAPIs(t *testing.T) {
10891007
inErr: fmt.Errorf("some error"),
10901008
reqMethod: "GET",
10911009
reqPath: "/api/v1/targets/metadata",
1092-
reqParam: url.Values{
1093-
"match_target": []string{"{job=\"prometheus\"}"},
1094-
"metric": []string{"go_goroutines"},
1095-
"limit": []string{"1"},
1096-
},
1097-
err: fmt.Errorf("some error"),
1010+
err: fmt.Errorf("some error"),
10981011
},
10991012

11001013
{
@@ -1110,10 +1023,6 @@ func TestAPIs(t *testing.T) {
11101023
},
11111024
reqMethod: "GET",
11121025
reqPath: "/api/v1/metadata",
1113-
reqParam: url.Values{
1114-
"metric": []string{"go_goroutines"},
1115-
"limit": []string{"1"},
1116-
},
11171026
res: map[string][]Metadata{
11181027
"go_goroutines": []Metadata{
11191028
{
@@ -1130,11 +1039,7 @@ func TestAPIs(t *testing.T) {
11301039
inErr: fmt.Errorf("some error"),
11311040
reqMethod: "GET",
11321041
reqPath: "/api/v1/metadata",
1133-
reqParam: url.Values{
1134-
"metric": []string{""},
1135-
"limit": []string{"1"},
1136-
},
1137-
err: fmt.Errorf("some error"),
1042+
err: fmt.Errorf("some error"),
11381043
},
11391044

11401045
{
@@ -1322,7 +1227,9 @@ func TestAPIs(t *testing.T) {
13221227
if err.Error() != test.err.Error() {
13231228
t.Errorf("unexpected error: want %s, got %s", test.err, err)
13241229
}
1325-
if apiErr, ok := err.(*Error); ok {
1230+
1231+
apiErr := &Error{}
1232+
if ok := errors.As(err, &apiErr); ok {
13261233
if apiErr.Detail != test.inRes {
13271234
t.Errorf("%q should be %q", apiErr.Detail, test.inRes)
13281235
}

prometheus/examples_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package prometheus_test
1515

1616
import (
1717
"bytes"
18+
"errors"
1819
"fmt"
1920
"math"
2021
"net/http"
@@ -713,7 +714,8 @@ func ExampleAlreadyRegisteredError() {
713714
Help: "The total number of requests served.",
714715
})
715716
if err := prometheus.Register(reqCounter); err != nil {
716-
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
717+
are := &prometheus.AlreadyRegisteredError{}
718+
if errors.As(err, are) {
717719
// A counter for that metric has been registered before.
718720
// Use the old counter from now on.
719721
reqCounter = are.ExistingCollector.(prometheus.Counter)

prometheus/internal/difflib.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func (m *SequenceMatcher) QuickRatio() float64 {
483483
if m.fullBCount == nil {
484484
m.fullBCount = map[string]int{}
485485
for _, s := range m.b {
486-
m.fullBCount[s] = m.fullBCount[s] + 1
486+
m.fullBCount[s]++
487487
}
488488
}
489489

@@ -637,7 +637,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
637637
func GetUnifiedDiffString(diff UnifiedDiff) (string, error) {
638638
w := &bytes.Buffer{}
639639
err := WriteUnifiedDiff(w, diff)
640-
return string(w.Bytes()), err
640+
return w.String(), err
641641
}
642642

643643
// Split a string on "\n" while preserving them. The output can be used

0 commit comments

Comments
 (0)