Skip to content

Commit

Permalink
fix assorted oddities found by golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed May 2, 2022
1 parent 4048091 commit a3592cf
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 168 deletions.
2 changes: 1 addition & 1 deletion api/client.go
Expand Up @@ -109,7 +109,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {

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

u := *c.endpoint
Expand Down
4 changes: 2 additions & 2 deletions api/prometheus/v1/api.go
Expand Up @@ -858,7 +858,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ..
}

var qres queryResult
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
return qres.v, warnings, json.Unmarshal(body, &qres)
}

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

var qres queryResult

return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
return qres.v, warnings, json.Unmarshal(body, &qres)
}

func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error) {
Expand Down
121 changes: 14 additions & 107 deletions api/prometheus/v1/api_test.go
Expand Up @@ -40,10 +40,8 @@ type apiTest struct {
inRes interface{}

reqPath string
reqParam url.Values
reqMethod string
res interface{}
warnings Warnings
err error
}

Expand All @@ -55,7 +53,7 @@ type apiTestClient struct {
func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
path := ep
for k, v := range args {
path = strings.Replace(path, ":"+k, v, -1)
path = strings.ReplaceAll(path, ":"+k, v)
}
u := &url.URL{
Host: "test:9090",
Expand Down Expand Up @@ -257,11 +255,6 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
"timeout": []string{(5 * time.Second).String()},
},
res: &model.Scalar{
Value: 2,
Timestamp: model.TimeFromUnix(testTime.Unix()),
Expand All @@ -273,11 +266,7 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},
{
do: doQuery("2", testTime),
Expand All @@ -291,11 +280,7 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
},
err: errors.New("server_error: server error: 500"),
err: errors.New("server_error: server error: 500"),
},
{
do: doQuery("2", testTime),
Expand All @@ -309,11 +294,7 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
},
err: errors.New("client_error: client error: 404"),
err: errors.New("client_error: client error: 404"),
},
// Warning only.
{
Expand All @@ -329,15 +310,10 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
},
res: &model.Scalar{
Value: 2,
Timestamp: model.TimeFromUnix(testTime.Unix()),
},
warnings: []string{"warning"},
},
// Warning + error.
{
Expand All @@ -353,12 +329,7 @@ func TestAPIs(t *testing.T) {

reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)},
},
err: errors.New("client_error: client error: 404"),
warnings: []string{"warning"},
err: errors.New("client_error: client error: 404"),
},

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

