Skip to content

Commit be9addf

Browse files
authored
Merge pull request #815 from drawks/linting-fixes
Updates for automated linting config and various code fixes to match
2 parents a59d46d + f11537f commit be9addf

File tree

29 files changed

+165
-107
lines changed

29 files changed

+165
-107
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v5
1313
- name: golangci-lint
14-
uses: golangci/golangci-lint-action@v6
14+
uses: golangci/golangci-lint-action@v9
1515
with:
1616
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
17-
version: v1.64.7
17+
version: v2.8.0
1818

1919
# Optional: working directory, useful for monorepos
2020
# working-directory: somedir

.golangci.yml

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
version: "2"
12
linters:
2-
disable-all: true
3+
default: none
34
enable:
4-
- gofmt
5-
- goimports
65
- gocritic
76
- ineffassign
7+
- asciicheck
8+
- misspell
9+
- promlinter
10+
- errorlint
11+
- govet
12+
- unparam
13+
- bodyclose
14+
- gochecknoinits
815

916
# enabled in carbonapi
10-
# - asciicheck
11-
# - bodyclose
12-
# - gochecknoinits
1317
# - gosec
14-
# - misspell
15-
# - unparam
1618

1719
# The linters we need to enable ASAP.
1820
# Enabling would require significant changes.
@@ -23,9 +25,29 @@ linters:
2325
# The linters that would be nice to have in order of decreasing priority.
2426
# They are disabled now because they cause a lot of warnings that would require multiple changes.
2527
# - revive
26-
# - errorlint
27-
# - promlinter
2828
# - cyclop or gocyclo
2929
# - whitespace
3030
# - gomnd
3131
# - lll
32+
33+
exclusions:
34+
generated: lax
35+
presets:
36+
- comments
37+
- common-false-positives
38+
- legacy
39+
- std-error-handling
40+
paths:
41+
- third_party$
42+
- builtin$
43+
- examples$
44+
formatters:
45+
enable:
46+
- gofmt
47+
- goimports
48+
exclusions:
49+
generated: lax
50+
paths:
51+
- third_party$
52+
- builtin$
53+
- examples$

carbon/app.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"github.com/go-graphite/go-carbon/tags"
2525
"github.com/lomik/zapwriter"
2626

27-
// register receivers
28-
_ "github.com/go-graphite/go-carbon/receiver/http"
29-
_ "github.com/go-graphite/go-carbon/receiver/kafka"
30-
_ "github.com/go-graphite/go-carbon/receiver/pubsub"
31-
_ "github.com/go-graphite/go-carbon/receiver/tcp"
32-
_ "github.com/go-graphite/go-carbon/receiver/udp"
27+
// receivers
28+
http_receiver "github.com/go-graphite/go-carbon/receiver/http"
29+
kafka_receiver "github.com/go-graphite/go-carbon/receiver/kafka"
30+
pubsub_receiver "github.com/go-graphite/go-carbon/receiver/pubsub"
31+
tcp_receiver "github.com/go-graphite/go-carbon/receiver/tcp"
32+
udp_receiver "github.com/go-graphite/go-carbon/receiver/udp"
3333
)
3434

