Skip to content

Commit 49f33a4

Browse files
authored
Enable usestdlibvars linter (#6249)
#### Description - enable [usestdlibvars](https://golangci-lint.run/usage/linters/#usestdlibvars) linter - uses standard constants or vars instead of their values. Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 3f8c2e7 commit 49f33a4

File tree

20 files changed

+159
-146
lines changed

20 files changed

+159
-146
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ linters:
105105
# Checks usage of github.com/stretchr/testify.
106106
- testifylint
107107

108+
# Detects the possibility to use variables/constants from the Go standard library.
109+
- usestdlibvars
110+
108111
# TODO consider adding more linters, cf. https://olegk.dev/go-linters-configuration-the-right-version
109112

110113
linters-settings:

cmd/anonymizer/app/anonymizer/anonymizer_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package anonymizer
55

66
import (
7+
"net/http"
78
"os"
89
"path/filepath"
910
"testing"
@@ -18,7 +19,7 @@ import (
1819

1920
var tags = []model.KeyValue{
2021
model.Bool("error", true),
21-
model.String("http.method", "POST"),
22+
model.String("http.method", http.MethodPost),
2223
model.Bool("foobar", true),
2324
}
2425

@@ -127,7 +128,7 @@ func TestAnonymizer_SaveMapping(t *testing.T) {
127128
func TestAnonymizer_FilterStandardTags(t *testing.T) {
128129
expected := []model.KeyValue{
129130
model.Bool("error", true),
130-
model.String("http.method", "POST"),
131+
model.String("http.method", http.MethodPost),
131132
}
132133
actual := filterStandardTags(tags)
133134
assert.Equal(t, expected, actual)

cmd/anonymizer/app/writer/writer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package writer
55

66
import (
7+
"net/http"
78
"testing"
89
"time"
910

@@ -15,7 +16,7 @@ import (
1516

1617
var tags = []model.KeyValue{
1718
model.Bool("error", true),
18-
model.String("http.method", "POST"),
19+
model.String("http.method", http.MethodPost),
1920
model.Bool("foobar", true),
2021
}
2122

cmd/internal/status/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Command(v *viper.Viper, adminPort int) *cobra.Command {
3030
url := convert(v.GetString(statusHTTPHostPort))
3131
ctx, cx := context.WithTimeout(context.Background(), time.Second)
3232
defer cx()
33-
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
33+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
3434
resp, err := http.DefaultClient.Do(req)
3535
if err != nil {
3636
return err

cmd/query/app/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,12 @@ func TestServerHTTPTenancy(t *testing.T) {
760760
{
761761
name: "no tenant",
762762
// no value for tenant header
763-
status: 401,
763+
status: http.StatusUnauthorized,
764764
},
765765
{
766766
name: "tenant",
767767
tenant: "acme",
768-
status: 200,
768+
status: http.StatusOK,
769769
},
770770
}
771771

crossdock/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
5454
// when method is HEAD, report back with a 200 when ready to run tests
55-
if r.Method == "HEAD" {
55+
if r.Method == http.MethodHead {
5656
if !handler.isInitialized() {
5757
http.Error(w, "Components not ready", http.StatusServiceUnavailable)
5858
}
@@ -92,7 +92,7 @@ func (h *clientHandler) isInitialized() bool {
9292
}
9393

9494
func is2xxStatusCode(statusCode int) bool {
95-
return statusCode >= 200 && statusCode <= 299
95+
return statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices
9696
}
9797

9898
func httpHealthCheck(logger *zap.Logger, service, healthURL string) {

examples/hotrod/pkg/tracing/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *HTTPClient) GetJSON(ctx context.Context, _ string /* endpoint */, url s
4848

4949
defer res.Body.Close()
5050

51-
if res.StatusCode >= 400 {
51+
if res.StatusCode >= http.StatusBadRequest {
5252
body, err := io.ReadAll(res.Body)
5353
if err != nil {
5454
return err

examples/hotrod/pkg/tracing/rpcmetrics/metrics.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package rpcmetrics
66

77
import (
8+
"net/http"
89
"sync"
910

1011
"github.com/jaegertracing/jaeger/pkg/metrics"
@@ -45,13 +46,13 @@ type Metrics struct {
4546

4647
func (m *Metrics) recordHTTPStatusCode(statusCode int64) {
4748
switch {
48-
case statusCode >= 200 && statusCode < 300:
49+
case statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices:
4950
m.HTTPStatusCode2xx.Inc(1)
50-
case statusCode >= 300 && statusCode < 400:
51+
case statusCode >= http.StatusMultipleChoices && statusCode < http.StatusBadRequest:
5152
m.HTTPStatusCode3xx.Inc(1)
52-
case statusCode >= 400 && statusCode < 500:
53+
case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError:
5354
m.HTTPStatusCode4xx.Inc(1)
54-
case statusCode >= 500 && statusCode < 600:
55+
case statusCode >= http.StatusInternalServerError && statusCode < 600:
5556
m.HTTPStatusCode5xx.Inc(1)
5657
}
5758
}

model/adjuster/sort_tags_and_log_fields_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package adjuster
66

77
import (
8+
"net/http"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
@@ -88,7 +89,7 @@ func TestSortTagsAndLogFieldsDoesSortFields(t *testing.T) {
8889
func TestSortTagsAndLogFieldsDoesSortTags(t *testing.T) {
8990
testCases := []model.KeyValues{
9091
{
91-
model.String("http.method", "GET"),
92+
model.String("http.method", http.MethodGet),
9293
model.String("http.url", "http://wikipedia.org"),
9394
model.Int64("http.status_code", 200),
9495
model.String("guid:x-request-id", "f61defd2-7a77-11ef-b54f-4fbb67a6d181"),

pkg/bearertoken/transport_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestRoundTripper(t *testing.T) {
7878
t.Run(tc.name, func(t *testing.T) {
7979
server := httptest.NewServer(nil)
8080
defer server.Close()
81-
req, err := http.NewRequestWithContext(tc.requestContext, "GET", server.URL, nil)
81+
req, err := http.NewRequestWithContext(tc.requestContext, http.MethodGet, server.URL, nil)
8282
require.NoError(t, err)
8383

8484
tr := RoundTripper{

pkg/clientcfg/clientcfghttp/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,14 @@ func TestHTTPHandlerErrors(t *testing.T) {
283283
withServer("", probabilistic(0.001), restrictions("luggage", 10), withGorilla, func(ts *testServer) {
284284
handler := ts.handler
285285

286-
req := httptest.NewRequest("GET", "http://localhost:80/?service=X", nil)
286+
req := httptest.NewRequest(http.MethodGet, "http://localhost:80/?service=X", nil)
287287
w := &mockWriter{header: make(http.Header)}
288288
handler.serveSamplingHTTP(w, req, handler.encodeThriftLegacy)
289289

290290
ts.metricsFactory.AssertCounterMetrics(t,
291291
metricstest.ExpectedMetric{Name: "http-server.errors", Tags: map[string]string{"source": "write", "status": "5xx"}, Value: 1})
292292

293-
req = httptest.NewRequest("GET", "http://localhost:80/baggageRestrictions?service=X", nil)
293+
req = httptest.NewRequest(http.MethodGet, "http://localhost:80/baggageRestrictions?service=X", nil)
294294
handler.serveBaggageHTTP(w, req)
295295

296296
ts.metricsFactory.AssertCounterMetrics(t,

pkg/es/wrapper/wrapper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package eswrapper
77
import (
88
"context"
99
"fmt"
10+
"net/http"
1011
"strings"
1112

1213
esV8 "github.com/elastic/go-elasticsearch/v8"
@@ -193,7 +194,7 @@ func (c TemplateCreatorWrapperV8) Do(context.Context) (*elastic.IndicesPutTempla
193194
if err != nil {
194195
return nil, fmt.Errorf("error creating index template %s: %w", c.templateName, err)
195196
}
196-
if resp.StatusCode != 200 {
197+
if resp.StatusCode != http.StatusOK {
197198
return nil, fmt.Errorf("error creating index template %s: %s", c.templateName, resp)
198199
}
199200
return nil, nil // no response expected by span writer

pkg/version/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func RegisterHandler(mu *http.ServeMux, logger *zap.Logger) {
1818
logger.Fatal("Could not get Jaeger version", zap.Error(err))
1919
}
2020
mu.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) {
21-
w.WriteHeader(200)
21+
w.WriteHeader(http.StatusOK)
2222
w.Write(jsonData)
2323
})
2424
}

plugin/sampling/strategyprovider/adaptive/aggregator_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package adaptive
55

66
import (
7+
"net/http"
78
"testing"
89
"time"
910

@@ -37,12 +38,12 @@ func TestAggregator(t *testing.T) {
3738

3839
a, err := NewAggregator(testOpts, logger, metricsFactory, mockEP, mockStorage)
3940
require.NoError(t, err)
40-
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001)
41-
a.RecordThroughput("B", "POST", model.SamplerTypeProbabilistic, 0.001)
42-
a.RecordThroughput("C", "GET", model.SamplerTypeProbabilistic, 0.001)
43-
a.RecordThroughput("A", "POST", model.SamplerTypeProbabilistic, 0.001)
44-
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001)
45-
a.RecordThroughput("A", "GET", model.SamplerTypeLowerBound, 0.001)
41+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
42+
a.RecordThroughput("B", http.MethodPost, model.SamplerTypeProbabilistic, 0.001)
43+
a.RecordThroughput("C", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
44+
a.RecordThroughput("A", http.MethodPost, model.SamplerTypeProbabilistic, 0.001)
45+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
46+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeLowerBound, 0.001)
4647

4748
a.Start()
4849
defer a.Close()
@@ -74,17 +75,17 @@ func TestIncrementThroughput(t *testing.T) {
7475
require.NoError(t, err)
7576
// 20 different probabilities
7677
for i := 0; i < 20; i++ {
77-
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001*float64(i))
78+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001*float64(i))
7879
}
79-
assert.Len(t, a.(*aggregator).currentThroughput["A"]["GET"].Probabilities, 10)
80+
assert.Len(t, a.(*aggregator).currentThroughput["A"][http.MethodGet].Probabilities, 10)
8081

8182
a, err = NewAggregator(testOpts, logger, metricsFactory, mockEP, mockStorage)
8283
require.NoError(t, err)
8384
// 20 of the same probabilities
8485
for i := 0; i < 20; i++ {
85-
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001)
86+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
8687
}
87-
assert.Len(t, a.(*aggregator).currentThroughput["A"]["GET"].Probabilities, 1)
88+
assert.Len(t, a.(*aggregator).currentThroughput["A"][http.MethodGet].Probabilities, 1)
8889
}
8990

9091
func TestLowerboundThroughput(t *testing.T) {
@@ -100,9 +101,9 @@ func TestLowerboundThroughput(t *testing.T) {
100101

101102
a, err := NewAggregator(testOpts, logger, metricsFactory, mockEP, mockStorage)
102103
require.NoError(t, err)
103-
a.RecordThroughput("A", "GET", model.SamplerTypeLowerBound, 0.001)
104-
assert.EqualValues(t, 0, a.(*aggregator).currentThroughput["A"]["GET"].Count)
105-
assert.Empty(t, a.(*aggregator).currentThroughput["A"]["GET"].Probabilities["0.001000"])
104+
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeLowerBound, 0.001)
105+
assert.EqualValues(t, 0, a.(*aggregator).currentThroughput["A"][http.MethodGet].Count)
106+
assert.Empty(t, a.(*aggregator).currentThroughput["A"][http.MethodGet].Probabilities["0.001000"])
106107
}
107108

108109
func TestRecordThroughput(t *testing.T) {
@@ -132,7 +133,7 @@ func TestRecordThroughput(t *testing.T) {
132133
require.Empty(t, a.(*aggregator).currentThroughput)
133134

134135
// Testing span with service name and operation but no probabilistic sampling tags
135-
span.OperationName = "GET"
136+
span.OperationName = http.MethodGet
136137
a.HandleRootSpan(span, logger)
137138
require.Empty(t, a.(*aggregator).currentThroughput)
138139

@@ -142,7 +143,7 @@ func TestRecordThroughput(t *testing.T) {
142143
model.String("sampler.param", "0.001"),
143144
}
144145
a.HandleRootSpan(span, logger)
145-
assert.EqualValues(t, 1, a.(*aggregator).currentThroughput["A"]["GET"].Count)
146+
assert.EqualValues(t, 1, a.(*aggregator).currentThroughput["A"][http.MethodGet].Count)
146147
}
147148

148149
func TestRecordThroughputFunc(t *testing.T) {
@@ -173,7 +174,7 @@ func TestRecordThroughputFunc(t *testing.T) {
173174
require.Empty(t, a.(*aggregator).currentThroughput)
174175

175176
// Testing span with service name and operation but no probabilistic sampling tags
176-
span.OperationName = "GET"
177+
span.OperationName = http.MethodGet
177178
a.HandleRootSpan(span, logger)
178179
require.Empty(t, a.(*aggregator).currentThroughput)
179180

@@ -183,5 +184,5 @@ func TestRecordThroughputFunc(t *testing.T) {
183184
model.String("sampler.param", "0.001"),
184185
}
185186
a.HandleRootSpan(span, logger)
186-
assert.EqualValues(t, 1, a.(*aggregator).currentThroughput["A"]["GET"].Count)
187+
assert.EqualValues(t, 1, a.(*aggregator).currentThroughput["A"][http.MethodGet].Count)
187188
}

0 commit comments

Comments
 (0)