Skip to content

Commit fcc6d4e

Browse files
committed
Enable gochecknoinits linter and fix violations
* enables `gochecknoinits` linter in `.golintci.yaml * refactors receiver plugins to require explicit `Register` call instead of implicit `init` * updates `app.go` to explicitly register all receiver plugins guarded by `sync.Once` * updates recevier tests to explicitly call `Register` during `TestMain` * adds a `nolint:gochecknoinits` in `trie_test` to allow developer debug logging
1 parent 4cde14d commit fcc6d4e

File tree

12 files changed

+40
-12
lines changed

12 files changed

+40
-12
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ linters:
1111
- govet
1212
- unparam
1313
- bodyclose
14+
- gochecknoinits
1415

1516
# enabled in carbonapi
16-
# - gochecknoinits
1717
# - gosec
1818

1919
# The linters we need to enable ASAP.

carbon/app.go

Lines changed: 17 additions & 6 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

carbonserver/trie_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.uber.org/zap"
2222
)
2323

24+
// nolint:gochecknoinits
2425
func init() { log.SetFlags(log.Lshortfile) }
2526

2627
type logf interface {

receiver/http/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/prometheus/client_golang/prometheus"
1919
)
2020

21-
func init() {
21+
func Register() {
2222
receiver.Register(
2323
"http",
2424
func() interface{} { return NewOptions() },

receiver/http/http_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
func TestMain(m *testing.M) {
16+
Register()
17+
}
18+
1519
func TestHttp(t *testing.T) {
1620
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
1721
if err != nil {

receiver/kafka/kafka.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"go.uber.org/zap"
2424
)
2525

26-
func init() {
26+
func Register() {
2727
receiver.Register(
2828
"kafka",
2929
func() interface{} { return NewOptions() },

receiver/pubsub/pubsub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// the allocation overhead of repeatedly calling gzip.NewReader
2727
var gzipPool sync.Pool
2828

29-
func init() {
29+
func Register() {
3030
receiver.Register(
3131
"pubsub",
3232
func() interface{} { return NewOptions() },

receiver/pubsub/pubsub_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ var (
2121
testSub = "test-sub"
2222
)
2323

24+
func TestMain(m *testing.M) {
25+
Register()
26+
}
27+
2428
func newTestClient() (*pstest.Server, *pubsub.Topic, *pubsub.Client, error) {
2529
ctx := context.Background()
2630
srv := pstest.NewServer()

receiver/tcp/tcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/prometheus/client_golang/prometheus"
2525
)
2626

27-
func init() {
27+
func Register() {
2828
receiver.Register(
2929
"tcp",
3030
func() interface{} { return NewOptions() },

receiver/tcp/tcp_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ type tcpTestCase struct {
1616
rcvChan chan *points.Points
1717
}
1818

19+
func TestMain(m *testing.M) {
20+
Register()
21+
}
22+
1923
func newTCPTestCase(t *testing.T, protocol string) *tcpTestCase {
2024
test := &tcpTestCase{
2125
T: t,

0 commit comments

Comments
 (0)