reqMethod: "POST",
reqPath: "/api/v1/query_range",
reqParam: url.Values{
"query": []string{"2"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
"step": []string{time.Minute.String()},
"timeout": []string{(5 * time.Second).String()},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

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

{
Expand All @@ -412,14 +375,12 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET",
reqPath: "/api/v1/labels",
err: fmt.Errorf("some error"),
warnings: []string{"a"},
},
{
do: doLabelNames([]string{"up"}),
inRes: []string{"val1", "val2"},
reqMethod: "GET",
reqPath: "/api/v1/labels",
reqParam: url.Values{"match[]": {"up"}},
res: []string{"val1", "val2"},
},

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

{
Expand All @@ -454,14 +414,12 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values",
err: fmt.Errorf("some error"),
warnings: []string{"a"},
},
{
do: doLabelValues([]string{"up"}, "mylabel"),
inRes: []string{"val1", "val2"},
reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{"match[]": {"up"}},
res: model.LabelValues{"val1", "val2"},
},

Expand All @@ -475,11 +433,6 @@ func TestAPIs(t *testing.T) {
},
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
res: []model.LabelSet{
{
"__name__": "up",
Expand All @@ -500,32 +453,21 @@ func TestAPIs(t *testing.T) {
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
res: []model.LabelSet{
{
"__name__": "up",
"job": "prometheus",
"instance": "localhost:9090",
},
},
warnings: []string{"a"},
},

{
do: doSeries("up", testTime.Add(-time.Minute), testTime),
inErr: fmt.Errorf("some error"),
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},
// Series with error and warning.
{
Expand All @@ -534,13 +476,7 @@ func TestAPIs(t *testing.T) {
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
err: fmt.Errorf("some error"),
warnings: []string{"a"},
err: fmt.Errorf("some error"),
},

{
Expand All @@ -550,9 +486,6 @@ func TestAPIs(t *testing.T) {
},
reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/snapshot",
reqParam: url.Values{
"skip_head": []string{"true"},
},
res: SnapshotResult{
Name: "20171210T211224Z-2be650b6d019eb54",
},
Expand Down Expand Up @@ -590,24 +523,14 @@ func TestAPIs(t *testing.T) {
},
reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/delete_series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
},

{
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
inErr: fmt.Errorf("some error"),
reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/delete_series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

{
Expand Down Expand Up @@ -1066,11 +989,6 @@ func TestAPIs(t *testing.T) {
},
reqMethod: "GET",
reqPath: "/api/v1/targets/metadata",
reqParam: url.Values{
"match_target": []string{"{job=\"prometheus\"}"},
"metric": []string{"go_goroutines"},
"limit": []string{"1"},
},
res: []MetricMetadata{
{
Target: map[string]string{
Expand All @@ -1089,12 +1007,7 @@ func TestAPIs(t *testing.T) {
inErr: fmt.Errorf("some error"),
reqMethod: "GET",
reqPath: "/api/v1/targets/metadata",
reqParam: url.Values{
"match_target": []string{"{job=\"prometheus\"}"},
"metric": []string{"go_goroutines"},
"limit": []string{"1"},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

{
Expand All @@ -1110,10 +1023,6 @@ func TestAPIs(t *testing.T) {
},
reqMethod: "GET",
reqPath: "/api/v1/metadata",
reqParam: url.Values{
"metric": []string{"go_goroutines"},
"limit": []string{"1"},
},
res: map[string][]Metadata{
"go_goroutines": []Metadata{
{
Expand All @@ -1130,11 +1039,7 @@ func TestAPIs(t *testing.T) {
inErr: fmt.Errorf("some error"),
reqMethod: "GET",
reqPath: "/api/v1/metadata",
reqParam: url.Values{
"metric": []string{""},
"limit": []string{"1"},
},
err: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

{
Expand Down Expand Up @@ -1322,7 +1227,9 @@ func TestAPIs(t *testing.T) {
if err.Error() != test.err.Error() {
t.Errorf("unexpected error: want %s, got %s", test.err, err)
}
if apiErr, ok := err.(*Error); ok {

apiErr := &Error{}
if ok := errors.As(err, &apiErr); ok {
if apiErr.Detail != test.inRes {
t.Errorf("%q should be %q", apiErr.Detail, test.inRes)
}
Expand Down
4 changes: 3 additions & 1 deletion prometheus/examples_test.go
Expand Up @@ -15,6 +15,7 @@ package prometheus_test

import (
"bytes"
"errors"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -713,7 +714,8 @@ func ExampleAlreadyRegisteredError() {
Help: "The total number of requests served.",
})
if err := prometheus.Register(reqCounter); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
are := &prometheus.AlreadyRegisteredError{}
if errors.As(err, are) {
// A counter for that metric has been registered before.
// Use the old counter from now on.
reqCounter = are.ExistingCollector.(prometheus.Counter)
Expand Down
4 changes: 2 additions & 2 deletions prometheus/internal/difflib.go
Expand Up @@ -483,7 +483,7 @@ func (m *SequenceMatcher) QuickRatio() float64 {
if m.fullBCount == nil {
m.fullBCount = map[string]int{}
for _, s := range m.b {
m.fullBCount[s] = m.fullBCount[s] + 1
m.fullBCount[s]++
}
}

Expand Down Expand Up @@ -637,7 +637,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
func GetUnifiedDiffString(diff UnifiedDiff) (string, error) {
w := &bytes.Buffer{}
err := WriteUnifiedDiff(w, diff)
return string(w.Bytes()), err
return w.String(), err
}

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

0 comments on commit a3592cf

Please sign in to comment.