3535
type NamedReceiver struct {
@@ -68,6 +68,8 @@ type App struct {
6868
FlushTraces func()
6969
}
7070

71+
var registerPluginsOnce sync.Once
72+
7173
// New App instance
7274
func New(configFilename string) *App {
7375
app := &App{
@@ -77,6 +79,15 @@ func New(configFilename string) *App {
7779
exit: make(chan bool),
7880
}
7981

82+
// Register all receivers explicitly
83+
registerPluginsOnce.Do(func() {
84+
http_receiver.Register()
85+
kafka_receiver.Register()
86+
pubsub_receiver.Register()
87+
tcp_receiver.Register()
88+
udp_receiver.Register()
89+
})
90+
8091
return app
8192
}
8293

@@ -456,7 +467,7 @@ func (app *App) Start() (err error) {
456467

457468
if conf.Carbonserver.TrigramIndex || conf.Carbonserver.TrieIndex {
458469
if fi, err := os.Lstat(conf.Whisper.DataDir); err != nil {
459-
return fmt.Errorf("failed to stat whisper data directory: %s", err)
470+
return fmt.Errorf("failed to stat whisper data directory: %w", err)
460471
} else if fi.Mode()&os.ModeSymlink == 1 {
461472
return fmt.Errorf("whisper data directory is a symlink")
462473
}
@@ -474,7 +485,7 @@ func (app *App) Start() (err error) {
474485
for _, rl := range conf.Carbonserver.HeavyGlobQueryRateLimiters {
475486
gqrl, err := carbonserver.NewGlobQueryRateLimiter(rl.Pattern, rl.MaxInflightRequests)
476487
if err != nil {
477-
return fmt.Errorf("failed to init Carbonserver.HeavyGlobQueryRateLimiters %s: %s", rl.Pattern, err)
488+
return fmt.Errorf("failed to init Carbonserver.HeavyGlobQueryRateLimiters %s: %w", rl.Pattern, err)
478489
}
479490
globQueryRateLimiters = append(globQueryRateLimiters, gqrl)
480491
}

carbonserver/cache_index_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (f *testInfo) commonCacheIdxTestHelper(t *testing.T) {
168168

169169
// assert [4] comes first, it has the smallest timestamp
170170
if !p1.Eq(points.OnePoint(addMetrics[4], 4, timestamps[4])) {
171-
fmt.Printf("error - recived wrong point - %v\n", p1)
171+
fmt.Printf("error - received wrong point - %v\n", p1)
172172
t.FailNow()
173173
}
174174

@@ -191,7 +191,7 @@ func (f *testInfo) commonCacheIdxTestHelper(t *testing.T) {
191191

192192
// assert [0] comes second, it has the second smallest timestamp
193193
if !p2.Eq(points.OnePoint(addMetrics[0], 0, timestamps[0])) {
194-
fmt.Printf("error - recived wrong point - %v\n", p2)
194+
fmt.Printf("error - received wrong point - %v\n", p2)
195195
t.FailNow()
196196
}
197197
f.checkExpandGlobs(t, addMetrics[0], true)

carbonserver/carbonserver.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ var statusCodes = map[string][]uint64{
147147
"capabilities": make([]uint64, 5),
148148
}
149149

150-
// interface to retrive retention and aggregation
150+
// interface to retrieve retention and aggregation
151151
// schema from persister.
152152
type configRetriever interface {
153153
MetricRetentionPeriod(string) (int, bool)
@@ -520,14 +520,14 @@ func (listener *CarbonserverListener) SetDoNotLog404s(doNotLog404s bool) {
520520
func (listener *CarbonserverListener) SetFailOnMaxGlobs(failOnMaxGlobs bool) {
521521
listener.failOnMaxGlobs = failOnMaxGlobs
522522
}
523-
func (listener *CarbonserverListener) SetMaxMetricsGlobbed(max int) {
524-
listener.maxMetricsGlobbed = max
523+
func (listener *CarbonserverListener) SetMaxMetricsGlobbed(m int) {
524+
listener.maxMetricsGlobbed = m
525525
}
526-
func (listener *CarbonserverListener) SetMaxMetricsRendered(max int) {
527-
listener.maxMetricsRendered = max
526+
func (listener *CarbonserverListener) SetMaxMetricsRendered(m int) {
527+
listener.maxMetricsRendered = m
528528
}
529-
func (listener *CarbonserverListener) SetMaxFetchDataGoroutines(max int) {
530-
listener.maxFetchDataGoroutines = max
529+
func (listener *CarbonserverListener) SetMaxFetchDataGoroutines(m int) {
530+
listener.maxFetchDataGoroutines = m
531531
}
532532
func (listener *CarbonserverListener) SetFLock(flock bool) {
533533
listener.flock = flock
@@ -637,8 +637,8 @@ func (listener *CarbonserverListener) ShouldThrottleMetric(ps *points.Points, in
637637

638638
return throttled
639639
}
640-
func (listener *CarbonserverListener) SetMaxInflightRequests(max uint64) {
641-
listener.MaxInflightRequests = max
640+
func (listener *CarbonserverListener) SetMaxInflightRequests(m uint64) {
641+
listener.MaxInflightRequests = m
642642
}
643643
func (listener *CarbonserverListener) SetNoServiceWhenIndexIsNotReady(no bool) {
644644
listener.NoServiceWhenIndexIsNotReady = no
@@ -964,13 +964,14 @@ func (listener *CarbonserverListener) updateFileList(dir string, cacheMetricName
964964
readFromCache = true
965965
for {
966966
entry, err := flc.Read()
967+
if errors.Is(err, io.EOF) {
968+
break
969+
}
967970
if err != nil {
968-
if !errors.Is(err, io.EOF) {
969-
infos = append(infos, zap.NamedError("file_list_cache_read_error", err))
971+
infos = append(infos, zap.NamedError("file_list_cache_read_error", err))
970972

971-
readFromCache = false
972-
trieIdx = newTrie(".wsp", listener.maxCreatesPerSecond, listener.estimateSize)
973-
}
973+
readFromCache = false
974+
trieIdx = newTrie(".wsp", listener.maxCreatesPerSecond, listener.estimateSize)
974975

975976
break
976977
}
@@ -1258,7 +1259,8 @@ func (listener *CarbonserverListener) updateFileList(dir string, cacheMetricName
12581259

12591260
func (*CarbonserverListener) logTrieInsertError(logger *zap.Logger, msg, metric string, err error) {
12601261
zfields := []zap.Field{zap.Error(err), zap.String("metric", metric)}
1261-
if ierr, ok := err.(*trieInsertError); ok {
1262+
var ierr *trieInsertError
1263+
if errors.As(err, &ierr) {
12621264
zfields = append(zfields, zap.String("err_info", ierr.info))
12631265
}
12641266
logger.Error(msg, zfields...)
@@ -2053,13 +2055,13 @@ type GlobQueryRateLimiter struct {
20532055
maxInflightRequests chan struct{}
20542056
}
20552057

2056-
func NewGlobQueryRateLimiter(pattern string, max uint) (*GlobQueryRateLimiter, error) {
2058+
func NewGlobQueryRateLimiter(pattern string, m uint) (*GlobQueryRateLimiter, error) {
20572059
exp, err := regexp.Compile(pattern)
20582060
if err != nil {
20592061
return nil, err
20602062
}
20612063

2062-
return &GlobQueryRateLimiter{pattern: exp, maxInflightRequests: make(chan struct{}, max)}, nil
2064+
return &GlobQueryRateLimiter{pattern: exp, maxInflightRequests: make(chan struct{}, m)}, nil
20632065
}
20642066

20652067
type ApiPerPathRatelimiter struct {

carbonserver/carbonserver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package carbonserver
22

33
import (
4+
"errors"
45
"fmt"
56
"math"
67
"os"
@@ -489,7 +490,7 @@ func TestGetMetricsListEmpty(t *testing.T) {
489490
}
490491

491492
metrics, err := carbonserver.getMetricsList()
492-
if err != errMetricsListEmpty {
493+
if !errors.Is(err, errMetricsListEmpty) {
493494
t.Errorf("err: '%v', expected: '%v'", err, errMetricsListEmpty)
494495
}
495496
if metrics != nil {

carbonserver/details.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (listener *CarbonserverListener) detailsHandler(wr http.ResponseWriter, req
107107
zap.String("reason", "response encode failed"),
108108
zap.Error(err),
109109
)
110-
http.Error(wr, fmt.Sprintf("An internal error has occured: %s", err), http.StatusInternalServerError)
110+
http.Error(wr, fmt.Sprintf("An internal error has occurred: %s", err), http.StatusInternalServerError)
111111
return
112112
}
113113
wr.Header().Set("Content-Type", contentType)

carbonserver/find.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
@@ -149,7 +150,8 @@ func (listener *CarbonserverListener) findHandler(wr http.ResponseWriter, req *h
149150
if err != nil || response == nil {
150151
var code int
151152
var reason string
152-
if _, ok := err.(errorNotFound); ok {
153+
var nf errorNotFound
154+
if errors.Is(err, &nf) {
153155
reason = "Not Found"
154156
code = http.StatusNotFound
155157
} else {
@@ -328,7 +330,7 @@ GATHER:
328330
}
329331
glob.Files, glob.Leafs, glob.TrieNodes, glob.Lookups, err = expandedResult.Files, expandedResult.Leafs, expandedResult.TrieNodes, expandedResult.Lookups, expandedResult.Err
330332
if err != nil {
331-
errors = append(errors, fmt.Errorf("%s: %s", expandedResult.Name, err))
333+
errors = append(errors, fmt.Errorf("%s: %w", expandedResult.Name, err))
332334
}
333335
expandedGlobs = append(expandedGlobs, glob)
334336
if responseCount == len(names) {
@@ -492,7 +494,8 @@ func (listener *CarbonserverListener) Find(ctx context.Context, req *protov2.Glo
492494
if err != nil || finalRes == nil {
493495
var code codes.Code
494496
var reason string
495-
if _, ok := err.(errorNotFound); ok {
497+
var nf errorNotFound
498+
if errors.As(err, &nf) {
496499
reason = "Not Found"
497500
code = codes.NotFound
498501
} else {

carbonserver/flc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ func ReadFileListCache(p string, version FLCVersion, writer io.Writer) error {
112112

113113
for {
114114
entry, err := flc.Read()
115+
if errors.Is(err, io.EOF) {
116+
return nil
117+
}
115118
if err != nil {
116-
if errors.Is(err, io.EOF) {
117-
return nil
118-
}
119119
return err
120120
}
121121

@@ -160,7 +160,7 @@ func NewFileListCache(p string, version FLCVersion, mode byte) (FileListCache, e
160160
case FLCVersionUnspecified, FLCVersion2:
161161
flcc.version = FLCVersion2 // for supporting FLCVersionUnspecified
162162
flc, err = newFileListCacheV2ReadOnly(flcc) // flcc is already closed if there is error.
163-
if err != nil && errors.Is(err, errFLCFallbackToV1) {
163+
if errors.Is(err, errFLCFallbackToV1) {
164164
// transparently detect which cache version is it.
165165
return NewFileListCache(p, FLCVersion1, mode)
166166
}

carbonserver/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (listener *CarbonserverListener) listHandler(wr http.ResponseWriter, req *h
124124
zap.String("reason", "response encode failed"),
125125
zap.Error(err),
126126
)
127-
http.Error(wr, fmt.Sprintf("An internal error has occured: %s", err), http.StatusInternalServerError)
127+
http.Error(wr, fmt.Sprintf("An internal error has occurred: %s", err), http.StatusInternalServerError)
128128
return
129129
}
130130
wr.Header().Set("Content-Type", contentType)
@@ -295,7 +295,7 @@ func (listener *CarbonserverListener) listQueryHandler(wr http.ResponseWriter, r
295295
zap.String("reason", "response encode failed"),
296296
zap.Error(err),
297297
)
298-
http.Error(wr, fmt.Sprintf("An internal error has occured: %s", err), http.StatusInternalServerError)
298+
http.Error(wr, fmt.Sprintf("An internal error has occurred: %s", err), http.StatusInternalServerError)
299299
return
300300
}
301301
wr.Header().Set("Content-Type", contentType)

0 commit comments

Comments
 (0)