Skip to content

Commit

Permalink
Enable usestdlibvars linter (jaegertracing#6249)
Browse files Browse the repository at this point in the history
#### 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]>
  • Loading branch information
mmorel-35 authored Nov 24, 2024
1 parent 3f8c2e7 commit 49f33a4
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ linters:
# Checks usage of github.com/stretchr/testify.
- testifylint

# Detects the possibility to use variables/constants from the Go standard library.
- usestdlibvars

# TODO consider adding more linters, cf. https://olegk.dev/go-linters-configuration-the-right-version

linters-settings:
Expand Down
5 changes: 3 additions & 2 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package anonymizer

import (
"net/http"
"os"
"path/filepath"
"testing"
Expand All @@ -18,7 +19,7 @@ import (

var tags = []model.KeyValue{
model.Bool("error", true),
model.String("http.method", "POST"),
model.String("http.method", http.MethodPost),
model.Bool("foobar", true),
}

Expand Down Expand Up @@ -127,7 +128,7 @@ func TestAnonymizer_SaveMapping(t *testing.T) {
func TestAnonymizer_FilterStandardTags(t *testing.T) {
expected := []model.KeyValue{
model.Bool("error", true),
model.String("http.method", "POST"),
model.String("http.method", http.MethodPost),
}
actual := filterStandardTags(tags)
assert.Equal(t, expected, actual)
Expand Down
3 changes: 2 additions & 1 deletion cmd/anonymizer/app/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package writer

import (
"net/http"
"testing"
"time"

Expand All @@ -15,7 +16,7 @@ import (

var tags = []model.KeyValue{
model.Bool("error", true),
model.String("http.method", "POST"),
model.String("http.method", http.MethodPost),
model.Bool("foobar", true),
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/status/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Command(v *viper.Viper, adminPort int) *cobra.Command {
url := convert(v.GetString(statusHTTPHostPort))
ctx, cx := context.WithTimeout(context.Background(), time.Second)
defer cx()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/query/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,12 @@ func TestServerHTTPTenancy(t *testing.T) {
{
name: "no tenant",
// no value for tenant header
status: 401,
status: http.StatusUnauthorized,
},
{
name: "tenant",
tenant: "acme",
status: 200,
status: http.StatusOK,
},
}

Expand Down
4 changes: 2 additions & 2 deletions crossdock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {

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

func is2xxStatusCode(statusCode int) bool {
return statusCode >= 200 && statusCode <= 299
return statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices
}

func httpHealthCheck(logger *zap.Logger, service, healthURL string) {
Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *HTTPClient) GetJSON(ctx context.Context, _ string /* endpoint */, url s

defer res.Body.Close()

if res.StatusCode >= 400 {
if res.StatusCode >= http.StatusBadRequest {
body, err := io.ReadAll(res.Body)
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions examples/hotrod/pkg/tracing/rpcmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package rpcmetrics

import (
"net/http"
"sync"

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

func (m *Metrics) recordHTTPStatusCode(statusCode int64) {
switch {
case statusCode >= 200 && statusCode < 300:
case statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices:
m.HTTPStatusCode2xx.Inc(1)
case statusCode >= 300 && statusCode < 400:
case statusCode >= http.StatusMultipleChoices && statusCode < http.StatusBadRequest:
m.HTTPStatusCode3xx.Inc(1)
case statusCode >= 400 && statusCode < 500:
case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError:
m.HTTPStatusCode4xx.Inc(1)
case statusCode >= 500 && statusCode < 600:
case statusCode >= http.StatusInternalServerError && statusCode < 600:
m.HTTPStatusCode5xx.Inc(1)
}
}
Expand Down
3 changes: 2 additions & 1 deletion model/adjuster/sort_tags_and_log_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package adjuster

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestSortTagsAndLogFieldsDoesSortFields(t *testing.T) {
func TestSortTagsAndLogFieldsDoesSortTags(t *testing.T) {
testCases := []model.KeyValues{
{
model.String("http.method", "GET"),
model.String("http.method", http.MethodGet),
model.String("http.url", "http://wikipedia.org"),
model.Int64("http.status_code", 200),
model.String("guid:x-request-id", "f61defd2-7a77-11ef-b54f-4fbb67a6d181"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/bearertoken/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestRoundTripper(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(nil)
defer server.Close()
req, err := http.NewRequestWithContext(tc.requestContext, "GET", server.URL, nil)
req, err := http.NewRequestWithContext(tc.requestContext, http.MethodGet, server.URL, nil)
require.NoError(t, err)

tr := RoundTripper{
Expand Down
4 changes: 2 additions & 2 deletions pkg/clientcfg/clientcfghttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ func TestHTTPHandlerErrors(t *testing.T) {
withServer("", probabilistic(0.001), restrictions("luggage", 10), withGorilla, func(ts *testServer) {
handler := ts.handler

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

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

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

ts.metricsFactory.AssertCounterMetrics(t,
Expand Down
3 changes: 2 additions & 1 deletion pkg/es/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package eswrapper
import (
"context"
"fmt"
"net/http"
"strings"

esV8 "github.com/elastic/go-elasticsearch/v8"
Expand Down Expand Up @@ -193,7 +194,7 @@ func (c TemplateCreatorWrapperV8) Do(context.Context) (*elastic.IndicesPutTempla
if err != nil {
return nil, fmt.Errorf("error creating index template %s: %w", c.templateName, err)
}
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error creating index template %s: %s", c.templateName, resp)
}
return nil, nil // no response expected by span writer
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RegisterHandler(mu *http.ServeMux, logger *zap.Logger) {
logger.Fatal("Could not get Jaeger version", zap.Error(err))
}
mu.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
})
}
35 changes: 18 additions & 17 deletions plugin/sampling/strategyprovider/adaptive/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package adaptive

import (
"net/http"
"testing"
"time"

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

a, err := NewAggregator(testOpts, logger, metricsFactory, mockEP, mockStorage)
require.NoError(t, err)
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("B", "POST", model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("C", "GET", model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", "POST", model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", "GET", model.SamplerTypeLowerBound, 0.001)
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("B", http.MethodPost, model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("C", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", http.MethodPost, model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001)
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeLowerBound, 0.001)

a.Start()
defer a.Close()
Expand Down Expand Up @@ -74,17 +75,17 @@ func TestIncrementThroughput(t *testing.T) {
require.NoError(t, err)
// 20 different probabilities
for i := 0; i < 20; i++ {
a.RecordThroughput("A", "GET", model.SamplerTypeProbabilistic, 0.001*float64(i))
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeProbabilistic, 0.001*float64(i))
}
assert.Len(t, a.(*aggregator).currentThroughput["A"]["GET"].Probabilities, 10)
assert.Len(t, a.(*aggregator).currentThroughput["A"][http.MethodGet].Probabilities, 10)

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

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

a, err := NewAggregator(testOpts, logger, metricsFactory, mockEP, mockStorage)
require.NoError(t, err)
a.RecordThroughput("A", "GET", model.SamplerTypeLowerBound, 0.001)
assert.EqualValues(t, 0, a.(*aggregator).currentThroughput["A"]["GET"].Count)
assert.Empty(t, a.(*aggregator).currentThroughput["A"]["GET"].Probabilities["0.001000"])
a.RecordThroughput("A", http.MethodGet, model.SamplerTypeLowerBound, 0.001)
assert.EqualValues(t, 0, a.(*aggregator).currentThroughput["A"][http.MethodGet].Count)
assert.Empty(t, a.(*aggregator).currentThroughput["A"][http.MethodGet].Probabilities["0.001000"])
}

func TestRecordThroughput(t *testing.T) {
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestRecordThroughput(t *testing.T) {
require.Empty(t, a.(*aggregator).currentThroughput)

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

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

func TestRecordThroughputFunc(t *testing.T) {
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestRecordThroughputFunc(t *testing.T) {
require.Empty(t, a.(*aggregator).currentThroughput)

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

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

0 comments on commit 49f33a4

Please sign in to comment.