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

Enable Lint Rules: early-return & indent-error-flow #5526

Merged
merged 2 commits into from
Jun 4, 2024
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
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ linters-settings:
# maybe enable, needs invesitgation of the impact
- name: get-return
disabled: true
# enable after cleanup
- name: early-return
disabled: true
# investigate, could be real bugs. But didn't recent Go version changed loop variables semantics?
- name: range-val-address
disabled: true
Expand All @@ -232,9 +229,6 @@ linters-settings:
- name: nested-structs
disabled: true
# enable after cleanup
- name: indent-error-flow
disabled: true
# enable after cleanup
- name: unexported-return
disabled: true
# looks useful, but requires refactoring: "calls to log.Fatal only in main() or init() functions"
Expand Down
6 changes: 3 additions & 3 deletions cmd/agent/app/reporter/grpc/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func (b *ConnBuilder) InitFromViper(v *viper.Viper) (*ConnBuilder, error) {
b.CollectorHostPorts = strings.Split(hostPorts, ",")
}
b.MaxRetry = uint(v.GetInt(retry))
if tls, err := tlsFlagsConfig.InitFromViper(v); err == nil {
b.TLS = tls
} else {
tls, err := tlsFlagsConfig.InitFromViper(v)
if err != nil {
return b, fmt.Errorf("failed to process TLS options: %w", err)
}
b.TLS = tls
b.DiscoveryMinPeers = v.GetInt(discoveryMinPeers)
return b, nil
}
18 changes: 9 additions & 9 deletions cmd/collector/app/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ func (opts *HTTPOptions) initFromViper(v *viper.Viper, logger *zap.Logger, cfg s
opts.IdleTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPIdleTimeout)
opts.ReadTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPReadTimeout)
opts.ReadHeaderTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPReadHeaderTimeout)
if tlsOpts, err := cfg.tls.InitFromViper(v); err == nil {
opts.TLS = tlsOpts
} else {
tlsOpts, err := cfg.tls.InitFromViper(v)
if err != nil {
return fmt.Errorf("failed to parse HTTP TLS options: %w", err)
}
opts.TLS = tlsOpts
return nil
}

