Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add more linters #4881

Merged
merged 3 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ issues:
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters: [gosec]
linters: [gosec, bodyclose, noctx]

- path: crossdock
linters: [noctx]

# See https://github.com/jaegertracing/jaeger/issues/4488
- path: internal/grpctest/
Expand All @@ -22,16 +25,70 @@ linters:
disable:
- errcheck
enable:
# Plain ASCII identifiers.
- asciicheck

# Checks for dangerous unicode character sequences.
- bidichk

# Checks whether HTTP response body is closed successfully.
# TODO enable this but maybe find a way to disable in tests.
- bodyclose

# Check whether the function uses a non-inherited context.
- contextcheck

# Check declaration order of types, consts, vars and funcs.
- decorder

# Checks if package imports are in a list of acceptable packages.
- depguard

# Check for two durations multiplied together.
- durationcheck

# Checks `Err-` prefix for var and `-Error` suffix for error type.
- errname

# Suggests to use `%w` for error-wrapping.
# TODO enable this. Curently fails in about 20 places.
# - errorlint

# Checks for pointers to enclosing loop variables.
- exportloopref

- gocritic
- gofmt
- gofumpt
- goimports

# Allow or ban replace directives in go.mod
# or force explanation for retract directives.
# Maybe enable once we get rid of old sarama.
# - gomoddirectives

- gosec

# Linter that specializes in simplifying code.
- gosimple
- govet

# Detects when assignments to existing variables are not used.
- ineffassign

- misspell

# Finds naked/bare returns and requires change them.
- nakedret

# Require a bit more explicit returns.
- nilerr

# Finds sending HTTP request without context.Context.
- noctx

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

linters-settings:
depguard:
list-type: blacklist
Expand Down
7 changes: 6 additions & 1 deletion cmd/internal/status/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package status

import (
"context"
"flag"
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -37,7 +39,10 @@ func Command(v *viper.Viper, adminPort int) *cobra.Command {
Long: `Print Jaeger component status information, exit non-zero on any error.`,
RunE: func(cmd *cobra.Command, args []string) error {
url := convert(v.GetString(statusHTTPHostPort))
resp, err := http.Get(url)
ctx, cx := context.WithTimeout(context.Background(), time.Second)
defer cx()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions crossdock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func is2xxStatusCode(statusCode int) bool {
func httpHealthCheck(logger *zap.Logger, service, healthURL string) {
for i := 0; i < 240; i++ {
res, err := http.Get(healthURL)
res.Body.Close()
if err == nil && is2xxStatusCode(res.StatusCode) {
logger.Info("Health check successful", zap.String("service", service))
return
Expand Down
2 changes: 1 addition & 1 deletion internal/metricstest/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (b *Backend) Snapshot() (counters, gauges map[string]int64) {
}
}

return
return counters, gauges
}

// Stop cleanly closes the background goroutine spawned by NewBackend.
Expand Down
1 change: 1 addition & 0 deletions pkg/es/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (c *Client) request(esRequest elasticRequest) ([]byte, error) {
if err != nil {
return []byte{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return []byte{}, c.handleFailedRequest(res)
Expand Down
8 changes: 4 additions & 4 deletions plugin/metrics/disabled/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type (
// the METRICS_STORAGE_TYPE has not been set.
MetricsReader struct{}

// errMetricsQueryDisabled is the error returned by disabledMetricsQueryService.
errMetricsQueryDisabled struct{}
// errMetricsQueryDisabledError is the error returned by disabledMetricsQueryService.
errMetricsQueryDisabledError struct{}
)

// ErrDisabled is the error returned by a "disabled" MetricsQueryService on all of its endpoints.
var ErrDisabled = &errMetricsQueryDisabled{}
var ErrDisabled = &errMetricsQueryDisabledError{}

func (m *errMetricsQueryDisabled) Error() string {
func (m *errMetricsQueryDisabledError) Error() string {
return "metrics querying is currently disabled"
}

Expand Down
11 changes: 9 additions & 2 deletions plugin/sampling/strategystore/static/strategy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@

func (h *strategyStore) downloadSamplingStrategies(url string) ([]byte, error) {
h.logger.Info("Downloading sampling strategies", zap.String("url", url))
resp, err := http.Get(url)

ctx, cx := context.WithTimeout(context.Background(), time.Second)
defer cx()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("cannot construct HTTP request: %w", err)
}

Check warning on line 106 in plugin/sampling/strategystore/static/strategy_store.go

View check run for this annotation

Codecov / codecov/patch

plugin/sampling/strategystore/static/strategy_store.go#L105-L106

Added lines #L105 - L106 were not covered by tests
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to download sampling strategies: %w", err)
}

defer resp.Body.Close()

buf := new(bytes.Buffer)
if _, err = buf.ReadFrom(resp.Body); err != nil {
return nil, fmt.Errorf("failed to read sampling strategies HTTP response body: %w", err)
Expand Down
Loading