Skip to content

Commit

Permalink
fix assorted oddities found by golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Mewes <christoph@kubermatic.com>
  • Loading branch information
xrstf committed Jun 17, 2022
1 parent 2cfd1eb commit d3603e0
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 151 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 @@ -856,7 +856,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 @@ -885,7 +885,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, 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 @@ -255,11 +253,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 @@ -271,11 +264,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 @@ -289,11 +278,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 @@ -307,11 +292,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 @@ -327,15 +308,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 @@ -351,12 +327,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 @@ -369,14 +340,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 @@ -393,7 +357,6 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET",
reqPath: "/api/v1/labels",
res: []string{"val1", "val2"},
warnings: []string{"a"},
},

{
Expand All @@ -410,14 +373,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 @@ -435,7 +396,6 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values",
res: model.LabelValues{"val1", "val2"},
warnings: []string{"a"},
},

{
Expand All @@ -452,14 +412,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 @@ -474,11 +432,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 @@ -591,24 +524,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 @@ -1067,11 +990,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 @@ -1090,12 +1008,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 @@ -1111,10 +1024,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": {
{
Expand All @@ -1131,11 +1040,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 @@ -1323,7 +1228,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
2 changes: 1 addition & 1 deletion prometheus/internal/difflib.go
Expand Up @@ -485,7 +485,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
2 changes: 1 addition & 1 deletion prometheus/internal/difflib_test.go
Expand Up @@ -134,7 +134,7 @@ four`
Context: 3,
}
result, _ := GetUnifiedDiffString(diff)
fmt.Println(strings.Replace(result, "\t", " ", -1))
fmt.Println(strings.ReplaceAll(result, "\t", " "))
// Output:
// --- Original 2005-01-26 23:30:50
// +++ Current 2010-04-02 10:20:52
Expand Down
4 changes: 2 additions & 2 deletions prometheus/internal/go_runtime_metrics.go
Expand Up @@ -61,9 +61,9 @@ func RuntimeMetricsToProm(d *metrics.Description) (string, string, string, bool)
// name has - replaced with _ and is concatenated with the unit and
// other data.
name = strings.ReplaceAll(name, "-", "_")
name = name + "_" + unit
name += "_" + unit
if d.Cumulative && d.Kind != metrics.KindFloat64Histogram {
name = name + "_total"
name += "_total"
}

valid := model.IsValidMetricName(model.LabelValue(namespace + "_" + subsystem + "_" + name))
Expand Down

0 comments on commit d3603e0

Please sign in to comment.