Expand All @@ -252,11 +252,11 @@ func (opts *GRPCOptions) initFromViper(v *viper.Viper, logger *zap.Logger, cfg s
opts.MaxReceiveMessageLength = v.GetInt(cfg.prefix + "." + flagSuffixGRPCMaxReceiveMessageLength)
opts.MaxConnectionAge = v.GetDuration(cfg.prefix + "." + flagSuffixGRPCMaxConnectionAge)
opts.MaxConnectionAgeGrace = v.GetDuration(cfg.prefix + "." + flagSuffixGRPCMaxConnectionAgeGrace)
if tlsOpts, err := cfg.tls.InitFromViper(v); err == nil {
opts.TLS = tlsOpts
} else {
tlsOpts, err := cfg.tls.InitFromViper(v)
if err != nil {
return fmt.Errorf("failed to parse gRPC TLS options: %w", err)
}
opts.TLS = tlsOpts
opts.Tenancy = tenancy.InitFromViper(v)

return nil
Expand Down Expand Up @@ -289,11 +289,11 @@ func (cOpts *CollectorOptions) InitFromViper(v *viper.Viper, logger *zap.Logger)

cOpts.Zipkin.KeepAlive = v.GetBool(flagZipkinKeepAliveEnabled)
cOpts.Zipkin.HTTPHostPort = ports.FormatHostPort(v.GetString(flagZipkinHTTPHostPort))
if tlsZipkin, err := tlsZipkinFlagsConfig.InitFromViper(v); err == nil {
cOpts.Zipkin.TLS = tlsZipkin
} else {
tlsZipkin, err := tlsZipkinFlagsConfig.InitFromViper(v)
if err != nil {
return cOpts, fmt.Errorf("failed to parse Zipkin TLS options: %w", err)
}
cOpts.Zipkin.TLS = tlsZipkin
cOpts.Zipkin.CORS = corsZipkinFlags.InitFromViper(v)

return cOpts, nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/collector/app/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,10 @@ func TestStartDynQueueSizeUpdater(t *testing.T) {

// we wait up to 50 milliseconds
for i := 0; i < 5; i++ {
if p.queue.Capacity() == 100 {
time.Sleep(10 * time.Millisecond)
} else {
if p.queue.Capacity() != 100 {
break
}
time.Sleep(10 * time.Millisecond)
}

assert.EqualValues(t, 104857, p.queue.Capacity())
Expand Down
17 changes: 8 additions & 9 deletions cmd/es-rollover/app/init/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ func (c Action) Do() error {
return err
}
if c.Config.UseILM {
if version >= ilmVersionSupport {
policyExist, err := c.ILMClient.Exists(c.Config.ILMPolicyName)
if err != nil {
return err
}
if !policyExist {
return fmt.Errorf("ILM policy %s doesn't exist in Elasticsearch. Please create it and re-run init", c.Config.ILMPolicyName)
}
} else {
if version < ilmVersionSupport {
return fmt.Errorf("ILM is supported only for ES version 7+")
}
policyExist, err := c.ILMClient.Exists(c.Config.ILMPolicyName)
if err != nil {
return err
}
if !policyExist {
return fmt.Errorf("ILM policy %s doesn't exist in Elasticsearch. Please create it and re-run init", c.Config.ILMPolicyName)
}
}
rolloverIndices := app.RolloverIndices(c.Config.Archive, c.Config.SkipDependencies, c.Config.AdaptiveSampling, c.Config.IndexPrefix)
for _, indexName := range rolloverIndices {
Expand Down
5 changes: 2 additions & 3 deletions cmd/ingester/app/consumer/offset/concurrent_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ func getHighestContiguous(offsets []int64) int64 {

highestContiguous := minOffset
for {
if _, ok := offsetSet[highestContiguous+1]; ok {
highestContiguous++
} else {
if _, ok := offsetSet[highestContiguous+1]; !ok {
break
}
highestContiguous++
}

return highestContiguous
Expand Down
16 changes: 8 additions & 8 deletions cmd/internal/flags/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ func (s *Service) Start(v *viper.Viper) error {
sFlags := new(SharedFlags).InitFromViper(v)
newProdConfig := zap.NewProductionConfig()
newProdConfig.Sampling = nil
if logger, err := sFlags.NewLogger(newProdConfig); err == nil {
s.Logger = logger
grpcZap.ReplaceGrpcLoggerV2(logger.WithOptions(
// grpclog is not consistent with the depth of call tree before it's dispatched to zap,
// but Skip(2) still shows grpclog as caller, while Skip(3) shows actual grpc packages.
zap.AddCallerSkip(3),
))
} else {
logger, err := sFlags.NewLogger(newProdConfig)
if err != nil {
return fmt.Errorf("cannot create logger: %w", err)
}
s.Logger = logger
grpcZap.ReplaceGrpcLoggerV2(logger.WithOptions(
// grpclog is not consistent with the depth of call tree before it's dispatched to zap,
// but Skip(2) still shows grpclog as caller, while Skip(3) shows actual grpc packages.
zap.AddCallerSkip(3),
))

metricsBuilder := new(metricsbuilder.Builder).InitFromViper(v)
metricsFactory, err := metricsBuilder.CreateMetricsFactory("")
Expand Down
3 changes: 1 addition & 2 deletions cmd/jaeger/internal/extension/jaegerstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
emptyCfg := createDefaultConfig().(*Config)
if reflect.DeepEqual(*cfg, *emptyCfg) {
return fmt.Errorf("%s: no storage type present in config", ID)
} else {
return nil
}
return nil

Check warning on line 40 in cmd/jaeger/internal/extension/jaegerstorage/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/config.go#L40

Added line #L40 was not covered by tests
}
12 changes: 6 additions & 6 deletions cmd/query/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ func AddFlags(flagSet *flag.FlagSet) {
func (qOpts *QueryOptions) InitFromViper(v *viper.Viper, logger *zap.Logger) (*QueryOptions, error) {
qOpts.HTTPHostPort = v.GetString(queryHTTPHostPort)
qOpts.GRPCHostPort = v.GetString(queryGRPCHostPort)
if tlsGrpc, err := tlsGRPCFlagsConfig.InitFromViper(v); err == nil {
qOpts.TLSGRPC = tlsGrpc
} else {
tlsGrpc, err := tlsGRPCFlagsConfig.InitFromViper(v)
if err != nil {
return qOpts, fmt.Errorf("failed to process gRPC TLS options: %w", err)
}
if tlsHTTP, err := tlsHTTPFlagsConfig.InitFromViper(v); err == nil {
qOpts.TLSHTTP = tlsHTTP
} else {
qOpts.TLSGRPC = tlsGrpc
tlsHTTP, err := tlsHTTPFlagsConfig.InitFromViper(v)
if err != nil {
return qOpts, fmt.Errorf("failed to process HTTP TLS options: %w", err)
}
qOpts.TLSHTTP = tlsHTTP
qOpts.BasePath = v.GetString(queryBasePath)
qOpts.StaticAssets.Path = v.GetString(queryStaticFiles)
qOpts.StaticAssets.LogAccess = v.GetBool(queryLogStaticAssetsAccess)
Expand Down
17 changes: 8 additions & 9 deletions cmd/query/app/query_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ func (p *queryParser) parseTraceQueryParams(r *http.Request) (*traceQueryParamet

var traceIDs []model.TraceID
for _, id := range r.Form[traceIDParam] {
if traceID, err := model.TraceIDFromString(id); err == nil {
traceIDs = append(traceIDs, traceID)
} else {
traceID, err := model.TraceIDFromString(id)
if err != nil {
return nil, fmt.Errorf("cannot parse traceID param: %w", err)
}
traceIDs = append(traceIDs, traceID)
}

traceQuery := &traceQueryParameters{
Expand Down Expand Up @@ -360,11 +360,11 @@ func parseSpanKinds(r *http.Request, paramName string, defaultSpanKinds []string
func mapSpanKindsToOpenTelemetry(spanKinds []string) ([]string, error) {
otelSpanKinds := make([]string, len(spanKinds))
for i, spanKind := range spanKinds {
if v, ok := jaegerToOtelSpanKind[spanKind]; ok {
otelSpanKinds[i] = v
} else {
v, ok := jaegerToOtelSpanKind[spanKind]
if !ok {
return otelSpanKinds, fmt.Errorf("unsupported span kind: '%s'", spanKind)
}
otelSpanKinds[i] = v
}
return otelSpanKinds, nil
}
Expand All @@ -385,11 +385,10 @@ func (*queryParser) parseTags(simpleTags []string, jsonTags []string) (map[strin
retMe := make(map[string]string)
for _, tag := range simpleTags {
keyAndValue := strings.Split(tag, ":")
if l := len(keyAndValue); l > 1 {
retMe[keyAndValue[0]] = strings.Join(keyAndValue[1:], ":")
} else {
if l := len(keyAndValue); l <= 1 {
return nil, fmt.Errorf("malformed 'tag' parameter, expecting key:value, received: %s", tag)
}
retMe[keyAndValue[0]] = strings.Join(keyAndValue[1:], ":")
}
for _, tags := range jsonTags {
var fromJSON map[string]string
Expand Down
6 changes: 3 additions & 3 deletions cmd/remote-storage/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
// InitFromViper initializes Options with properties from CLI flags.
func (o *Options) InitFromViper(v *viper.Viper, logger *zap.Logger) (*Options, error) {
o.GRPCHostPort = v.GetString(flagGRPCHostPort)
if tlsGrpc, err := tlsGRPCFlagsConfig.InitFromViper(v); err == nil {
o.TLSGRPC = tlsGrpc
} else {
tlsGrpc, err := tlsGRPCFlagsConfig.InitFromViper(v)
if err != nil {

Check warning on line 58 in cmd/remote-storage/app/flags.go

View check run for this annotation

Codecov / codecov/patch

cmd/remote-storage/app/flags.go#L57-L58

Added lines #L57 - L58 were not covered by tests
return o, fmt.Errorf("failed to process gRPC TLS options: %w", err)
}
o.TLSGRPC = tlsGrpc

Check warning on line 61 in cmd/remote-storage/app/flags.go

View check run for this annotation

Codecov / codecov/patch

cmd/remote-storage/app/flags.go#L61

Added line #L61 was not covered by tests
o.Tenancy = tenancy.InitFromViper(v)
return o, nil
}
8 changes: 3 additions & 5 deletions pkg/es/client/index_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,10 @@ func (i IndicesClient) version() (uint, error) {
// CreateTemplate an ES index template
func (i IndicesClient) CreateTemplate(template, name string) error {
endpointFmt := "_template/%s"
if v, err := i.version(); err == nil {
if v >= 8 {
endpointFmt = "_index_template/%s"
}
} else {
if v, err := i.version(); err != nil {
return err
} else if v >= 8 {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
endpointFmt = "_index_template/%s"
}
_, err := i.request(elasticRequest{
endpoint: fmt.Sprintf(endpointFmt, name),
Expand Down
6 changes: 3 additions & 3 deletions pkg/fswatcher/fswatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@
if p == "" {
continue
}
if h, err := hashFile(p); err == nil {
w.fileHashContentMap[p] = h
} else {
h, err := hashFile(p)
if err != nil {

Check warning on line 90 in pkg/fswatcher/fswatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/fswatcher/fswatcher.go#L89-L90

Added lines #L89 - L90 were not covered by tests
return err
}
w.fileHashContentMap[p] = h

Check warning on line 93 in pkg/fswatcher/fswatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/fswatcher/fswatcher.go#L93

Added line #L93 was not covered by tests
dir := path.Dir(p)
if _, ok := uniqueDirs[dir]; !ok {
if err := w.watcher.Add(dir); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/queue/bounded_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ func helper(t *testing.T, startConsumers func(q *BoundedQueue, consumerFn func(i
q.StartLengthReporting(time.Millisecond, gauge)
for i := 0; i < 1000; i++ {
_, g := mFact.Snapshot()
if g["size"] == 0 {
time.Sleep(time.Millisecond)
} else {
if g["size"] != 0 {
break
}
time.Sleep(time.Millisecond)
}

c, g := mFact.Snapshot()
Expand Down
6 changes: 3 additions & 3 deletions plugin/storage/cassandra/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
f.primarySession = primarySession

if f.archiveConfig != nil {
if archiveSession, err := f.archiveConfig.NewSession(logger); err == nil {
f.archiveSession = archiveSession
} else {
archiveSession, err := f.archiveConfig.NewSession(logger)
if err != nil {
return err
}
f.archiveSession = archiveSession
} else {
logger.Info("Cassandra archive storage configuration is empty, skipping")
}
Expand Down
3 changes: 1 addition & 2 deletions plugin/storage/grpc/shared/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@
}

return &storage_v1.CloseWriterResponse{}, nil
} else {
return nil, status.Error(codes.Unimplemented, "span writer does not support graceful shutdown")
}
return nil, status.Error(codes.Unimplemented, "span writer does not support graceful shutdown")

Check warning on line 160 in plugin/storage/grpc/shared/grpc_handler.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/grpc/shared/grpc_handler.go#L160

Added line #L160 was not covered by tests
}

// GetTrace takes a traceID and streams a Trace associated with that traceID
Expand Down
Loading